I tried to read a Dataset from a SD card on my Yun.
The Data is in a .txt doc with 4 columns (seperated with a HT) and I want to fill the first 1D array with the fisrt column and another 3D array with the other 3 columns.
I started with a unsinged char array for my incoming ASCII characters but the atof funtction can't convert it to a float number. The error message is
SD_read:59: error: invalid conversion from 'unsigned char' to 'const char*' [-fpermissive]
SerialUSB.println(atof(read_buffer[0]);
Am I using the atof function wrong?
#include <FileIO.h>
void setup() {
// Initialize the Bridge and the Serial
Bridge.begin();
Serial.begin(9600);
FileSystem.begin();
while (!SerialUSB); // wait for Serial port to connect.
SerialUSB.println("Filesystem datalogger\n");
}
void loop() {
int i;
unsigned char read_buffer[100];
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_READ);
// if the file is available, write to it:
if (dataFile) {
while(dataFile.available()){
read_buffer[dataFile.position()] = dataFile.read();
SerialUSB.println(atof(read_buffer);
}
//SerialUSB.println(atoi(read_buffer));
//dataFile.close();
// print to the serial port too:
}
// if the file isn't open, pop up an error:
else {
SerialUSB.println("error opening datalog.txt");
}
dataFile.close();
delay(15000);
}
The atof() function operates on an array of chars terminated by a zero, otherwise known as a C style string.
Your SerialUSB.println(atof(read_buffer[0]); has 2 problems. The first is that the parameter being passed to atof() is not a zero terminated array of chars and secondly there is a parenthesis missing.
I hope that there are less than 100 characters in the file. Why are you calling atof() after adding each character? Why are you calling a function that expects a string with a char array?
You need to learn the difference between a string and a char array. Hint: A string is a NULL-terminated something.
#include <FileIO.h>
void setup() {
// Initialize the Bridge and the Serial
Bridge.begin();
Serial.begin(9600);
FileSystem.begin();
while (!SerialUSB); // wait for Serial port to connect.
SerialUSB.println("Filesystem datalogger\n");
}
void loop() {
int i,j;
int inChar;
String inString = "";
float array1[50];
float array2[10];
//float array3[10]
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_READ);
//str_buffer = '0';
// if the file is available, write to it:
if (dataFile) {
i=0;
while(dataFile.available()){
array1[i] = dataFile.parseFloat();
array2[i][0] = dataFile.parseFloat();
array2[i][1] = dataFile.parseFloat();
array2[i][2] = dataFile.parseFloat();
SerialUSB.println(array1[i]);
i++;
/*
if ((inChar>45)&&(inChar<58)) {
inString += (char)inChar;
i++;
}
else if (inChar <11){
input[i] = inString.toFloat();
inString = "";
}
*/
}
}
// if the file isn't open, pop up an error:
else {
SerialUSB.println("error opening datalog.txt");
}
dataFile.close();
//for (j=0;j<3;j++)
//{
/*j=0;
for(i=0;i<4;i++){
SerialUSB.println("erste:");
SerialUSB.println(array2[0][0]);
} */
//}
delay(30000);
}