Sending Serial Command from one Arduino to Another

Hello guys,

Question about sending Serial command from one Arduino to another.

Using a Uno as a transmitter to send Serial data to a Mega as a receiver to turn on 3 LEDs on and then off every 2 seconds.
I would like to know why my sketch only allow me to send one digit data only, 0 to 9 or A to Z.

It is working fine right now , but as soon a I try to put 2 digits, it doesn't work.

What I did observed is when I change the letter or number on my receiver sketch from 1 digit to 2, it goes from Green to Gray (if (letter == 'A')) witch mean the Arduino IDE doesn't recognize it.

transmitter code

#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);

void setup()  
{
  softSerial.begin(9600);
} 
void loop()  
{ 
  softSerial.print("*A");
  delay(2000);
  softSerial.print("*B");
  delay(2000);
  softSerial.print("*C");
  delay(2000);
  softSerial.print("*0");
  delay(2000);
}

receiver code

 #include <SoftwareSerial.h>
  SoftwareSerial softSerial(10, 11);
  char letter  = ' ';
  int GREEN = 13;
  int YELLOW = 5;
  int RED = 2;
  

  void setup()
{
  softSerial.begin(9600);
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(RED, OUTPUT);
}


  void loop()
{
  if (softSerial.available())
{
  char letter = softSerial.read();
  
  if (letter == 'A') 
{
  digitalWrite(GREEN, HIGH);
}
  if (letter == 'B') 
{
  digitalWrite(YELLOW, HIGH);
}
  
  if (letter == 'C') 
{
  digitalWrite(RED, HIGH);
}  
  if (letter == '0') 
{
  digitalWrite(GREEN, LOW);
  digitalWrite(YELLOW, LOW);
  digitalWrite(RED, LOW);
 
}
  Serial.println(letter);
}
}

Thanks in Advance,

Cheers

See Serial Input Basics - updated

Thanks

If you send more than one char you need an End marker. This keeps sync. You need to buffer chars and the when you receive the end char (like cr), then parse the buffer.
You could also build it as a state machine, but the end character is still needed.

1 Like

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