1

I have two microcontrollers talking to each other using serial UART pins.

Now for some reason which I can not figure out why... doing a Serial.print() does not work in an "if" statement. And also assigning/declaring/initializing variables and values inside the "if" statement does not work... this is really weird. I can turn on the GPIO pins in the "if" statement but other than that nothing else works.

Microcontrollers are: Arduino Nano and WeMos D1 Mini.

Just want to note that both of the microcontrollers are talking to each other fine and arduino can determine the serial input but it can not "Serial.print" in the "if" statement of "strcmp", it is being skipped.

EDIT: I just woke up this morning, and magically "Serial.print" is working properly inside the "if" statement of "strcmp"... I guess the microcontroller needed some rest, I was testing it for 13+ hours...

Please view the code below:

    void setup()
    {
      Serial.begin(9600);   
    }
    
    void loop() 
    {
      char a[5];
      delay(100);
      int i = 0;
    
      if (Serial.available() > 0)      
      {
        while(Serial.available() > 0)
        {
            a[i]=Serial.read(); 
            i++;
        }
        
        a[i] = '\0';  //last character inserted with a "null char".
      }
    
     if(strcmp(a,"RGB_1") == 0)   //<----- "if" statement, comparing the string array buffer
     {
        int m = 1; //<---------------- declaring and initializing a variable
        //Serial.print(m); //<-------- Serial monitor will hang and skip the GPIO pins below to trigger.
        digitalWrite(12, HIGH);
        delay(1000);
        Serial.print(m); //<---------- "if" statement allows the GPIO pins to trigger but skips this serial print.
     }
   }
1
  • 2
    print the value of i
    – jsotola
    Nov 19, 2020 at 8:33

1 Answer 1

5

High likely you are exceeding your character array, see:

char a[5];

Which can hold 5 characters. However, you write:

if(strcmp(a,"RGB_1") == 0)

Which assumes you receive "RGB_1" which is 6 characters, because a string ends with a 0.

What also might happen is that you indeed receive only 5 characters without the ending newline, which means strcmp keeps searching until it finds a string end which may not happen.

So you can increase the array size a few just for testing purposes, and print i as jsotola proposes. Beyond that, also print each character you put into the array so you know how/if to handle a new line.

If you don't receive a string end, then use instead of the strcmp function, then use strncmp, which can search for exactly 5 characters, not checking until the end of the string.

3
  • 1
    @S-To: No. a[5] holds 5 values, indexed 0 to 4.
    – ocrdu
    Nov 19, 2020 at 14:06
  • 1
    @ Michel Keijzers Thank You for quick comment.
    – S To
    Nov 19, 2020 at 14:35
  • 1
    @ocrdu Yes you're right, my bad.... just had a brain fart.
    – S To
    Nov 19, 2020 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.