Workinonit
Member
I am attempting to implement a rotary encoder the way that I have done before with an Arduino many times without any luck. It only ever reads nothing or just down... I am using another library that is very interrupt inclusive and I cannot use interrupts, so trying to debounce without interrupts... I have changed the counter up and down but not had any luck. This runs perfectly acceptable on a Arduino Pro328. Any help is much appreciated... Code follows...
As always...
Workinonit!
un
As always...
Workinonit!
un
Code:
signed long delayt = 1000;
static uint8_t counter = 0; //this variable will be changed by encoder input
static uint8_t tmpcounter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
//encoder definitions
#define ENC_E 6 // pin 6
#define ENC_F 7 // pin 7
#define ENC_PORTB PIND
void setup() {
Serial.begin(115200);
pinMode(ENC_E, INPUT);
digitalWrite(ENC_E, HIGH);
pinMode(ENC_F, INPUT);
digitalWrite(ENC_F, HIGH);
}
void loop() {
tmpdata = read_encoder();
if( tmpdata ) {
if(tmpcounter%4==0) {
Serial.print("rotary = ");
Serial.print(tmpdata, DEC);
Serial.print(" - ");
delayt+=tmpdata*50;
}
Serial.println(delayt,DEC);
tmpcounter += tmpdata;
}
}
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
uint8_t old_AB2;
uint8_t old_AB3;
/**/
old_AB2 = old_AB;
old_AB2 = old_AB2 & 0xc0;
old_AB = 0x00;
old_AB |= ( ENC_PORTB & 0xc0 ); //add current state
old_AB3=old_AB;
old_AB3>>=2;
old_AB3=old_AB3 | old_AB2;
old_AB3>>=4;
return ( enc_states[( old_AB3 & 0x0f )]);
}