Reading a float from an SD Card

Status
Not open for further replies.

benchtester

New member
Hello All,

I hate to bother everyone with my ignorant question, but I haven't used C much in the last 28 years. What I want to do is give a customer a Teensy 3.2 controller with the SD Card adapter. I simply want to provide way for them to enter 3 floating point numbers in a text file on the card and then have the program read them as floats (or convert to floats) and assign their values to variables in the program.

For example the text file could written like this (with Notepad for example):

0.058459
0.020367
5.000000

I currently have a working board and have successfully run a segment of the ReadWrite example in the SD directory:

// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

This successfully sends the file information to the serial monitor. The problem is that I don't know how to convert this information to floats and assign them to variables.
In particular, I don't know what's going on here:
while (myFile.available()) {
Serial.write(myFile.read());
}


I would really appreciate it if someone could give me guidance on how to turn the information into 3 floats. If there is a different but better way, that would be great too.

Thanks in advance,
Eric
 
Arduino Stream parseFloat could be what you need:
https://www.arduino.cc/reference/tr/language/functions/communication/stream/streamparsefloat/

The following code snippet (untested) should extract all float numbers from the file
Code:
// Search for float numbers until end of file:
while (myFile.available()) {
  float fvalue = myFile.parseFloat(SKIP_ALL);
  Serial.print("Found float value ");
  Serial.println(fvalue);
}

Thank you, I got this working and the code looks pretty clean (unlike my previous attempts).
 
Status
Not open for further replies.
Back
Top