I have to repost this after I realized my previous post was not following the ‘standard’ here. My apologies.
At the current stage of my project, I’m trying to read the CSV file from the SD card. I know this topic has been posted many times, and there are examples I can find online, or even here. Believe me, I have gone through those tutorials/examples, and tried to use those programs. Apparently, I have managed to get the data printed on the serial monitor, but the readings are somehow inaccurate (some corrects, some are not). The CSV file contains a series of Longitude and Latitude data of a route. Here is the example of the data:
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("datalog3.csv");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
String a="";
float Wval;
for(int i=0;i<99;++i)
{
char temp=myFile.read();
// Serial.println(temp);
// Serial.print("ciclo ");
// Serial.println(i);
if(temp!=','&&temp!='\n')
{ //a=temp;
a+=temp;}
else if(temp==','||temp=='\n'){
Serial.println(a);
char buf[a.length()];
a.toCharArray(buf,a.length());
Wval=atof(buf);
Serial.println(Wval,4);
break;}
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
I’m not sure why it gives different reading from the file. I just need for the program to read the data correctly from the columns and following the sequence, so that I can proceed with the next stage. Really appreciate if somebody can help me out with this. Thank you.
PaulS:
You know the maximum length of a record. Ditch the stupid String class. Use a char array to hold the data.
I do not understand why you are puzzled by the differences. Go read the documentation on float for the Arduino. What you see is perfectly reasonable.
The reading needs to be very accurate, bcos it will determine the success of my project in general and to allow me to proceed to the next stage. But thank you for your advice.
The reading needs to be very accurate, bcos it will determine the success of my project in general
Then your project is doomed to failure. A float is good for 6 or 7 digits of precision. That would be 4530.39 or maybe 4530.397. Printing a float that is in the thousands to 4 decimal places is just wishful thinking.
You know, you could think outside of the box and write a protocol that automatically multiplies those numbers by 10^(whatever you feel comfortable with) then when you receive the numbers, have the second protocol automatically divide by the same number… of course i’m actually kind of fond of the float solution that i mentioned Kruspi implemented straight into the IDE.
Thomas499:
You know, you could think outside of the box and write a protocol that automatically multiplies those numbers by 10^(whatever you feel comfortable with) then when you receive the numbers, have the second protocol automatically divide by the same number.... of course i'm actually kind of fond of the float solution that i mentioned Kruspi implemented straight into the IDE.
I have tried to convert the string into the int., unfortunately I have the same problem of conversion. I tried:
….
Serial.println(a);
int b=atoi(a.c_str());
Serial.println(b);
And the print was:
84481211
121341
I have also tried:
int b= a.toInt();
But it still gave the same output. I'm sorry, but I’m not familiar with the concept of conversion in Arduino.
But it still gave the same output. I'm sorry, but I'm not familiar with the concept of conversion in Arduino.
Nor with the concept of posting all of your code.
Nor with the concept of limits on the size of the value that can be stored in a given type. And, that is so easy to check. Go look at the reference page NOW!