Advertisement
Guest User

Untitled

a guest
Sep 4th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Rotary Encoder Module connections
  2. const int PinSW=3;   // Rotary Encoder Switch
  3. const int PinDT=4;    // DATA signal
  4. const int PinCLK=2;    // CLOCK signal
  5.  
  6. // Variables to debounce Rotary Encoder
  7. long TimeOfLastDebounce = 0;
  8. int DelayofDebounce = 0.01;
  9.  
  10. // Store previous Pins state
  11. int PreviousCLK;  
  12. int PreviousDATA;
  13.  
  14. int displaycounter=0; // Store current counter value
  15.  
  16. void isr ()
  17.   {
  18.  
  19.   // If enough time has passed check the rotary encoder
  20.   if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
  21.    
  22.     check_rotary();  // Rotary Encoder check routine below
  23.    
  24.     PreviousCLK=digitalRead(PinCLK);
  25.     PreviousDATA=digitalRead(PinDT);
  26.    
  27.     TimeOfLastDebounce=millis();  // Set variable to current millis() timer
  28.   }
  29.  
  30.   // Check if Rotary Encoder switch was pressed
  31.   if (digitalRead(PinSW) == LOW) {
  32.     displaycounter=0;  // Reset counter to zero
  33.   }
  34.    
  35.   }
  36.  
  37.  
  38.  
  39. void setup() {
  40.  
  41.   // Set the Switch pin to use Arduino PULLUP resistors
  42.   pinMode(PinSW, INPUT_PULLUP);
  43.   pinMode(PinCLK,INPUT_PULLUP);
  44.   pinMode(PinDT,INPUT_PULLUP);
  45.   Serial.begin(115200);
  46.   Serial.println("Encoder");
  47. }
  48.  
  49. void loop() {
  50.  
  51. }
  52.  
  53. // Check if Rotary Encoder was moved
  54. void check_rotary() {
  55.  
  56.  if ((PreviousCLK == 0) && (PreviousDATA == 1)) {
  57.     if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
  58.       displaycounter++;
  59.     }
  60.     if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
  61.       displaycounter--;  
  62.     }
  63.   }
  64.  
  65. if ((PreviousCLK == 1) && (PreviousDATA == 0)) {
  66.     if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
  67.       displaycounter++;
  68.     }
  69.     if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
  70.       displaycounter--;  
  71.     }
  72.   }
  73.  
  74. if ((PreviousCLK == 1) && (PreviousDATA == 1)) {
  75.     if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
  76.       displaycounter++;
  77.     }
  78.     if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
  79.       displaycounter--;
  80.     }
  81.   }  
  82.  
  83. if ((PreviousCLK == 0) && (PreviousDATA == 0)) {
  84.     if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
  85.       displaycounter++;
  86.     }
  87.     if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
  88.       displaycounter--;
  89.  
  90.     }
  91.   }  
  92.           Serial.print("Counter = "); Serial.println(displaycounter);    
  93.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement