Rotary Encoder
Rotary Encoder
Für Arduino
#define BTN_A_PIN 2 #define BTN_B_PIN 3 #define BTN_PUSH_PIN 4 bool Btn_Push_Last;
volatile int8_t AB_Old = 0;
volatile int RotaryPos = 0;
int8_t StepTable[16] = {0, 0,0,0,0,0,0,-1, 0,0,0,0,0,1, 0,0};
void setup(){
pinMode(BTN_A_PIN, INPUT); pinMode(BTN_B_PIN, INPUT);
pinMode(BTN_PUSH_PIN, INPUT);
Btn_Push_Last = digitalRead(BTN_PUSH_PIN);
noInterrupts(); TIMSK1 |= (1<<OCIE1A); TCCR1A = 0; TCCR1B = (1<<WGM12) | (1<<CS12) | (1<<CS10); OCR1A = 14; interrupts();
Serial.begin(250000);
Serial.println("Drehknopf Test");
}
void loop(){
if(digitalRead(BTN_PUSH_PIN) != Btn_Push_Last){
Btn_Push_Last = !Btn_Push_Last;
if(Btn_Push_Last==HIGH) Serial.println("PressUp");
else Serial.println("PressDown");
}
if(RotaryPos!=0){ Serial.println(RotaryPos); RotaryPos = 0; }
}
ISR(TIMER1_COMPA_vect) { AB_Old <<= 2;
AB_Old &= B00001100;
AB_Old |= (digitalRead(BTN_B_PIN) << 1) | digitalRead(BTN_A_PIN);
RotaryPos += StepTable[AB_Old];
}