Serial Communication issue

Hello,

I am working on a project and need to create a remote command to control another arduino.

I used 2 HC-05 Bluetooth module, both well configured ( Master and Slave)
the 2 modules get paired without trouble

I am doing some tests with 3 pushbutton on my arduino "Master" and 3 Led on my Arduino "Slave."

It is not working and i don't understand why.

the slave seems to receive data but not the letter I send , it receive strange symbol "⸮" each time I push a button on Master

I added my code of both arduino.
If someone could give me some advice to fix it , that should be very kind.

Thanks in advance
Laplume

//Arduino Master code

#define GreenButton 3
#define RedButton 7
#define  BlueButton 9
int buttonState = LOW;

void setup() {
  pinMode(GreenButton, INPUT_PULLUP);
  pinMode(RedButton, INPUT_PULLUP);
  pinMode(BlueButton, INPUT_PULLUP);

  Serial.begin(38400);//init Bluetooth serial communication

}

void loop() {
  delay(100);
  buttonState = digitalRead(GreenButton);
  if (buttonState == LOW) {
    Serial.write('V');
  }

  delay(100);
  buttonState = digitalRead(RedButton);
  if (buttonState == LOW) {
    Serial.write('R');
  }

  delay(100);
  buttonState = digitalRead(BlueButton);
  if (buttonState == LOW) {
    Serial.write('B');
  }
}
//Arduino Slave code

#define GreenLed 3
#define RedLed 6
#define BlueLed 9
char LedColor;

void setup()
{
  pinMode(BlueLed, OUTPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(GreenLed, OUTPUT);
    
  Serial.begin(38400);//init Bluetooth serial communication
}

void loop()
{
  if (Serial.available() > 0)
    LedColor = Serial.read();
    Serial.println(LedColor);

  // Controle des LED
  if (LedColor == 'G') {
    digitalWrite(GreenLed, HIGH);
    delay(500);
    digitalWrite(GreenLed, LOW);
  }
  if (LedColor == 'R') {
    digitalWrite(RedLed, HIGH);
    delay(500);
    digitalWrite(RedLed, LOW);
  }
  if (LedColor == 'B') {
    digitalWrite(BlueLed, HIGH);
    delay(500);
    digitalWrite(BlueLed, LOW);
  }
}
[/quote]

A very well presented question and code.
Not being an expert I ask if You could try using a lower baudrate. For Serial monitor I've used 115200 but does Your periferal support 38400?
Receiving the wrong characters sounds like a baudrate mismatch, baudrate trouble, to me.

Random googling suggests that the default data rate is 9600 baud. Did you configure it differently?

@wildbill?
Yes. Do You know the HC-05 Bluetooth module? I don't and I don't have the ambition to look up and read specs for every questioner, therefore my wague advice.
Using specs and "worst case" made my designs never fail during plenty of yeras.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

@OP
Could this be a mistake?
    Serial.write('V'); This will send the binary value of V, not the ASCII character V.

Would     Serial.write("V"); make any difference? The same aspects apply to later coding.

Serial.write('V'); is the right way to send the character V in ASCII.

Also, OP, you might be interested in using this serial transfer library

First check your Bluetooth modules' baud rate, you will need to use AT commands to check, normally HC-05 are set at 9600 and in your case they don't have same baud rate.

AT+UART? tells you the default baud rate

AT+UART=9600 sets the baud rate to 9600.

Moreover, try using Serial.print instead of Serial.write

Hello everybody , Thanks for you help.
I fixed my issue

jackthom41 was righ, it was a baud rate problem.

both of my BT module was at 9600.

I changed them to 38400 , and now it is working perfectly !!

Laplume

Railroader:
@wildbill?
Do You know the HC-05 Bluetooth module?

No. Never used one. I googled it and skimmed the docs page looking for baud rate. Found it fairly quickly and then - case closed :slight_smile:

Laplume:
Hello everybody , Thanks for you help.
I fixed my issue

jackthom41 was righ, it was a baud rate problem.

both of my BT module was at 9600.

I changed them to 38400 , and now it is working perfectly !!

Laplume

Good to know that it worked for you.

Btw if both modules were at 9600 then it must have worked unless you have added 38400 in the code. So, instead of changing the baud rate of your modules, if you have just changed the baud rate in code to 9600 then it would have worked too. I hope you got that.