Asked
Resolved Resolved by Athena AI!

Sending Float Value Via UART

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

ARC Pro

Upgrade to ARC Pro

ARC Pro is your passport to a world of endless possibilities in robot programming, waiting for you to explore.

#9  

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

while ((LeftFrontWheel.currentPosition() + RightFrontWheel.currentPosition()) <= pathEndpointDistance) {

        // Ensure at least 5 bytes are available: 1 header + 4 data bytes
  if (Serial3.available() >= 5) {
    // Read and check the header byte (expected to be 65)
     if (Serial3.read() == 65) {
      // Read each data byte individually (wait for each byte if needed)
      while (Serial3.available() < 4);  // Wait until 4 bytes are available
      
      int32_t b0 = Serial3.read();
      int32_t b1 = Serial3.read();
     int32_t b2 = Serial3.read();
    int32_t b3 = Serial3.read();
      
      // Rebuild the scaled integer inline without a loop
      scaledValue = (b0 & 0xFF) | 
                    ((b1 & 0xFF) << 8) | 
                    ((b2 & 0xFF) << 16) | 
                    ((b3 & 0xFF) << 24);
                    
      // Reverse the scaling to obtain the original float value
      actualCourse = scaledValue / 100.0;
      
      // Print the result for debugging
      Serial.print("actualCourse: ");
      Serial.println(actualCourse);
    }
  } 
  
//if (Serial3.available() > 0) {  // Checking for incoming STOP command due to obstruction detected or inability to track reflector.
        // Read the incoming byte
       // int incomingByte = Serial3.read();
       // Check if the incoming byte is 'x'
       // if (incomingByte == 'x') {
        //  Serial.print("x :   ");
       //Serial.println('x');
        //  stopMotors();
        //  goto Main;
     // } 
//}

}




PRO
Canada
#10  

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:

  1. Check for at least 5 bytes available (one header plus four data bytes).
  2. Read the header byte and verify it is 65 (ASCII A).
  3. While waiting for the four required data bytes, also check if another incoming byte (e.g. the stop command x) is received.
  4. If a stop command is detected, we call stopMotors() and break out (using a goto label in this example for simplicity).
  5. If there is no stop command, we then read the four data bytes and reconstruct the 32bit scaled number without using a for loop.
  6. Finally, we reverse the scaling to recover the original float value.

Below is the integrated code snippet:


// Assume that LeftFrontWheel.currentPosition() and RightFrontWheel.currentPosition()
// are functions that return a current wheel position integer value.
// Also assume stopMotors() is a defined function to stop your motors.

// The stop command is assumed to be the character 'x'.
// "Main" is a label to jump to once we need to break out of this loop.

void loop() {
  // Sample condition: wait while the sum of wheel positions equals 5
  // (Change this condition according to your application)
  while ((LeftFrontWheel.currentPosition() + RightFrontWheel.currentPosition()) == 5) {
  
    // Check if we have at least 5 bytes available:
    // 1 header byte + 4 data bytes.
    if (Serial3.available() >= 5) {
    
      // Read and check the header byte (expected to be 65)
      int headerByte = Serial3.read();
      if (headerByte == 65) {
      
        // Wait here until there are at least 4 data bytes
        // or check if an incoming STOP command arrives.
        while (Serial3.available() < 4) {
          // If any other byte is pending, peek it
          if (Serial3.available() > 0) {
            int incomingByte = Serial3.peek(); // do not remove it yet
            // Check if this byte is our STOP command (assume 'x' is the stop command)
            if (incomingByte == 'x') {
              // Consume the byte and stop motors
              Serial3.read();  
              Serial.print("Received STOP command: ");
              Serial.println(incomingByte);
              stopMotors();
              goto Main; // jump out to the Main section
            }
          }
        }
        
        // Now read the 4 bytes that represent our scaled float value
        int32_t b0 = Serial3.read();
        int32_t b1 = Serial3.read();
        int32_t b2 = Serial3.read();
        int32_t b3 = Serial3.read();
        
        // Rebuild the scaled integer inline (note the shift amounts)
        int32_t scaledValue = (b0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
        
        // Reverse the scaling: if we multiplied by 100 when sending, divide by 100.0
        float actualCourse = scaledValue / 100.0;
        
        // Print the result for debugging
        Serial.print("Received actualCourse: ");
        Serial.println(actualCourse);
      }
      else {
        // If header does not match, you might want to flush the UART buffer or ignore this message.
        Serial.print("Unexpected header: ");
        Serial.println(headerByte);
      }
    }
  }  // end of while

Main:
  // Continue with the rest of your code.
  // ...
}

Explanation

  1. 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.

  2. 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.

  3. 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.
#11  

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.


float desiredCourse = 90.00; //example of number.  It will constantly be computed (+ or -)
float actualCourse = 87.00; //example of number.  It will constantly be computed (+ or -)   
float diffCourse = 00.00;  // can be + or -



void loop() {

float diffCourse =  actualCourse - desiredCourse;  // Determines if error state exists between actualCourse and desiredCourse.

     Serial.print("diffCourse:  ");
     Serial.println(diffCourse,2);

}

Can you help with C++ code that will allow diffCourse to display positive or negative values? Thanks. Jack

PRO
Synthiam
#12  

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.

PRO
Canada
#13  

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:

float desiredCourse = 90.00;  // desired course value
float actualCourse  = 87.00;  // actual course value

void setup() {
  Serial.begin(9600);  // start serial communication for debugging
}

void loop() {
  // Calculate the difference (positive or negative)
  float diffCourse = actualCourse - desiredCourse;
  
  // Print the result with two decimal digits
  Serial.print("diffCourse: ");
  Serial.println(diffCourse, 2);

  // Delay to slow down the output (for demonstration purposes)
  delay(1000);
}

Explanation

  1. Variable Declaration:
    Both actualCourse and desiredCourse 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.

  2. Arithmetic Operation:
    The expression

    float diffCourse = actualCourse - desiredCourse;
    

    correctly computes the difference. Since both operands are floats, the result will also be a float that can be positive or negative.

  3. 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.

  4. 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.
#14   — Edited

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.

PRO
Canada
#16  

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.