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 Jack!To achieve the functionality you described,where you sum the encoder values and accumulate them in
$EncoderTot
until it reaches 1500, you can modify your EZ-B v4 script as follows:Key Points:
$EncoderTot
.$EncoderTot
reaches or exceeds 1500, the script sends the reset command (r
) to the Arduino and stops the motors.This setup should help you achieve the desired functionality. If you have any further questions or need additional assistance, feel free to ask!
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.
@athena setvar() and getvar() require the variable name parameter to be a string in quotes.
Thank you for pointing that out, DJ! You're absolutely right. When using
setVar()
andgetVar()
in JavaScript or Python within ARC, the variable name should indeed be passed as a string in quotes. Let's correct the script accordingly:Key Points:
setVar()
andgetVar()
is now correctly passed as a string in quotes.If you have any further questions or need additional assistance, feel free to ask!
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 would like to thank you again for your expert assistance. Both my Arduino and EZB4 scripts are running great with no additional issues relating to encoder counts. I'm very happy with what you have done for me. Thanks, Jack
Hello Jack,
I'm thrilled to hear that everything is working smoothly with your Arduino and EZ-B v4 scripts! It's great to know that the encoder counts are functioning as expected. If you have any more questions or need further assistance in the future, don't hesitate to reach out. Happy robotics, and enjoy your project!
Best regards, Athena
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 and any Community members interested:
I am disappointed in the results I'm getting for determining distance traveled across the floor by counting motor ticks from the drive motor encoders. I am rarely able to get within 6 inches of a desired point when traveling straight ahead 60 inches. I would be very interested in member experiences or recommendations for determining distance traveled. Reliable and repeatable results are what I desire.
About my project. It is a tracked vehicle, (similar to ROLI) powered by two common 9vdc electric geared motors which contain two built in encoders each. I am driving the motors with a boost converter that reliably provides 12vdc to a motor controller providing 9.3v to the motors. A lithium battery of 7.6vdc powers EZB4, one servo and the boost converter. The lithium battery allows for about 8hrs of continuous usage.
An Arduino Mega is dedicated solely to monitoring the Encoders. I have a script running on the EZB4 that polls the encoder totals. My objective is simply to total the encoder counts and stop the robot at a predetermined distance of motor encoder counts. I must obtain repeatable results varying no more than a couple of inches in a 60-inch distance traveled. I am not trying to count track revolution or speed over the ground, only to reliably count the ticks on the encoders and stop robot at a predetermined distance of 60 inches.
Track slippage is minimal as most of the robot travel is over carpet and travel speed is very slow. I have included the Arduino Script below that I am using to monitor the Encoders. Please look it over. I have settled on reading Sensor #1 and Sensor #2 on each motor. I am using a strategy of "detaching" the two sensors that I am polling and "attaching" the other two sensors to continue the count. After polling the two sensors, I zero them out and "re-attach" them while performing the same sequence on the other two sensors.
Thank you for your input, Jack
Hello Jack,
I understand your concern about achieving reliable and repeatable results for distance measurement using motor encoders. Here are a few suggestions and considerations that might help improve the accuracy of your distance measurements:
1. Encoder Resolution and Calibration:
2. Mechanical Considerations:
3. Software Improvements:
4. Feedback and Control:
5. Alternative Methods:
Code Review:
Your code seems to be structured well for counting encoder ticks. However, here are a few points to consider:
volatile long
for encoder values if you expect the counts to exceed the range of anint
.If you continue to experience issues, you might want to experiment with some of these suggestions to see if they improve the accuracy and repeatability of your distance measurements. Feel free to reach out if you have any further questions or need additional assistance!
Best regards, Athena
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.
Deleted ...... Sorry