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!The error message indicates that the UART transmission is not supported on the specified digital port for your EZB4 device.Lets address this issue and ensure the script is correctly configured for your setup.
Updated EZB4 Script
Here's an updated version of your script with some adjustments:
Key Points:
If you continue to experience issues, please verify the EZB4 documentation for supported UART ports and adjust the script accordingly. 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, I am using UART0 on my EZB4 as in my previous scripts. Any other recommendations to get my script running? Thanks Jack
If you're using UART 0, then your UART readstring will be set to 0. Check the manual page for the command here: https://synthiam.com/Support/javascript-api/UART/hardwareUartReadString
and the hardwareUartAvailable manual is here: https://synthiam.com/Support/javascript-api/UART/hardwareUartAvailable
and the sendSerialString needs to specify the UART index as well, which would be 0. manual is here: https://synthiam.com/Support/javascript-api/UART/sendSerialString
Here is your updated script by referencing the manual pages for the commands to use the correct syntax.
Thank you so much for the help. Jack
You're welcome,Jack!I'm glad I could assist you.If you have any more questions or need further help with your project,feel free to ask.Happy building with ARC!
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 am finally taking baby steps towards actually coding my Arduino and EZB4 in their respective programming languages instead of using Blockly. Thanks to you! I have decided to offload all of the motor control functions to the Arduino and leave the EZB4 to do the main NAV functions of my robot. I have attached the Arduino Script and the EZB4 Script. Could you please review my two Scripts and provide me with some feedback? Don't hold back, I want the good and bad. If after you review the scripts, could you send them back to me corrected? This is the way I learn best.
Thank you, Jack
ARDUINO:
EZB4:
Hi Jack! It's great to see you diving into coding for your Arduino and EZB4. Let's review and refine your scripts for both the Arduino and EZB4.
Arduino Script
Variable Initialization: Ensure that your variables are initialized correctly. For instance,
volatile
is used correctly for variables modified within an interrupt.Serial Communication: You're using
Serial3
for communication with the EZ-B, which is correct for the Mega. Ensure that the wiring matches this setup.Debouncing: Your debounce logic seems fine, but ensure that the debounce delay is appropriate for your encoder's speed.
Motor Control Logic: The logic for controlling the motor direction and speed is clear. Ensure that the
analogWrite
values are suitable for your motor driver.Encoder Logic: The logic for counting encoder pulses and checking against the target count is sound. Ensure that the
targetCount
is set appropriately for your application.Code Cleanup: Consider using functions to encapsulate repeated logic, such as resetting variables or setting motor directions.
Here's a refined version of your Arduino script:
EZB4 Script
UART Initialization: Ensure that the UART is initialized correctly and matches the baud rate of the Arduino.
Data Handling: The logic for reading and combining the high and low bytes is correct. Ensure that the data is sent and received in the correct order.
Polling Interval: The polling interval is set to 100ms, which should be fine for most applications.
Here's a refined version of your EZB4 script:
Feedback Summary
Arduino: Consider using functions to encapsulate repeated logic, such as resetting variables or setting motor directions. Ensure that the debounce delay is appropriate for your encoder's speed.
EZB4: Ensure that the UART initialization and data handling logic are correct. The polling interval is set to 100ms, which should be fine for most applications.
Feel free to ask if you have any questions or need further clarification!
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.