Problem sending a single digit across serial between two arduinos

Hi all! I have a switch attached to one arduino, and I am trying to convey the state of the switch to another arduino via serial connection.

On the TX board I have the following code:

int LightsOn = 0;
int LightsPin = 17;

void setup(void) {

  Serial.begin(9600);
  Serial2.begin(9600);

  pinMode(LightsPin, INPUT_PULLDOWN);


void loop(void) {
  
if (digitalRead(LightsPin) == LOW)LightsOn = 1;
if (digitalRead(LightsPin) == HIGH)LightsOn = 0;
 
Serial2.println(LightsOn);
//Serial.println(LightsOn);
}

On the RX board I have this code:

//----- Backlighting
int DashLEDPin = 35;
int PointerLEDPin = 36;
int FrontLEDPin = 37;
int RearLEDPin = 38;

int PointerBrightness = 255;
int FrontBrightness = 25;
int RearBrightness = 25;
int DashBrightness = 255;




//---Outputs
int Output_Lights1 = 14;
int Output_AC_LED = 1;

int rc = 0;

void setup() {
  pinMode(Input_Sidelights, INPUT_PULLUP);
  pinMode(Input_AC, INPUT_PULLDOWN);
  pinMode(Output_Lights1, OUTPUT);
  pinMode(Output_AC_LED, OUTPUT);
  Serial5.begin(9600);
}


void loop() {
Get_Serial_Data();

}




//-----------------------Receive Serial Data--------------------------------------------------------
void Get_Serial_Data() {
  //inChar = Serial1.read();

  if (Serial5.available() > 0) {
    rc = Serial5.read();
    Serial.println();
    if (rc == 1) {
      analogWrite(PointerLEDPin, PointerBrightness);
      analogWrite(FrontLEDPin, FrontBrightness);
      analogWrite(RearLEDPin, RearBrightness);
    }
    else {
      
    }
  }
}

For some reason, on the RX board, I see a few random integers on the serial5 read (10, 48 etc) and the 48 changes to 49 when I flick the switch. I'm not sure what's going on here, why isn't it just reading 0 or 1?

Thanks,

Nick

the 1 is transmitted as an ascii character

So how do I translate it back? Or is there a better way to transmit?

since your just communicating a switch value, compare the received char to an ascii char, if (rc == '1')

Ok that works! But one more problem, the leds I'm driving on the other side flicker, as RC seems to flick between 0 and 1. Is there a way to have the state stay fixed until the switch change is detected?

mechanical switch bounce. you can debounce the switch by checking that it's value is stable after a slight delay (10 msec) and only send when it has changed value

Yes my bad - I realized the error and updated it!

gcjr:
mechanical switch bounce. you can debounce the switch by checking that it's value is stable after a slight delay (10 msec) and only send when it has changed value

Hmm it's a mini toggle switch so shouldn't bounce... I have a debounced push switch elsewhere in the code but that seems over the top - is there a simple way to run a verification that the switch has changed?

You are sending the '0' or '1' using println() which adds a linefeed (10) to the character so when the receiver sees the linefeed it acts on it. Consider what the code does if what it receives is neither '0' or '1'

it's a mini toggle switch so shouldn't bounce

why?

UKHeliBob:
You are sending the '0' or '1' using println() which adds a linefeed (10) to the character so when the receiver sees the linefeed it acts on it. Consider what the code does if what it receives is neither '0' or '1'

Got it! put an if case on '0' and it works great... Thanks all!