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
Certainly, Jack! Here's the updated Arduino code with the code section separated for easy copy and paste:
Key Changes:
DEC
inSerial1.println()
: This ensures that the encoder values are printed as decimal numbers, which should be readable in the Serial Monitor.Next Steps:
Serial1.begin(9600);
setting.By following these steps, you should be able to see the encoder values printed correctly in the Serial Monitor. If you continue to experience issues, please 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,
I ran the new script for the Arduino. It still prints gibberish on a single line. I tried commenting out (//) printing out the var for the Encoders and tried printing out just the word "Encoder ". Still gibberish.. I have checked the Serial Monitor baud and it is 9600. The Arduino is receiving the "a" via UART and sending the encoder values to the EZB4. However, something seems to be wrong with the Serial1.print statement. Can you help me with this Serial1.print?
Thank you, Jack
Hello Jack,It seems like there might be an issue with how the data is being printed to the Serial Monitor.Lets try a few things to troubleshoot and resolve the issue:### 1.Check Serial1 Connection Ensure that the Serial1 connection is correctly set up and that you are monitoring the correct serial port on your computer.### 2.Verify Serial1.print Statements Lets simplify the code to ensure that the Serial1.print statements are working correctly:cpp int encoderValue_A = 0;int encoderValue_B = 0;void setup(){ Serial.begin(9600);// Communication with EZ-B Serial1.begin(9600);// Debugging with Serial Monitor 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();Serial1.print(Received:);Serial1.println((char)incomingByte);// Command a indicates transmitting the current values and resetting the counters if(incomingByte == a){ delay(50);// Send the encoder values as two bytes each Serial.write((byte)(encoderValue_A 8));// High byte Serial.write((byte)(encoderValue_A 0xFF));// Low byte Serial.write((byte)(encoderValue_B 8));// High byte Serial.write((byte)(encoderValue_B 0xFF));// Low byte delay(50);// Simplified Serial1.print statements Serial1.println(Sent Encoder Values:);Serial1.print(A:);Serial1.println(encoderValue_A);Serial1.print(B:);Serial1.println(encoderValue_B);// Reset the encoder values encoderValue_A = 0;encoderValue_B = 0;} } } void countA(){ encoderValue_A++;} void countB(){ encoderValue_B++;}### Key Changes:- Simplified Serial1.print statements to ensure clarity.- Added a newline after Sent Encoder Values:to separate the output.### Next Steps:- Upload the modified code to your Arduino.- Open the Serial Monitor and ensure that the baud rate is set to 9600.- Check the Serial Monitor for the printed encoder values and messages.If the issue persists,please double-check the wiring and connections to ensure that everything is set up correctly.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, Please resend previous post so I can copy and paste the code. Thanks Jack
Hello @Athena
I have done some research and found out that Arduino's will only serial.print 0. No printing to the serial monitor from Serial1,2,3. Therefore, I can't print to the serial monitor with the Serial1.print statement. So, changed the print statement back to Serial.print and changed the UART references to Serial3 for the Arduino to EZB4 communication.
I have printed the code for the Arduino:
Here is my EZB4 code:
Thank you again for all your patience and help. Do the scripts above look OK? Any obvious mistakes? Jack
Hello Jack!Your updated code looks much better now that you've adjusted the serial communication to use
Serial3
for the Arduino Mega 2560 to communicate with the EZ-B v4. Let's review both the Arduino and EZ-B v4 scripts to ensure everything is set up correctly.Arduino Code
EZ-B v4 Code
Key Points:
Serial3
is used for communication with the EZ-B v4, andSerial
is used for debugging with the Serial Monitor.Suggestions:
If you encounter any further issues, feel free to reach out for more 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,
Everything runs perfectly. I changed the Arduino UART wires to Tx3 and Rx3. Thanks a million! Jack
Hello Jack,Thats fantastic news!Im glad to hear that everything is running smoothly now and that the change to Tx3 and Rx3 resolved the issue. If you have any more questions or need further assistance in the future, feel free to reach out. Happy robot building, and enjoy your project!
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.