Bluetooth Function I Don't Understand

Could someone please help me understand this code? This is code to receive Bluetooth info from the Arduino serial port. I've not seen this used before and I'm just struggling to understand the flow. Thank you in advance for the insight!

Ken Adams

/***********************Variable definitions***********************/

byte RX_package[11] = {0};
int Serialcount = 0;
char x_axis = 0;                                            // Store variables along the X-axis
char y_axis = 0;                                            // Store variables on the Y axis
byte klaxon = 0;                                            // The default storage rate is1 ~ 255
byte S_Button = 0;                                          // Store a clockwise rotation variable
byte N_Button = 0;                                          // Store counterclockwise rotation variables
byte mode1_Button = 0;
byte mode2_Button = 0;
byte mode3_Button = 0;
byte mode1_var = 0;
byte mode2_var = 0;
byte mode3_var = 0;

/*********************************************************
Function name: RX_Information()
Function Function: Receives data packets through Bluetooth
Function parameters: None
The function returns: none
*********************************************************/
void RX_Information(void)
{
    if(Serial.available() > 0)
    {
        delay(1);                                           // delay 1 ms
        if(Serial.readBytes(RX_package, 11))
        {
            if (RX_package[0] == 0xA5 && RX_package[10] == 0x5A)     // 
            {
                Serialcount = 0;
                x_axis = RX_package[1];                     // The X-axis value
                y_axis = RX_package[2];                     // The Y-axis value
                klaxon = RX_package[3];                     // the horn
                N_Button = RX_package[4];                   // Rotate counterclockwise
                S_Button = RX_package[5];                   // Rotate clockwise
                // mode1_Button = RX_package[6];
                // mode2_Button = RX_package[7];
                // mode3_Button = RX_package[8];
                if(RX_package[6] > 0)
                {
                    ResetCarState();
                    RGB(GREEN);
                    mode1_var = 1;
                    mode2_var = 0;
                    mode3_var = 0;
                }
                else if(RX_package[7] > 0)
                {
                    ResetCarState();
                    RGB(BLUE);
                    mode1_var = 0;
                    mode2_var = 1;
                    mode3_var = 0;
                }
                else if (RX_package[8] > 0)
                {
                    ResetCarState();
                    RGB(PURPLE);
                    mode1_var = 0;
                    mode2_var = 0;
                    mode3_var = 1;
                }
                
            }
            else
            {
                Serialcount++;
                return;
            }
        }
    }
    else
    {
        Serialcount++;
        if(Serialcount > 300)
        {
            klaxon = 0;
            x_axis = 0;
            y_axis = 0;
            N_Button = 0;
            S_Button = 0;
        }
    }
}


What device does the data come from? Do you know anything about the data that would help understand the way the code parses it?

I've not seen this used before

What is the "this" that you refer to?

The data is coming from an Android app. The Android app sends a bluetooth signal to a bluetooth module (HC-05) that interfaces with the Arduino Nano to control the 2 wheel robot (turn left, right, forward, reverse, etc.).

The "this" I'm referring to is the Arduino communication protocol being used here. For example, I'm used to an Android app sending a simple "L" to turn left, "R" to turn right, etc. That seems simple to me. The way it is done here with the "if(Serial.readBytes(RX_package, 11))" is throwing me for a loop just because I'm having a hard time understanding what is being done in the code. I hope that helps.

So it will be a game of 100 questions, then? What is the "android app" that you refer to? Do you know anything about the android app that would help us understand the way the code parses it?

Sorry, I know little to nothing about the Android app. That was my goal is to write my own app that I DO understand. Not much help here.

Is this part, at least, not a clue to the meaning of the data packet members?

Then why not write one using the "L", "R" etc. technique that you say you do understand?

From me/us, or from the supplier of the software/hardware that you are now working with?

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#hardware
"If you are using something that is a non-Arduino product then it helps to have a link to its technical data sheet or a link to where you bought it."

Somewhat. See below:

x_axis = RX_package[1]; // The app has a joystick and this is the X-axis value (-127 to 127)
y_axis = RX_package[2]; // The app has a joystick and this is the Y-axis value (-127 to 127)
klaxon = RX_package[3]; // there is a buzzer on the robot. This turns it on and off
N_Button = RX_package[4]; // Rotates the robot counterclockwise
S_Button = RX_package[5]; // Rotates the robot clockwise

Somewhat, what?

Please follow the forum protocols and post all the information about the system you are working with.

You didn't say what your understanding of this is. You only posted it. What is your level of programming skill? Do you not understand the code itself, or instead, what it performs?

This kind of presentation makes it hard to help you because we can't reach into your mind and see what is missing there.

Here is the link to the product that this code goes with:

I consider myself to be an advanced beginner coder. I am just having difficulty understanding the serial data handling.

It fills an input buffer. Search the introductory threads for the Serial Input Basics tutorial, it will help you understand that part.

Shame the product is apparently undocumented... you didn't say but I'm assuming since I asked...

where did you get the Android app?

True, very little to no documentation. Also, what little documentation there is is in Chinese. :frowning:

Will do. Thanks for the input!

An overview, this system sends all the control data in individual packets. When a full packet is received, it's sorted out in the 'if' statements and acted on. The general approach of the code is to fill a buffer with incoming bytes until a complete packet is detected. It will process it, then sit and wait for another packet.

It recognizes a packet by a start and end marker, the hex codes here:

            if (RX_package[0] == 0xA5 && RX_package[10] == 0x5A)     // 

A really friendly program would call those out like

const byte rx_start_marker = 0xA5;
const byte rx_end_marker = 0x5A;
...
  if (RX_package[0] == rx_start_marker && RX_package[10] == rx_end_marker)     // 

I have a feeling the control flow in this code is excessively convoluted. If you just want to understand, the tutorial will be 10x more helpful.

It did seem overly complex for a simple task.
Thanks for the comments.

I can't agree. It's not the complexity that I object to, actually the implementation is appropriate and fairly efficient. What I don't like, is that it is obfuscated and difficult to follow.

Sending all the information in packets is a rather good idea, it's efficient and easily scalable. Once the packet communication is settled, changes to the payload are independent of that, and so it's easy to add new data items. It can also be easily made more reliable as desired, since the packets can contain error checking.

Encoding the data in binary instead of ASCII, is more efficient because it uses fewer bits for the same data transmission.

The serial code can't be made simpler without sacrificing performance, it uses a fairly standard technique to read the serial port without pausing and freezing the rest of the program. Just, this implementation is not very straightforward.

You Can Use Chat GPT that will help You.

Welcome to the forum. Do you have any advice that will actually help the original poster? If you read the thread, it will become obvious to you that an AI can't help.