Go Down

Topic: Reading a txt file (Read 489 times) previous topic - next topic

Crebsyn

Ok so I am trying to read a txt file from the SD card (the file was written from the arduino and moved to a different directory) I am using the code below to convert the # but I can see I am getting Line feed and Carriage return #'s(-35, -38). How should I correct this? The # should be 259
Thank you for your help.

Reading Speed File
10000.00
2.00
5.00
9.00
-35.00
-38.00
25512.00
0.00
Code: [Select]
void AvgspeedR() {
  enableSPI();
  char AvgSR[27];
  sprintf(AvgSR, "/Read/Speed/speed%03d.txt", B+39);
  File myFile1 = SD.open(AvgSR);
  if (myFile1)
  {
    Serial.println("Reading Speed File");
    float decade = pow(10, (myFile1.available() - 1));
    Serial.println (decade);
    while(myFile1.available())
   {
     float temp3 = (myFile1.read() - '0');
     Serial.println (temp3);
     Matchspeed = temp3*decade+Matchspeed;
     decade = decade/10;
    }
  Serial.println (Matchspeed);
  Matchspeed = 0;
  Serial.println (Matchspeed);
  myFile2.close();
  }
  disableSPI();
}

johnwasser

In general the way to read numbers one character at a time is:
Code: [Select]

    int digit;
    int total = 0;
    while (true)
        {
        if (myFile1.available() == 0)
            break;
        digit = myFile1.read();
        if (digit == '\n' || digit == '\r')
            break;
        total = total * 10 + (digit - '0')
        }
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Crebsyn

Thank you, this works. I will have to spend some time figuring out how it works next.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy