As the title says I have a two dimensional array that I am attempting to read data into.
byte serialData[6][6]
If you look at the code below the idea is that I'm trying to take this
123456B789012C345678D012345E555555F666666
data and parse it. What I am doing appears to work great,
I use the SPACE char at the beginning to start the parse and then the B-F char to split the 6 bytes of data. Each time I get a new alpha char I am incrementing the first index of the array and then taking the data chars into the 2nd index.
When I print the values of serialData[i] I get the below results
[0] = 111111
[0][1][2][3][4][5]
[1] = NULL
[0][1][2][3][4][5]
[2] = 222222
[0][1][2][3][4][5]
[3] = NULL
[0][1][2][3][4][5]
[4] = 333333
[0][1][2][3][4][5]
[5] = NULL
[0][1][2][3][4][5]
If I print even farther than I am even defining in the array...
serialData[6] = 444444
serialData[7] = NULL
serialData[8] = 555555
What the heck am I doing wrong? Why is it incrementing twice?!?
Thank you!
// State machine values
int state =0;
byte serialData[6][6] = {0};
//serial data
byte serialChar =0;
boolean newData =0;
byte i2 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Serial 1 Good Morning");
Serial1.begin(115200);
Serial1.println("Serial 2 Good Morning");
}
void loop() {
if (Serial1.available() > 42) {
newData = true;
Serial.write("***** BEGIN DATA state = ");
if (state == 0){
for(i=0;i<43;i++)
{
serialChar = Serial1.read();
if (serialChar == 32){
state = 1;
serialProcess(state);
break;
}
}
}
}
delay(250);
if (newData){
Serial.write("{t1:");
Serial.print(serialData[0]);
Serial.write(",t2:");
Serial.print(serialData[1]);
Serial.write(",t3:");
Serial.print(serialData[2]);
Serial.write(",lat:");
Serial.print(serialData[3]);
Serial.write(",lon:");
Serial.print(serialData[4]);
Serial.write(",rpm:");
Serial.print(serialData[5]);
Serial.write("}");
Serial.println("");
newData =false;
}
}
void serialProcess(int st){
Serial.println("");
Serial.print("State of: ");
Serial.println(state);
for (byte i=0; i<7; i++) {
serialChar = Serial1.read();
if ((serialChar > 65) && (serialChar < 71)){
if (serialChar == 66){state = 2;}
if (serialChar == 67){state = 3;}
if (serialChar == 68){state = 4;}
if (serialChar == 69){state = 5;}
if (serialChar == 70){state = 6;}
serialProcess(state);
i2=0;
break;
}
else if ((serialChar != 32) && (serialChar !=255))
{
Serial.print("Echoing State before print state: ");
Serial.println(state);
Serial.print("Char: ");
Serial.write(serialChar);
Serial.print("");
serialData[(state-1)][i2] = serialChar;
i2++;
}
else if (serialChar == 13){
i2=0;
state=0;
Serial.println("");
Serial.println("***END OF DATA***");
break;
}
}
}
Moderator edit: [code] ... [/code] tags added to debug output. (Nick Gammon)