Summary
I have a low-resolution 10 P/R rotary encoder that I'm trying to collect data from using my Arduino Elegoo Mega 2560. See this image and the link below for more info on the encoder:
I attached a blue longboard wheel to the encoder so it's easy for me to rotate its shaft.
What Is Working Right Now
For the most part, the Arduino is collecting the data correctly. When I spin the longboard wheel slowly, the data in the Serial Monitor seems to make sense. View this photo to see the working Serial Monitor data. As expected, when I rotate the wheel a little bit, the serial monitor outputs "HIGH" and when I rotate it a little more, the serial monitor outputs "LOW". This image explains what's going on within the rotary encoder itself.
The Problem
The problem is when I start spinning the longboard wheel at faster speeds.
When I do that I see:
- Multiple "HIGH"s in a row in the serial monitor which doesn't make sense to me since the code should always switch from HIGH to LOW to HIGH and so on
- Multiple events data outputs at the EXACT same timestamp which again, makes no sense to me because the status of the encoder should only be reported once per change.
See this image to understand more what I mean
The Code
void setup() {
// Baud rate
Serial.begin (9600);
// Internal pullup input pin 2
pinMode(2, INPUT_PULLUP);
// Set up interrupts
attachInterrupt(0, ai0, RISING);
attachInterrupt(1, ai1, FALLING);
}
void loop()
{
}
// ai0 is activated if DigitalPin 2 is going from LOW to HIGH
void ai0() {
Serial.println("HIGH");
Serial.flush();
}
// ai1 is activated if DigitalPin 2 is going from HIGH to LOW
void ai1() {
Serial.println("LOW");
Serial.flush();
}
What I'm Looking For
I am looking for a solution that allows me to spin my longboard wheel really fast (at any speed) while outputting accurate data onto the serial monitor. To my understanding, and please correct me if I'm wrong, the data should always output "HIGH" then "LOW" repeatedly and if I'm spinning the longboard wheel fast, the timestamps in between each change should be very small but NEVER the exact same.