I am learning how to use arrays and I have read the Arduino tutorial on the subject. I am doing a basic project to learn how they work and just when I thought I was starting to understand them I got stuck. My goal is to send serial data such as SCH*,123,456,789# which is populated into an array and then split it into arrays based on the comma. I took the basic structure of the GPS sketch in the playground since it takes serial data, verifies the starting characters, uses a terminating character and then splits it into arrays based on a comma. In my sketch I am able to send serial data and verify the starting characters just fine. I use SCH* as starting characters to verify the data is valid and I use # as a terminating character which seems to work. This array works well, where I run into trouble is when I try to split the data into arrays based on the comma. My goal is to end up with an array such as SCH* is array 0, 123 is array 1, 456 is array 2, etc. My sketch does not seem read the initial array and find the comma in order to split it up. I put print statement all over the place to help me debug and understand better but I still cant seem to get past this. Any suggestions on where I went wrong or how I could use the array feature better?
int incoming = -1 ;
int count = 0;
int conta=0;
char linea[30] = "";
char verify[6] = "SCH*";
int check = 0;
int indicies[5];
int cont = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() {
incoming = Serial.read(); // Read a byte of the serial port
if (incoming == -1) { // See if the port is empty yet
delay(100); //delay if nothing is on the serial yet
}
else {
linea[conta] = incoming; // If there is serial port data, it is put in the buffer
conta++; //moves the array up one
}
//checks for an #
if (incoming == '#'){
Serial.println("linea got an #");
Serial.println(linea);
Serial.println("This is first array in verify");
Serial.println(verify[0]);
Serial.println("This is first array in linea");
Serial.println(linea[0]);
conta = 0; // resets the array once it gets an #
check = 0; //if ==3 then the start of the string is correct and should proceed to process data
for (int i=0;i<3;i++){ // Verifies if the received command starts with SCH*
if (linea[i]==verify[i]){
Serial.println("this is i");
Serial.println(linea[i]);
check++;
}
if(check ==3){
Serial.println("the initial string matches");
//incoming = -1;
for (int i=0; i< 30;i++){
//Serial.println("looping 30 times looking for ,");
//Serial.println("this is i");
Serial.print(linea[i]);
//
// Sketch gets stuck here and the section below does not work to separate based on the comma
//
if (linea[i]==','){
indicies[cont] = i; //used to seperate based on comma but does not read linea properly
cont++;
Serial.println("this is indices 0");
Serial.println(indicies[0]);
}
for (int i =0 ;i< 30;i++){
linea[i] = ' ';
}
}
}
else{
Serial.println("SCHED check does not match");
}
}
}
}