Go Down

Topic: read, save and compare arrays (Read 955 times) previous topic - next topic


Hi, I am currently doing some serial communication program.
As for the part to read from the Serial.read and save it in an array, I've managed to do it.

let say, if we send 'ms1'

Code: [Select]
if (Serial.available() > 0) // if new data is available, repeat this loop until Serial.available= 0
           inByte = Serial.read(); // Read new byte
           //Serial.print("READ : "); // Display the new byte
           stringx[string_len] = inByte; // Save the data in a character array
          // Serial.println(string[string_len]); // Print the characters that was recieved. -1 so that we would not read the '>' end string
           string_len++;  //add array 

on this line, stringx will have 'ms1'.

As usual, if I want to check the array, one of the ways are doing like:

Code: [Select]
        if(stringx[1]=='s'){   // if received 'm','s'
             if(stringx[2]=='1'){  //if received 'm','s','1'
             Serial.println("main switch is on");
             else if(stringx[2]=='0'){  //if received 'm','s','0'
             Serial.println("main switch is off");
              blinkLED(ledPin, 2, 200); // blinkLED(ledPin, times, delay);

which will check whether 'ms1' has received.

Is it possible for me to use something simple like this?

Code: [Select]
for(char i=0; i<(string_len-1); i++)   // this function copies and combine all the char

       Serial.println ("success!");   //put a new line

So I can compare all the string is stringx array, with the char that I wanted.

With above code, it gives me an error. Any simpler options also would be good.

Thank you.


Per Arduino ad Astra


The art of getting good answers lies in asking good questions.


Thanks both of you. 

Code: [Select]
if (strcmp(stringx, "ms1>")  == 0)  // test to see if the two strings are equal
       Serial.println ("success!");   //put a new line

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131