Hi all,
I am currently using a optical incremental rotary encoder (S5-1024, US Digital) to detect the number of pulses and directionality using an Arduino Uno R3 and interrupts. However I noticed that when I negatively increment my pulse count the value sometimes increases to nonsensically high positive values, before switching back to plausible ones. First I thought it was about the calculations I was doing in my original script, but this also kept happening in the simplified sample below:
/*
Rotary Encoder Test
Read pulses from an optical incremental rotary encoder to calculate rpm (unidirectional rotation only)
Displays results on Serial Monitor
Adapted from https://dronebotworkshop.com/rotary-encoders-arduino/
*/
// Encoder output pulse per rotation (change as required)
#define ENC_COUNT_REV 4096
// Encoder output to Arduino Interrupt pin
#define ENC_IN_A 2
#define ENC_IN_B 3
// Pulse count from encoder
volatile long encoderValue = 0;
// Interval for measurements
int interval = 8;
// Counters for milliseconds during interval
long previousMillis = 0;
long currentMillis = 0;
// Variable for RPM measuerment
int rpm = 0;
void setup()
{
// Setup Serial Monitor
Serial.begin(9600);
// Set encoder as input with internal pullup
pinMode(ENC_IN_A, INPUT_PULLUP);
pinMode(ENC_IN_B, INPUT_PULLUP);
// Attach interrupt
attachInterrupt(digitalPinToInterrupt(ENC_IN_A), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENC_IN_B), updateEncoder, CHANGE);
// Setup initial values for timer
previousMillis = millis();
}
void loop()
{
// Show Pulses at specified frequency, if changes are detected
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (encoderValue != 0) {
Serial.print(" PULSES: ");
Serial.println(encoderValue);
}
encoderValue = 0;
}
}
void updateEncoder()
{
// Increment value for each pulse from encoder
encoderValue--;
}
This sometimes gives outputs like this:
" PULSES: -30
PULSES: -30
PULSES: -30
PULSES: -30
PULSES: 65506
PULSES: -29
PULSES: -30
PULSES: -29
PULSES: -29
PULSES: -29"
When encoderValue--
is changed to encoderValue++
this phenomenon disappears, which led me to the assumption, that there is a problem with handling negative numbers? But somehow I doubt this should be the case. Also the numbers I am dealing with are not very high (counts of up to 400).
Does anybody know what might be the problem here?
Thanks for your help!