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;
}
}
}
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?
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
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.
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
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.
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.