Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Modified Rotary Encoder sketch
- * Originally by Nick Gammon, as of 25th May 2011
- * Modified by Nanne Kuperus, as of 09 October 2018
- *
- * TODO: Fix rotary encoder dual stepping (one turn increments / decrements count by two steps),
- * add reset button and add pretty RGB lighting on rotary encoder.
- */
- #define pinA 2
- #define pinB 3
- #define interrupt0 0 // that is pin D2
- // #define arduinoPullups // Uncomment this to use Arduino (software) pullups, if you do not use hardware pullups.
- volatile boolean fired; // Ensure that any variables for use inside the ISR are set as volatile, otherwise you cannot manipulate them.
- volatile boolean up;
- int count;
- void isr() // Interrupt Service Routine for a change to pinA on interrupt pin 0 (D2)
- {
- if (digitalRead(pinA))
- {
- up = digitalRead(pinB);
- }
- else
- {
- up = !digitalRead(pinB);
- }
- fired = true;
- }
- // ISR END
- void setup()
- {
- Serial.begin(9600);
- #ifdef arduinoPullups
- Serial.print("Arduino pullups will be enabled!");
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, HIGH);
- #endif
- pinMode(pinA, INPUT);
- pinMode(pinB, INPUT);
- attachInterrupt(interrupt0, isr, CHANGE); //interrupt0 is pin D2, interrupt 1 is pin D3
- }
- void loop()
- {
- /*
- * The original sketch used a static long to redefine int over and over again, I have instead defined it
- * with the other variable definitions and as such it need not be a static variable (it can be).
- */
- // static long count = 0;
- if(fired)
- {
- if (up)
- {
- count++;
- }
- else
- {
- count--;
- }
- fired = false;
- Serial.print("Count: ");
- Serial.println(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement