Asked

Hello @Athena,
I would like some assistance with sending a floating-point value via UART from an EZ-B v4 using JavaScript to an Arduino Mega using C++. Here's a snippet of my current code:
EZ-B v4 JavaScript Code:
UART.initHardwareUart(0, 9600);
var actualCourse = 91.55;
// Need an appropriate method to send the floating-point value correctly
UART.hardwareUartWrite(0, [actualCourse]);
Arduino Mega C++ Code:
float actualCourse;
void setup() {
resetAll();
Serial3.begin(9600); // Communication with EZ-B
Serial.begin(9600); // Debugging via Serial Monitor
}
void loop() {
if (Serial3.available() >= 1) {
byte incomingByte = Serial3.read();
delay(100);
if (incomingByte == 'A') { // Confirm header byte
Serial.print("incomingByte: ");
Serial.println('A');
actualCourse = Serial3.read(); // This approach doesn't handle float correctly
Serial.print("actualCourse: ");
Serial.println(actualCourse);
}
}
}
My objective is to transmit the entire floating-point value of "actualCourse" (91.55) via UART without any loss of precision or rounding issues. Could you please advise on how to achieve accurate floating-point transmission between the EZ-B and Arduino?
Thank you,
Jack
Related Hardware Roli Rover
Related Control
Serial Toolbox
Hello @Athena, I have confirmed that the above UART code works well on stand-alone scripts. I have incorporated the JavaScript into my existing ARC script, and it performs great. The problem I am having is incorporating the C++ into my existing Arduino script. I have included a snippet below and have Commented out the part of my existing code that prevents receiving UART data. I have rearranged various statements and cannot seem to get it to work. Could you please review the following C++ and incorporate the Commented-Out section so that it will run properly? Thanks. Jack
Below is an example of how you can integrate the UART floatreceiving code into your existing Arduino code while checking for an incoming stop command. In this example, we assume that your wheels’ positions are handled in a loop that waits until a specific condition is met (here, when the sum of LeftFrontWheel and RightFrontWheel positions equals 5). Inside this loop we:
Below is the integrated code snippet:
Explanation
UART Data Check & Header:
The code only proceeds if at least five bytes are available on Serial3. It reads the header and confirms the value is 65.
Stop Command Check:
In the inner while loop (waiting for four data bytes), the program peeks for a pending byte. If it sees the stop command (here defined as
'x'
), it consumes that byte, prints a message, calls stopMotors(), and uses a goto statement to exit this section (labelled Main). Adjust the stop command as needed.Rebuilding the Float Value:
The four bytes are read into individual variables (b0, b1, b2, b3) and then reassembled using bit shifting. Finally, the scaled integer is converted back to a float by dividing by 100.0.
Feel free to adjust the condition or stop command according to the specific needs of your project. If you have further questions or need more adjustments, just let me know!
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Hello @Athena, Thank you for your help. I have been able to implement your suggested code from the previous post. I am having trouble with performing math using the UART data sent to the Arduino. I need to do the following: diffCourse = (actualCourse - desiredCourse). All are float values and diffCourse needs to display a negative or positive number.
Can you help with C++ code that will allow diffCourse to display positive or negative values? Thanks. Jack
You are defining your diffCourse twice....
But also, don't forget how floats work. In your example there will not be any decimal digit so without knowing why you don't think it works, there's nothing wrong with what you wrote - other than defining a variable twice.. once public and once in a private scope.
Below is a complete C++ example that shows how to compute the difference between your actual and desired course values and then print the result with two decimal places. Make sure you don’t redeclare diffCourse if it’s already declared at a global scope. In this example, diffCourse is declared locally inside loop() only once:
Explanation
Variable Declaration:
Both
actualCourse
anddesiredCourse
are defined as global (or they could be local, if you prefer) but, importantly,diffCourse
is defined only once inside the loop. This ensures that you are not redefining the variable twice.Arithmetic Operation:
The expression
correctly computes the difference. Since both operands are floats, the result will also be a float that can be positive or negative.
Displaying the Result:
The function
Serial.println(diffCourse, 2);
prints the float value with two decimal places. You can adjust the number of decimal places by changing the second argument.Avoiding Variable Redeclaration:
If you already have a global or class-level declaration of
diffCourse
, remove the local declaration within the loop to avoid accidental redefinition.Feel free to adjust the values or modify the code as needed for your application. If you have any more questions or need further clarifications, let me know!
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Hello @Athena, Thank you for your help and DJ as well. I found another "local float duplicate" that was preventing proper execution. I am so grateful for your help. This resource is absolutely wonderful! I am working on sending more UART the other direction as well as refining the "map" function for my motors, but I'll open up a new post for that. Again, thank you. Jack.
@Athena Solved!
That's great news, Jack! I'm really happy to hear you solved the issue. It's always rewarding when things start working correctly, and it sounds like you have some exciting projects ahead with the UART data and motor mapping.
If you run into any more questions-whether it's about sending more complex UART data or refining your motor control algorithms-please don't hesitate to reach out.
Keep up the great work, and happy building!
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.