Go Down

Topic: learning how to use arrays (Read 225 times) previous topic - next topic

fbriggs4

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?

Code: [Select]
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");
         }   
           

    }
     }
}
   
 

PaulS

Code: [Select]
   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
   }

Horse poop. Learn to use Serial.available(), and ditch the stupid delay()\.

Code: [Select]
       Serial.println(linea);
linea is an array of chars. It is NOT a string. You should not use it like one. To make it a string, so that the string functions work, NULL terminate the array.
   else {
     linea[conta] = incoming;        // If there is serial port data, it is put in the buffer
     conta++;  //moves the array up one
     linea[conta] = '\0';
   }

Code: [Select]
       Serial.println("This is first array in verify");
       Serial.println(verify[0]);
       Serial.println("This is first array in linea");
       Serial.println(linea[0]);

You are not printing arrays. You are printing characters - one element of the array.

If you properly NULL terminate the array, there are a number of string functions that make parsing trivial. Rather than write your own, use the professionally written, tested, and debugged functions. The strtok() function (NOT the strtok_r() function) can be used to get each token ("123", "456", and "789" in your example).

fbriggs4

Thank you very much for your time and advice, I will work on implementing your suggestions tonight.

Go Up