Hey guys,
I have a text file on an SD card and I would like to read this into an array on my Arduino.
The text file contains numbers in the form of:
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10 .. etc, until 24 rows. So its 24*10 comma separated variables.
Writing it into an array of the form: array[24][10] (so pretty self explanatory).
I got this to work in a normal C compiler .. however once copied into the Arduino it did not work. I have now learned that you cannot just copy and paste things and expect them to work
So, I have been looking around the internet/forums for code ideas and theres lots of posts out there but I cannot get something that actually works.
So far I have this:
#include <SD.h>
const int cs = 4;
int led[24][10],i=0,j=0;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing card.");
pinMode(53, OUTPUT);
if (!SD.begin(cs))
{
Serial.println("Card failed to initialize or no card available");
return;
}
Serial.println("Card initialized!");
// open the file named ourfile.txt
File myfile = SD.open("array.txt");
// if the file is available, read the file
if (myfile)
{
while (myfile.available())
{
//write array from file
for(i=0;i<24;i++)
{
for(j=0;j<10;j++)
{
led[i][j]=myfile.read();
}
}
}
myfile.close();
}
else {
Serial.println("Cannot open file!");
}
}
void loop()
{
//print out array to serial monitor
for(i=0;i<24;i++)
{
for(j=0;j<10;j++)
{
Serial.write(led[i][j]);
}
}
delay(10000000);
}
It .. kind of works but not really. When I read from the array in the void loop() it prints the first line from the file, then the last 5 lines and then a load of rubbish. So its definitely not right, its skipped 18 rows or something or its just put them into the array randomly - but it shows that the 'write' code has done something .. and like 5% of what it did was correct.
I assume its to do with the fact I haven't got anything here to actually skip the commas .. I was reading about strtok and parseint but I have never used them before and when I tried to figure them out - it gave me even more rubbish results than before!
If anybody could fix this for me then I would greatly appreciate it.
Thanks!