Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const unsigned long ROTARY_DEBOUNCE_TIME = 100; // milliseconds
- const byte Encoder_A_Pin = 8; // PB0, pin 12 (TQFP) on board
- const byte Encoder_B_Pin = 9; // PB1, pin 13 (TQFP) on board
- // handle pin change interrupt for D8 to D13 here
- ISR (PCINT0_vect)
- {
- static byte pinA, pinB;
- static boolean ready;
- static unsigned long lastFiredTime;
- byte newPinA = digitalRead (Encoder_A_Pin);
- byte newPinB = digitalRead (Encoder_B_Pin);
- if (pinA == newPinA &&
- pinB == newPinB)
- return; // spurious interrupt
- // so we only record a turn on both the same (HH or LL)
- // Forward is: LH/HH or HL/LL
- // Reverse is: HL/HH or LH/LL
- if (newPinA == newPinB)
- {
- if (ready)
- {
- if (millis () - lastFiredTime >= ROTARY_DEBOUNCE_TIME)
- {
- if (newPinA == HIGH) // must be HH now
- {
- if (pinA == LOW)
- fileNumber ++;
- else
- fileNumber --;
- }
- else
- { // must be LL now
- if (pinA == LOW)
- fileNumber --;
- else
- fileNumber ++;
- }
- if (fileNumber > MAX_FILE_NUMBER)
- fileNumber = 0;
- else if (fileNumber < 0)
- fileNumber = MAX_FILE_NUMBER;
- lastFiredTime = millis ();
- fired = true;
- }
- ready = false;
- } // end of being ready
- } // end of completed click
- else
- ready = true;
- pinA = newPinA;
- pinB = newPinB;
- } // end of PCINT2_vect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement