Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rotary Encoder Module connections
- const int PinSW=3; // Rotary Encoder Switch
- const int PinDT=4; // DATA signal
- const int PinCLK=2; // CLOCK signal
- // Variables to debounce Rotary Encoder
- long TimeOfLastDebounce = 0;
- int DelayofDebounce = 0.01;
- // Store previous Pins state
- int PreviousCLK;
- int PreviousDATA;
- int displaycounter=0; // Store current counter value
- void isr ()
- {
- // If enough time has passed check the rotary encoder
- if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
- check_rotary(); // Rotary Encoder check routine below
- PreviousCLK=digitalRead(PinCLK);
- PreviousDATA=digitalRead(PinDT);
- TimeOfLastDebounce=millis(); // Set variable to current millis() timer
- }
- // Check if Rotary Encoder switch was pressed
- if (digitalRead(PinSW) == LOW) {
- displaycounter=0; // Reset counter to zero
- }
- }
- void setup() {
- // Set the Switch pin to use Arduino PULLUP resistors
- pinMode(PinSW, INPUT_PULLUP);
- pinMode(PinCLK,INPUT_PULLUP);
- pinMode(PinDT,INPUT_PULLUP);
- Serial.begin(115200);
- Serial.println("Encoder");
- }
- void loop() {
- }
- // Check if Rotary Encoder was moved
- void check_rotary() {
- if ((PreviousCLK == 0) && (PreviousDATA == 1)) {
- if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
- displaycounter++;
- }
- if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
- displaycounter--;
- }
- }
- if ((PreviousCLK == 1) && (PreviousDATA == 0)) {
- if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
- displaycounter++;
- }
- if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
- displaycounter--;
- }
- }
- if ((PreviousCLK == 1) && (PreviousDATA == 1)) {
- if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
- displaycounter++;
- }
- if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
- displaycounter--;
- }
- }
- if ((PreviousCLK == 0) && (PreviousDATA == 0)) {
- if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
- displaycounter++;
- }
- if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
- displaycounter--;
- }
- }
- Serial.print("Counter = "); Serial.println(displaycounter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement