1.2.56. fejezet, Forgó jeladó (rotary encoder)
Beküldte pzoli - 2018, augusztus 12 - 2:01du
Kapcsolási rajz
int encoderPin1 = 2; int encoderPin2 = 3; int encoderSwitchPin = 4; //push button switch volatile int lastEncoded = 0; volatile long encoderValue = 0; long lastencoderValue = 0; int lastMSB = 0; int lastLSB = 0; void setup() { Serial.begin (9600); pinMode(encoderPin1, INPUT); pinMode(encoderPin2, INPUT); pinMode(encoderSwitchPin, INPUT); digitalWrite(encoderPin1, HIGH); //turn pullup resistor on digitalWrite(encoderPin2, HIGH); //turn pullup resistor on digitalWrite(encoderSwitchPin, HIGH); //turn pullup resistor on //call updateEncoder() when any high/low changed seen //on interrupt 0 (pin 2), or interrupt 1 (pin 3) attachInterrupt(0, updateEncoder, CHANGE); attachInterrupt(1, updateEncoder, CHANGE); } void loop(){ //Do stuff here if(digitalRead(encoderSwitchPin)){ }else{ Serial.println("Pushed"); } Serial.println(encoderValue); delay(1000); //just here to slow down the output, and show it will work even during a delay } void updateEncoder(){ int MSB = digitalRead(encoderPin1); //MSB = most significant bit int LSB = digitalRead(encoderPin2); //LSB = least significant bit int encoded = (MSB << 1) |LSB; int sum = (lastEncoded << 2) | encoded; if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++; if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --; lastEncoded = encoded; }
- A hozzászóláshoz be kell jelentkezni