Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void setup() {
- //Use this setup code for 12 ticks/revolution
- attachInterrupt(digitalPinToInterrupt(rotaryPinA), encode, FALLING);
- /*
- Use this setup code for 24 ticks/revolution
- (trigger interrupt on rising and falling edge)
- attachInterrupt(digitalPinToInterrupt(rotaryPinA), encode, CHANGE);
- */
- /*
- Use this setup code for 48 ticks/revolution (trigger interrupts on rising and falling edge of either pin)
- attachInterrupt(digitalPinToInterrupt(rotaryPinA), encode, CHANGE);
- attachInterrupt(digitalPinToInterrupt(rotaryPinB), encodePinB, CHANGE);
- */
- //The rest of your setup code...
- }
- void encode() {
- rotaryValueChanged = true;
- //Read the PinA & PinB (Digital Pins 2 & 3) using port register PINE, 4th and 5th bit
- //Fast equivalent to pinAState = digitalRead(rotaryPinA) == LOW
- bool pinAState = (PINE & (1 << 4)) == 0;
- bool pinBState = (PINE & (1 << 5)) == 0;
- rotaryValue += (pinAState == pinBState) ? -1 : 1;
- }
- void encodePinB() {
- rotaryValueChanged = true;
- //Read the PinA & PinB (Digital Pins 2 & 3) using port register PINE, 4th and 5th bit
- bool pinAState = (PINE & (1 << 4)) == 0;
- bool pinBState = (PINE & (1 << 5)) == 0;
- rotaryValue += (pinAState != pinBState) ? -1 : 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement