Hello,
For my project i use a rotary encoder. I've try to make a small library for this in order to get the number of rotation perfomed since last call.
But for obscure reason sometimes the function GetEncoderCounterSinceLastCall()
return -256 only when i turn the rotaryEncoder counterClockwise
I've attempted to put some log to find the cuplrit whithout much success log randomly goes like this:
Update: -1
Get: -1
Loop: -256
while i attempt to be in all case
Update: -1
Get: -1
Loop: -1
I can't find the culprit and i see nothing wrong here. Any help would be greatly appreciated
Here is my code :
Main :
#include "RotaryEncoder/RotaryEncoder.h"
#define pinA 2 // CLK Output A Do not use other pin for clock as we are using interrupt
#define pinB 3 // DT Output B
volatile unsigned int encoder0Pos = 0;
volatile unsigned int oldencoder0Pos = 0;
long unsigned tempsA;
int aLastState;
RotaryEncoder rotaryEncoder(pinA, pinB);
void setup()
{
Serial.begin(115200);
rotaryEncoder._streamRef = &Serial;
}
void loop()
{
short rotaryValue = rotaryEncoder.GetEncoderCounterSinceLastCall();
if (rotaryValue != 0)
{
Serial.print("Loop: "); Serial.println(rotaryValue);
}
}
RotaryEncoder.h
#ifndef RotaryEncoder_h
#define RotaryEncoder_h
#include "Arduino.h"
class RotaryEncoder
{
public:
RotaryEncoder(int aPin, int bPin);
void UpdateEncoder();
short GetEncoderCounterSinceLastCall();
static RotaryEncoder *sEncoder;
Stream *_streamRef;
private:
int _aPin;
int _bPin;
volatile short _counter;
long unsigned _aTemps;
int aLastState;
static void updateEncoderISR();
};
#include "RotaryEncoder.cpp"
#endif
RotartyEncoder.cpp
#include "Arduino.h"
#include "RotaryEncoder.h"
RotaryEncoder *RotaryEncoder::sEncoder = 0;
RotaryEncoder::RotaryEncoder(int aPin, int bPin)
{
_aPin = aPin;
_bPin = bPin;
sEncoder = this;
pinMode(_aPin, INPUT_PULLUP);
pinMode(_bPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(_aPin), RotaryEncoder::updateEncoderISR, CHANGE);
_aTemps = millis();
}
// Static function
void RotaryEncoder::updateEncoderISR()
{
if (sEncoder != 0)
sEncoder->UpdateEncoder();
}
void RotaryEncoder::UpdateEncoder()
{
int acurrentState = digitalRead(_aPin);
int bcurrentState = digitalRead(_bPin);
if (abs(millis() - _aTemps) > 10 && aLastState != acurrentState)
{
if (bcurrentState == HIGH && acurrentState == LOW)
{
--_counter;
_streamRef->print("Update: ");
_streamRef->println(_counter);
}
else if (bcurrentState == LOW && acurrentState == LOW)
{
++_counter;
}
_aTemps = millis();
}
aLastState = acurrentState;
}
short RotaryEncoder::GetEncoderCounterSinceLastCall()
{
short current = _counter;
if (_counter != 0)
{
_streamRef->print("Get: ");
_streamRef->println(_counter);
_counter = 0;
}
return current;
}
Laser.ino (605 Bytes)
RotaryEncoder.cpp (1.3 KB)
RotaryEncoder.h (461 Bytes)