Asked
— Edited

Hello @Athena,
Could you please review the following code that I am using on my Arduino Mega 2560 to count motor pulses? I am encountering an issue with the output on the Serial Monitor.
int encoderValue_A = 0;
int encoderValue_B = 0;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(3, INPUT);
attachInterrupt(digitalPinToInterrupt(2), countA, RISING);
attachInterrupt(digitalPinToInterrupt(3), countB, RISING);
}
void loop() {
// Check if there is data available to read
if (Serial.available() > 0) {
// Read the incoming data from the EZ-B
int incomingByte = Serial.read();
Serial.print("Received: ");
Serial.println((char)incomingByte);
// Command 'a' indicates transmitting the current values and resetting the counters
if (incomingByte == 'a') {
delay(50);
// Send the encoder values
Serial.write(encoderValue_A);
Serial.write(encoderValue_B);
// Print the encoder values to the Serial Monitor
Serial.print("Encoder A: ");
Serial.println(encoderValue_A);
Serial.print("Encoder B: ");
Serial.println(encoderValue_B);
// Reset the encoder values
encoderValue_A = 0;
encoderValue_B = 0;
}
}
}
void countA() {
encoderValue_A++;
}
void countB() {
encoderValue_B++;
}
Here is the output that appears on the Serial Monitor:
Received: a
XXEncoder A: 0
Encoder B: 0
The "XX" in front of "Encoder A" are displayed as square boxes, almost like placeholders. Could you help me understand what these characters are and how I can remove them?
Thank you,
Jack
Related Hardware Arduino Due/Mega
Related Control
Wheel Encoder Counter
Hello @Athena,
I have incorporated the recommended print statements, for high byte and low byte. It prints the following:
EZB4 Sending pathTotalDistance high byte: 9 Sending pathTotalDistance low byte: 136 Sending pathEndpointDistance high byte: 7 Sending pathEndpointDistance low byte: 108 Sending oppositeDistance high byte: 1 Sending oppositeDistance low byte: 16
EZB4 Code:
Based upon the Values ( pathTotalDistance: 2440, pathEndpointDistance:1900, oppositeDistance:272) sent from my EZB4 code, are the high bytes and low bytes accurate?
Thank you, Jack
Let's verify the high and low byte calculations for the values you're sending from the EZB4 code.
Calculations for High and Low Bytes
pathTotalDistance = 2440
highByte = (pathTotalDistance >> 8) & 0xFF
highByte = (2440 >> 8) & 0xFF
highByte = 9
lowByte = pathTotalDistance & 0xFF
lowByte = 2440 & 0xFF
lowByte = 136
pathEndpointDistance = 1900
highByte = (pathEndpointDistance >> 8) & 0xFF
highByte = (1900 >> 8) & 0xFF
highByte = 7
lowByte = pathEndpointDistance & 0xFF
lowByte = 1900 & 0xFF
lowByte = 108
oppositeDistance = 272
highByte = (oppositeDistance >> 8) & 0xFF
highByte = (272 >> 8) & 0xFF
highByte = 1
lowByte = oppositeDistance & 0xFF
lowByte = 272 & 0xFF
lowByte = 16
Conclusion
The high and low byte calculations in your EZB4 code are accurate based on the values you provided. The print statements confirm that the bytes are being calculated correctly:
These values match the expected results for the given inputs. If you continue to experience issues, ensure that the Arduino is correctly reading and combining these bytes in the same order they are sent. Let me know if you need further assistance!
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.
Thank you @Athena,
The following values are printing from the received data on the Arduino:
Received pathTotalDistance high byte: 7. low byte: 6C. Received pathEndpointDistance high byte: 5A. low byte: 5A.
Received oppositeDistance high byte: FFFFFFFF. low byte: FFFFFFFF.
ARDUINO Code:
volatile int AencoderValue_A = 0; volatile int AencoderValue_B = 0; volatile unsigned long AB_EncoderTot = 0; volatile unsigned long lastDebounceTimeA = 0; volatile unsigned long lastDebounceTimeB = 0; const unsigned long LeftMotFwdSensDebounceDelay = 5; const unsigned long RightMotFwdSensDebounceDelay = 7; //The following are now refered to as "FoundationVariables" long adjacentRemainingDistance; long pathTotalDistance; long pathEndpointDistance = 100; float oppositeDistance; float desiredCourse = 90; long actualCourse = 90;
void setup() { Serial3.begin(9600); // Communication with EZ-B Serial.begin(9600); // Debugging via Serial Monitor pinMode(2, INPUT_PULLUP); pinMode(3, INPUT_PULLUP); setMotorDirectionForward(); attachInterrupt(digitalPinToInterrupt(2), AcountA, RISING); attachInterrupt(digitalPinToInterrupt(3), AcountB, RISING); }
void loop() { if (Serial3.available() >= 8) { // Wait for 8 bytes resetFoundationVariables(); delay(10); resetAll(); delay(10); pathTotalDistance = (Serial3.read() << 8) | Serial3.read(); // Combine two bytes Serial.print("Received pathTotalDistance high byte: ");Serial.println(Serial3.read(), HEX); Serial.print("Received pathTotalDistance low byte: ");Serial.println(Serial3.read(), HEX);
The Values being sent by the EZB4 are ( pathTotalDistance: 2440, pathEndpointDistance:1900, oppositeDistance:272). Are the received high bytes and low bytes correct?
Thanks Jack
It seems there might be an issue with how the bytes are being read and combined on the Arduino side.Lets break down the process to ensure the bytes are being handled correctly.### Arduino Code Review 1.Reading Bytes: - You are reading bytes from
Serial3
and combining them using bitwise operations. - Ensure that you are reading the bytes in the correct order and combining them properly. 2.Printing Bytes: - You are printing the bytes immediately after reading them, which might not reflect the correct order if theSerial3.read()
calls are not managed properly. ### Correcting the Arduino Code 1.Store Bytes in Variables: - Store the high and low bytes in variables before combining them. 2.Print Stored Bytes: - Print the stored bytes to ensure they are being read correctly. 3.Combine Bytes Correctly: - Combine the bytes using bitwise operations after storing them. Here is a revised version of your Arduino code for reading and printing the bytes:cpp void loop() { if (Serial3.available() >= 8) { // Wait for 8 bytes resetFoundationVariables(); delay(10); resetAll(); delay(10); // Read and store bytes for pathTotalDistance byte highBytePTD = Serial3.read(); byte lowBytePTD = Serial3.read(); pathTotalDistance = (highBytePTD << 8) | lowBytePTD; Serial.print("Received pathTotalDistance high byte: "); Serial.println(highBytePTD, HEX); Serial.print("Received pathTotalDistance low byte: "); Serial.println(lowBytePTD, HEX); // Read and store bytes for pathEndpointDistance byte highBytePED = Serial3.read(); byte lowBytePED = Serial3.read(); pathEndpointDistance = (highBytePED << 8) | lowBytePED; Serial.print("Received pathEndpointDistance high byte: "); Serial.println(highBytePED, HEX); Serial.print("Received pathEndpointDistance low byte: "); Serial.println(lowBytePED, HEX); // Read and store bytes for oppositeDistance byte highByteOD = Serial3.read(); byte lowByteOD = Serial3.read(); oppositeDistance = (highByteOD << 8) | lowByteOD; Serial.print("Received oppositeDistance high byte: "); Serial.println(highByteOD, HEX); Serial.print("Received oppositeDistance low byte: "); Serial.println(lowByteOD, HEX); } }
### Explanation - Store Bytes: The high and low bytes are stored in variables before being combined. - Print Stored Bytes: The stored bytes are printed to ensure they are read correctly. - Combine Bytes: The bytes are combined using bitwise operations after storing them. ### Expected Output - pathTotalDistance: High byte =0x09
, Low byte =0x88
- pathEndpointDistance: High byte =0x07
, Low byte =0x6C
- oppositeDistance: High byte =0x01
, Low byte =0x10
By following this approach, you should be able to correctly read and combine the bytes on the Arduino side. Let me know if you need further assistance!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, Could you please resend the recommended Arduino code from the last post? It came through in paragraph form and I can't understand it. Please send in a format that I can copy and paste to my Arduino IDE. Thanks Jack
Certainly! Here's the revised Arduino code in a format suitable for copying and pasting into your Arduino IDE:
Explanation
This code should help you correctly read and combine the bytes on the Arduino side. Let me know if you need further assistance!
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 continued support. I am fairly confident that the EZB4 is reliably sending the bytes for the 3Parameters in question. (pathTotalDistance, pathEndpointDistance, and oppositeDistance). The print for each run of the EZB4 script prints the same characters.
EZB
However, the received bytes in the Arduino for the 3Parameters are correct only on the initial run. Subsequent runs produce faulty bytes. It seems as if the high/low bytes all slip down to the next variable.
Initial Run: ARDUINO
Subsequent Runs: ARDUINO
It's as if all the high/low bytes slip down to the next variable. My question is what could be causing this mix up of the bytes? Should a SerialRead Buffer be cleared each time?
Thank you. I have included my Arduino sketch below. Jack
The issue you're experiencing with the bytes slipping down to the next variable could be due to a few potential causes. Let's address these and provide a solution to ensure the bytes are read correctly each time.
Potential Causes and Solutions
Serial Buffer Not Being Cleared:
Incorrect Byte Handling:
Synchronization Issue:
Revised Arduino Code
Here's a revised version of your Arduino code with improvements to handle the serial data correctly:
Key Changes
This should help resolve the issue with the bytes slipping to the next variable. Let me know if you need further assistance!
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.