Received data from HC-05 is not very reliable

I am trying to control a led strip using a c# program that sends data to the HC-05 bluetooth module.
The data I am trying to send is formatted like "CRRGGBBbb".
C = color
RR = red value
GG = green value
BB = blue value
bb = brightness value
From the 248 data sent, 137 data was received succesfully, so that means around 55%. Is that normal?
The code I am using is from Robin2 from his thread
Here it is

#include <Arduino.h>
#include <SoftwareSerial.h>

const byte numChars = 90;
byte receivedChars[numChars];

SoftwareSerial mySerial(10, 11);

boolean newData = false;

void recvWithStartEndMarkers() {
 static boolean recvInProgress = false;
 static byte ndx = 0;
 char startMarker = '<';
 char endMarker = '>';
 byte rc;
 
 while (mySerial.available() > 0 && newData == false) { // <<== NEW - get all bytes from buffer
 rc = mySerial.read();

 if (recvInProgress == true) {
            if (rc == endMarker) {
                receivedChars[ndx] = '\0'; // terminate the string
 recvInProgress = false;
 ndx = 0;
 newData = true;
            } else if (rc == startMarker) {
                receivedChars[ndx] = '\0'; // terminate the string
 ndx = 0;
 newData = false;
            } else {
                receivedChars[ndx] = rc;
 ndx++;
 if (ndx >= numChars) {
 ndx = numChars - 1;
 }
            }
 }

 else if (rc == startMarker) {
 recvInProgress = true;
 }
 }
}

void showNewData() {
    if (newData == true) {
        Serial.println((char*)receivedChars);
        newData = false;
    }
}

void setup()
{
 Serial.begin(115200);
 mySerial.begin(115200);
}

void loop()
{
 recvWithStartEndMarkers();
 showNewData();
}
mySerial.begin(115200);

Software serial does not work reliably at high baud rates. You might get 38400.

1 Like

cattledog:

mySerial.begin(115200);

Software serial does not work reliably at high baud rates. You might get 38400.

Thank you, now I have 100% reliability with this code. When I enable the leds and make it change color based on the data received then when I start selecting colors very quickly the reliability goes down to about 70-80 %. Maybe I can limit the amount of data you can send per second or is there a better way to tackle this problem?

or is there a better way to tackle this problem?

Use one of the Arduino's which have a second hardware serial port. Perhaps a MEGA with multtiple hardware serial ports, or the Leonardo which has both a USB and Hardware serial connection.

When I enable the leds and make it change color based on the data received then when I start selecting colors very quickly the reliability goes down to about 70-80 %.

What is sending the Bluetooth data so rapidly?

cattledog:
Use one of the Arduino's which have a second hardware serial port. Perhaps a MEGA with multtiple hardware serial ports, or the Leonardo which has both a USB and Hardware serial connection.

So by using a hardware port I am able to use higher baud rates and receive data more quickly?

cattledog:
What is sending the Bluetooth data so rapidly?

I made a C# application with a color picker and when I hold my mouse button then it just sends data whenever the color from the picker changes, so that is on every mouse move which is very quickly ofcourse

So by using a hardware port I am able to use higher baud rates and receive data more quickly?

Yes.

cattledog:
Yes.

Alright I learned a new thing here. Thank you!
Although I don't think the problem doesn't lie in the baudrate. Because when I don't write the data to the LEDS then all the data is coming in perfectly fine even when I am sending it at high rates. Problems are starting when I am sending data to the leds and sending data at high rates. Is this because the arduino can handle only so much instructions per second?

Problems are starting when I am sending data to the leds and sending data at high rates. Is this because the arduino can handle only so much instructions per second?

You never posted any code which did something with the received data, so how can we tell? There may be conflicts with interrupts used by Software Serial.

cattledog:
You never posted any code which did something with the received data, so how can we tell? There may be conflicts with interrupts used by Software Serial.

I just tried to do it without the software serial and I just used the hardware serial. From what it looks like it is now receiving the data 100% correctly. Thank you very much. I'm not sure why I ever started using the SoftwareSerial library