Thanks for the reference to strtok.
I managed to get the following code, but with one last issue, in the void readSD, where i call the function readCode, i need to have to myFile.read converted to a char* i tried several options but couldn't get it to work right.
Also the myFile.read is seen as an int, which should be a string as it contains non-numbers.
I managed to convert it to char, but then ofcourse i get the error cannot convert char to char*
Code:
#include <string.h>
#include <SPI.h>
#include <SD.h>
File myFile;
int SSPIN = 4;
int joints = 4;
char delimiters[] = " xH yH zH J ";
int frametime = 0;
void setup()
{
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(SSPIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//char instring[] = "xH 16.3 yH 5.5 zH 40.5 J 30.3";
}
void loop()
{
}
void readSD ()
{
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
readCode(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void readCode (char instring[])
{
char* valPosition;
//This initializes strtok with our string to tokenize
valPosition = strtok(instring, delimiters);
if(valPosition != NULL){
for(int i=0; i < joints; i++)
{
if(i == 0)
{
Serial.println("Move Servo xH to:");
}
if(i == 1)
{
Serial.println("Move Servo yH to:");
}
if(i == 2)
{
Serial.println("Move Servo zH to:");
}
if(i == 3)
{
Serial.println("Frame Delay:");
}
Serial.println(valPosition);
frametime = atof(valPosition);
//Here we pass in a NULL value, which tells strtok to continue working with the previous string
valPosition = strtok(NULL, delimiters);
}
}
Serial.println("Delay:");
Serial.println(frametime);
delay(frametime);
}