R
R
Roman Popov2016-04-01 19:19:32
Arduino
Roman Popov, 2016-04-01 19:19:32

Read variable values ​​from file?

On the SD card, a file that contains strings consisting of two three-digit numbers. More or less like this:

123 567
137 457
265 356
...

You can use any separator, not just a space.
It is required to read this data line by line and assign values ​​to two variables. Ultimately, through simple calculations, these will be the angles of rotation for the two servos.
Tried like this:
#define N_SERVO 2
int PIN_SERVO[N_SERVO] = {2,3};
Servo servo[N_SERVO];

for (int i = 0; i < N_SERVO; i++)
{
  servo[i].attach(PIN_SERVO[i]));
}

data = SD.open("1.txt", FILE_READ);
while (data.available())
{
  for (int i = 0; i < N_SERVO; i++)
  {
   servo[i].write(data.parseInt( ));
  }
}
data.close();

Reads only one first DIGIT, not even a number, and closes the file.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Popov, 2016-04-01
@Roman_Popov

Figured it out myself. The habit of solving problems in a general way failed...
I threw out all the for loops. In the while loop:

while (myFile.available()) {
      int i=myFile.parseInt();
      int j=myFile.parseInt();
      int val = ((i*0.4905) - 55.0224) + 0.51;    
      myservo.write(val);
      delay (20);
      int val1 = ((j*0.4905) - 55.0224) + 0.51;    
      myservo1.write(val1);
      delay (20);
    }

A
Alexey Romanenko, 2016-04-01
@slimus

Did not work with sd but judging by the documentation ( https://www.arduino.cc/en/Tutorial/ReadWrite) you need to do data.read()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question