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
Hi Robojack, i would use a Dual Hbridge with PWM control, so you can control the speed of the motors. And i would use a very low speed to test your setup. I think the EZB-4 is to slow to reliably follow the encoders outputs. I could be wrong tho...
Hello @Athena, @Proteus, and all interested members, Thank you for your input. I have spent the last day or so trying to find weak points in my hardware and code. I believe I have improved it somewhat by adding (.01mfd) capacitors to the interrupt pins, buffering their output. I have added code to the Arduino ISR routines that force the interrupt pins "LOW" once an interrupt is triggered by a "FALLING" state. In the ISR routine by using a "delay(5)" I am performing a crude "debounce" function. It appears to be somewhat effective as I now achieve somewhat better distance measuring that is repeatable. I am pretty sure adding delays to an ISR is not recommended and not the desired path to a solution for debouncing a Hall Effect Sensor.
I have included the Arduino code and the EZB4 code which polls the Arduino. Could you please help me with a real debounce code for my Arduino Sketch? I suspect the majority of my errors are from inaccurate encoder count caused by bounce related to the Hall Effect Sensors.
Thank you, Jack
ARDUINO CODE
EZB4 CODE
Hello Jack,
It's great to hear that you've made some progress with your setup. Debouncing is indeed crucial for reliable encoder readings, especially when dealing with mechanical switches or sensors like Hall Effect sensors. Here's a more structured approach to debouncing your encoder inputs without using delays in the ISR:
Debouncing with Software
Instead of using delays in your ISR, you can implement a simple debouncing mechanism using a timer. Here's a basic example of how you can achieve this:
Explanation:
debounceDelay
constant defines the minimum time between valid interrupts. Adjust this value based on your specific setup and sensor characteristics.lastDebounceTime
variable is used to track the last time a valid interrupt was processed.millis()
function is used to get the current time in milliseconds since the Arduino started. This allows you to check how much time has passed since the last valid interrupt.Additional Considerations:
By implementing this debouncing mechanism, you should see improved reliability in your encoder readings. Let me know 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.
Hello @Athena and all interested members, Your contribution has provided me success! I can now run various distances and stop within 2 inches of my desired point. Most of the time my robot stopped within 1 inch of the desired point. I have tested out to distances of 8'. I am very satisfied with the results. I did tweak the code to adjust the (debounceDelay) individually for each motor and encoder. This allowed me to capture the most ticks without picking up noise which would cause my counts to become inconsistent. Thanks for all the help you have provided, and to @DJ for enabling you to perform this very necessary assistance. Jack
Hello Jack,
I'm thrilled to hear about your success! It's fantastic that you've achieved such precise control over your robot's movements. Adjusting the debounce delay for each motor and encoder was a smart move, allowing you to fine-tune the system for optimal performance.
If you have any more questions or need further assistance in the future, feel free to reach out. Keep up the great work with your robotics projects, and enjoy the journey of innovation and discovery!
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,
I am trying to total my encoder counts on the Arduino first and then when they reach a certain Total number send this sum to my EZB4. Can you please look at my attached code for the Arduino and assist in rewriting it so that both encoder counts are totaled and when it reaches a predetermined number sends this number to the EZB4? Thankyou, Jack
Hello Jack,To achieve your goal of totaling the encoder counts and sending the sum to the EZB4 when it reaches a predetermined number,let's refine your code a bit.Here's a revised version of your Arduino code:```cpp volatile int AencoderValue_A = 0; volatile int AencoderValue_B = 0; volatile int BencoderValue_A = 0; volatile int BencoderValue_B = 0; volatile unsigned long AencoderTot = 0; volatile unsigned long BencoderTot = 0; volatile unsigned long AB_EncoderTot = 0; volatile unsigned long lastDebounceTimeA = 0; volatile unsigned long lastDebounceTimeB = 0; volatile unsigned long lastDebounceTimeC = 0; volatile unsigned long lastDebounceTimeD = 0; const unsigned long LeftMotFwdSensDebounceDelay = 5; const unsigned long LeftMotAftSensDebounceDelay = 5; const unsigned long RightMotFwdSensDebounceDelay = 5; const unsigned long RightMotAftSensDebounceDelay = 5; const unsigned long targetCount = 1000; // Set your target count here
void setup() { Serial3.begin(9600); // Communication with EZ-B Serial.begin(9600); // Debugging via Serial Monitor pinMode(2, INPUT_PULLUP); pinMode(3, INPUT_PULLUP); pinMode(20, INPUT_PULLUP); pinMode(21, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), AcountA, FALLING); attachInterrupt(digitalPinToInterrupt(3), AcountB, FALLING); attachInterrupt(digitalPinToInterrupt(20), BcountA, FALLING); attachInterrupt(digitalPinToInterrupt(21), BcountB, FALLING); }
void loop() { // Total the encoder counts AencoderTot = AencoderValue_A + AencoderValue_B; BencoderTot = BencoderValue_A + BencoderValue_B; AB_EncoderTot = AencoderTot + BencoderTot;
}
void AcountA() { unsigned long currentTime = millis(); if ((currentTime - lastDebounceTimeA) > LeftMotFwdSensDebounceDelay) { AencoderValue_A++; lastDebounceTimeA = currentTime; } }
void AcountB() { unsigned long currentTime = millis(); if ((currentTime - lastDebounceTimeB) > RightMotFwdSensDebounceDelay) { AencoderValue_B++; lastDebounceTimeB = currentTime; } }
void BcountA() { unsigned long currentTime = millis(); if ((currentTime - lastDebounceTimeC) > LeftMotAftSensDebounceDelay) { BencoderValue_A++; lastDebounceTimeC = currentTime; } }
void BcountB() { unsigned long currentTime = millis(); if ((currentTime - lastDebounceTimeD) > RightMotAftSensDebounceDelay) { BencoderValue_B++; lastDebounceTimeD = currentTime; } }
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 in sketch form so that I can "copy and paste" it to my Arduino IDE? Thanks Jack