I am trying to figure out how to read various pieces of data stored in a CSV file. Each line would contain a combination of integers, floats, and text strings. I seem to be able to read the data, but I'm having problems storing the retrieved info. I think the primary problem is my attempts to convert the strings read into the proper variable types. Each row has items separated by comma's, and end with a '\n'. Anyone have some suggestions on what I may be doing wrong. I've included the primary routine I am working with below...
/*
SD card read/write
An attempt to show how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
*/
#include <SPI.h>
#include <SD.h>
#include <ctype.h>
File myFile;
struct parameters {
int Menu;
int Line; //1 - 4
int Item;
int Colmn; //0 - 19
int Size; //1 or 2
char description[21]; //text
int interval; //just a dummy place holder
} settings;
// Setting for SD-card reader
int index=0; //the index into the text char array TextStr
const int chipSelect = 10;
char charBuf[21];
/*****************************************************************
******************************************************************/
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("\nInitializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// WriteFile();
ShowFile();
AnalyzeFile(1,1);
}
/*****************************************************************
******************************************************************/
void loop()
{
// nothing happens after setup
}
/*****************************************************************
******************************************************************/
int AnalyzeFile(int Mnu,int itm)
{
int Pointer=0;
int cnt=0;
char TextStr[21];
Serial.println("Analyzing File...");
// Open the settings file for reading:
myFile = SD.open("Menu.csv");
Serial.print("Menu.csv opened\n");
char ch;
int j;
int ignore;
ch = myFile.read();
if (myFile)
{
while(myFile.available())
{
if (ch == '\n' || ch == 13 || ch == 10) //line is done prepare to analyze
{
//settings should be filled with info needed
Serial.print("Line ");
Serial.print(cnt);
Serial.print(" Read = [");
Serial.print(TextStr);
Serial.print("]\n");
ignore=false;
cnt++;
// description.toCharArray(myStrings,60);
// arrayStrings[x]=myStrings;
Serial.print("Menu:\t");
Serial.print("Menu:\t");
Serial.println(settings.Menu);
Serial.print("Line #:\t");
Serial.println(settings.Line);
Serial.print("Item #:\t");
Serial.println(settings.Item);
Serial.print("Column:\t");
Serial.println(settings.Colmn);
Serial.print("Char Size:\t");
Serial.println(settings.Size);
Serial.print("Menu Text:\t");
Serial.println(settings.description);
Serial.println("end");
}
else if(ch != '/') //simply ignore is a comment
{
ignore=true;
}
else if(ch != ',' && ignore != true)
{
//Serial.println(TextStr);
/* int Menu;
int Line; //1 - 4
int Item;
int Colmn; //0 - 19
int Size; //1 or 2
char TextStr[21]; //text
int interval; //just a dummy place holder
*/
if (Pointer >=1 && Pointer <= 6) { Serial.print(TextStr);}
if (Pointer==1) //points to particular variable to set...
{
settings.Menu = atoi(TextStr);
} else if (Pointer ==2) {// int Item
settings.Line = atoi(TextStr);
} else if (Pointer ==3) {// int Item
settings.Item = atoi(TextStr);
} else if (Pointer ==4) {// int Item
settings.Colmn = atoi(TextStr);
} else if (Pointer ==5) {// int Item
settings.Size = atoi(TextStr);
} else if (Pointer ==6) {// int Item
for (j==0; j <= 21; j++)
{
settings.description[j] = TextStr[j];
TextStr[j]='\0';
}
Serial.println("Pointer=6 Found \t");
Pointer=0;
ch = myFile.read();
} //endif Pointer
Pointer++;
}
else
{
TextStr[index++] = ch; //add the ascii character to the string.
} //end if
ch = myFile.read();
} //end while
myFile.close();
} // no datafile to read
else
{
Serial.println("No file to read");
} // endif(MyFile)
/* for (int i=0; i< 13; i++)
{
Serial.println(arrayStrings[i]);
delay(100);
}
*/
} //AnalyzeFile
/*****************************************************************
******************************************************************/
void ShowFile() {
int cnt=1;
char ch;
// re-open the file for reading:
myFile = SD.open("Menu.csv");
if (myFile) {
Serial.println("\nReading from: Menu.csv");
Serial.print(cnt);
Serial.print("\t");
// read from the file until there's nothing else in it:
while (myFile.available()) {
ch=myFile.read();
if (ch == '\n') {
cnt++;
Serial.println();
Serial.print(cnt);
Serial.print("\t");
} else {
Serial.print(ch);
} //endif
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
/*****************************************************************
******************************************************************/
void WriteFile()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("Menu.csv", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to Menu.csv...");
myFile.println("/ This is file containing menu items for Arduino Suziki Motorcycle Program by Larry Tetzner");
myFile.println("/ Menu #1");
myFile.println("1,1,1,0,1,This is first menu item");
myFile.println("/ Menu #2");
//myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening Menu.csv");
}
}
The file I'm reading has these lines....
/ This is file containing menu items for Arduino Suziki Motorcycle Program by Larry
/ Menu #1
1,1,1,0,1,This is first menu item
/
/ Menu #2
/ This is file containing menu items for Arduino Suziki Motorcycle Program by Larry
/ Menu #1
1,1,1,0,1,This is first menu item
/
/ Menu #2
/ This is file containing menu items for Arduino Suziki Motorcycle Program by Larry
/ Menu #1
1,1,1,0,1,This is first menu item
/
/ Menu #2
And results in the following output to the Serial port....
Initializing SD card...initialization done.
Reading from: Menu.csv
1 / This is file containing menu items for Arduino Suziki Motorcycle Program by Larry
2 / Menu #1
3 1,1,1,0,1,This is first menu item
4 /
......
86 Analyzing File...
Menu.csv opened
Line 0 Read = [ú]
Menu: Menu: 0
Line #: 0
Item #: 0
Column: 0
Char Size: 0
Menu Text:
end
Line 1 Read = [ú]
Menu: Menu: 0
Line #: 0
Item #: 0
Column: 0
Char Size: 0
Menu Text:
end
úLine 2 Read = [ú]
Menu: Menu: 0
Line #: 0
Item #: 0
Column: 0
Char Size: 0
Menu Text:
end
/[END POST]***********/