Agreed, testing is definitely needed.
While futzing around last night, learning the SD library and what not, I came up with this little snippet:
#include <SD.h>
File dataFile;
int done = 0;
char c;
String data;
void setup() {
Serial.begin(9600);
Serial.println("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop() {
dataFile = SD.open("data.txt", FILE_READ);
if (!done) {
if (dataFile) {
while (dataFile.available()) {
c = dataFile.read();
switch(c) {
case ',':
Serial.print(data);
Serial.print(" - ");
data = "";
break;
case '\n':
Serial.println(data);
/*
Process instructions here ...
Break up into {angle_pan (INT), speed_pan (INT), angle_tilt (INT), speed_tilt (INT), pause (INT)}
*/
Serial.println("new line detected!");
data = "";
break;
default:
data = data + String(c);
break;
}
}
done = 1;
dataFile.close();
Serial.println();
Serial.println("Done reading file.");
} else {
Serial.println("error opening data.txt");
}
}
}
The data.txt file looks like this:
90,2,90,2,5
45,4,135,4,2
135,4,45,4,2
90,2,90,2,5
The sketch does what I'm expecting it to do, it prints out (I'm not at my workbench, so I'm writing from memory here)
90 - 2 - 90 - 5
new line detected!
45 - 4 - 135 - 4 - 2
new line detected!
etc., etc.
Now, I did that as an exercise, see if I could figure out the parsing and all of that. Great. However, what I need are INTs, not Strings. Because I need to pass that data on to the Servo library. And this is where I wish I had paid more attention, converting from one to the other.
If you notice, in my sketch I wrote myself a little comment of where I need to parse the full instruction line. I don't know how to actually do that. Ideally it's just an array with the various values in it which I will then pass on to the servo function to do its thing.
Some help will be much appreciated here.