My code is listed at bottom.
I tested to make sure Serial incoming wasn't a problem. Using Serial Monitor, I sent "100,100,1,200,200,1,300,300,1,400,400,1,500,500,1,600,600,1,700,700,1\n"
The serial reads the string and stores it fine. Any new serial interrupt would result in 'serial event' being printed. inputString isn't reset until parsing is fully complete, checked that as well.
Is there a better way to get Ints out of strings, like the one listed above? String length will change.
I am researching the code below, which sounded like it should work, but I am a newb, so slow going.
char *tmp;
int i = 0;
tmp = strtok(&readString[0], ",");
while (tmp) {
ArrayKey[i++] = atoi(tmp);
tmp = strtok(NULL, ",");
}
My current code
//-----Variables --------------------------
int numPoints = 0;
int myPoints[200][3];
String inputString = ""; // a string to hold incoming data
boolean debug=true;
boolean stringAvail = false;
String com1 = "";
void setup()
{
Serial.begin(19200);
//Serial.println("setup");
//----- TLV5618 SPI Setup -----
}
void loop()
{
if(debug)
{
//Serial.println("in loop");
for (int i = 0;i<numPoints;i++)
{
char mesg[100];
sprintf(mesg, "Stored point: %d,%d,%d,%d", myPoints[i][0], myPoints[i][1], myPoints[i][2], numPoints);
Serial.println(mesg);
delay(10);
}
}
//draw_wireframe();
if (stringAvail)
{
com1 = inputString;
parseString();
Serial.println("clearing input");
inputString = "";
stringAvail = false;
}
delay(100);
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
if(debug){Serial.println("serial event");}
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
if (inChar == '\n')
{
if(debug)
{
Serial.print("inputString: ");
Serial.println(inputString);
}
//parseString(inputString);
//inputString = "";
stringAvail = true;
}
else
{
// add it to the inputString:
inputString += inChar;
}
}
}
void parseString()
{
numPoints = 0;
while (com1.length()>1)
{
myPoints[numPoints][0] = setPos(com1);
myPoints[numPoints][1] = setPos(com1);
myPoints[numPoints][2] = setPos(com1);
numPoints++;
}
}
int setPos(String com)
{
int x = com.substring(0,com.indexOf(',')).toInt();
com1 = com.substring(com.indexOf(',')+1);
return x;
}