Serial Data over Bluetooth

I have a very simple setup. A master arduino (32u4 chip) and a slave (32u4 chip). I have them talking over bluetooth and am simply sending a single byte; 1, 2, or a 3 from the master to the slave.

I have 3 led’s on the slave. When it receives a 1, it should light led1, when it receives a 2, it should light led2, and anything else it receives it should light led3.

I know that the data is being received at the slave because when I log the serial bus on the slave, I can see the 1, 2, and 3 show up on my terminal console.

I know the led’s work because if I do a simple digital.Write in setup() I can get each led to light.

My issue is… led’s 1 and 2 do not light up in the loop() when a 1 or a 2 are on the serial bus. It must be a simple mistake I am making, but cannot see it.

On the transmitter I am simply writing a 1, 2 or 3 conditionally:

 if (dot < -.15) {
    data = 1;
    Serial1.write(data);
  }

  else if (dot > .15) {
    data = 2;
    Serial1.write(data);
  }

  else {
    data = 3;
    Serial1.write(data);
  }

on the receiver, I am doing this:

byte data;

void setup() {

  Serial1.begin(9600);

  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);


}
void loop() {


  if (Serial1.available() > 0) {

    data = Serial1.read();

    if (data == 1) {
      digitalWrite(6, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
    }

    else if (data == 2) {
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(6, LOW);
    }

    else {
      digitalWrite(8, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(9, LOW);
    }
  }
}

When I have both units turned on, paired and connected, the slave lights up LED3 (pin8), but will not light the other two when a 1 or a 2 are on the serial bus.

Any suggestions or obvious errors I have made?

LED 3 lights up because it is the end of the line (the catchall 'else' statement). I am guessing that if you put "else if (data == 3)", then that will fail as well.

Are you sure you are sending numbers 1, 2, 3 and not characters '1', '2', and '3'?

Also, it makes no sense in the code snippet to set data to a number and then sending data, just send the value directly in the Serial.write() statement.

LED 3 lights up because it is the end of the line (the catchall 'else' statement). I am guessing that if you put "else if (data == 3)", then that will fail as well.

I had tested this, and indeed it does fail as well.

Are you sure you are sending numbers 1, 2, 3 and not characters '1', '2', and '3'?

I believe I am sending numbers. I have the variable "data" defined as a byte on both master and slave.
I will get rid of that variable.

If I do a " Serial1.write(2); " am I correct in assuming that it is sending the number 2 and not the character 2?

From the Serial reference:

write() Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

This implies you are sending the value 1 not the character '1'.

I would suggest that you check the interface using the serial monitor. Change the values to characters (like 'a', 'b' and 'c') that you can type and communicate with the receiving Arduino by typing in the command. Remember to turn off the line endings on the serial monitor. I would also print the value when you receive it. Once you have debugged that end you will be more confident to change it back to 1, 2, and 3 and use the sender again.

I updated both ends to be:

Master:

if (dot < -.15) {
    Serial1.print('a');
  }

  else if (dot > .15) {
    Serial1.print('b');
  }

  else {
    Serial1.print('c');
  }

Slave:

char data;

void setup() {

  Serial1.begin(9600);

  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);


}
void loop() {


  if (Serial1.available() > 0) {
    
    data = Serial1.read();

    if (data == 'a') {
      digitalWrite(6, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
    }

    else if (data == 'b') {
      digitalWrite(9, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(6, LOW);
    }

    else if (data == 'c')  {
      digitalWrite(8, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(9, LOW);
    }
  }
}

The serial monitor shows the letters a, b and c being received on the slave, but still no lights… this is baffling me…

The serial monitor shows the letters a, b and c being received on the slave,

How? There is no Serial.print() statement in the receiver code. And are you typing this in or still trying to send between Arduino processors?

BTW, you can Serial.write(‘a’). That would make sense in order to change the least amount.

Again, from your code snippet, I can’t tell if does the serial speed matches between sender and receiver?

At this stage I think that you need to post all of the sender code, or reduce the code to something that shows the problem. This process may also help you see what the issue might be.

The serial monitor shows the letters a, b and c being received on the slave

There is no serial monitor in the sketch posted. EDIT:Marco C beat me to it.

I have a very simple setup. A master arduino (32u4 chip) and a slave (32u4 chip). I have them talking over bluetooth and am simply sending a single byte; 1, 2, or a 3 from the master to the slave.

How are the bluetooth modules connected to the Ardudino's.

I am sending between the two arduinos.

both speeds are at 9600.

I know the characters are arriving on the slave because I am tapped into the RX line on the slave and sending it to a terminal and I can see the letters change between a, b, and c when commanded on the master.

I know the characters are arriving on the slave because I am tapped into the RX line

There is a difference between seeing the characters on the wire and actually knowing that your variable ‘data’ contains 'a, ‘b’ or ‘c’.

Yes! I believe you just made me see the light with your last comment...

These are custom PCB's I designed. They use the HC-05 bluetooth board, but not the one with the header pins, just the HC-05 board itself. I believe I swapped my TX and RX going from the board to the 32u4 chip on the slave. That would explain it all. I feel silly for making this mistake.

Thank you both for your patience and assistance. I love all that I am learning, but can sure be frustrating at times... :confused:

We need to see the rest of the code associated with this:

 if (dot < -.15) {
    data = 1;
    Serial1.write(data);
  }

  else if (dot > .15) {
    data = 2;
    Serial1.write(data);
  }

  else {
    data = 3;
    Serial1.write(data);
  }