Serial read and evaluation question

This may have to do with data types, or I'm missing something simple.
The below test code is running on a Teensy, receiving serial characters from a Nano.
I can send a single character from the Nano, "S" in the below example, and the serial monitor on the receiving Teensy will print out the sent character (called "rc" in the script) with Serial.println().

But if I immediately evaluate "rc" with an if conditional equating it to the character that the serial monitor prints it fails to evaluate as true to execute any actions.

#include <SoftwareSerial.h>
// Set up a new SoftwareSerial object
SoftwareSerial mySerial =  SoftwareSerial(2, 3); // pin 2 is TX, 3 is RX

void setup() {
    // Set the baud rate for the SoftwareSerial object
    mySerial.begin(9600);
    Serial.begin(9600); // debug with local serial monitor  
}

void loop() {
   char rc;

   while (mySerial.available() > 0){
      rc = mySerial.read();
      Serial.println(rc); // test
      if(rc == "S"){ 
         Serial.println("Anything");  //test
     }
  } // end while loop
}  // end main loop

I guess it's a bad conditional evaluation but I can't figure out why.

Thanks in advance for any enlightenment you can bestow upon me.

Try changing "S" to 'S' (or to 83). Using double-quotes ("S") creates a string, not a char.

1 Like

For a character compare you want single quotes 'S'

Yes, that works, thank you so much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.