Advertisement
learnelectronics

Rotary Encoder Sketch

Apr 16th, 2018
2,817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. /*
  2.  * Rotary Encoder Sketch
  3.  *
  4.  * learnelectronics
  5.  * 14 APR 2018
  6.  * www.youtube.com/c/learnelectronics
  7.  *
  8.  * Original Sketch by Ralph Bacon
  9.  *
  10.  */
  11.  
  12.  
  13.  
  14.  
  15. #include <Wire.h>
  16. #include <Adafruit_SSD1306.h>
  17.  
  18. #define OLED_RESET 4
  19.  
  20. Adafruit_SSD1306 display(OLED_RESET);
  21.  
  22.  
  23.  
  24.  
  25.  
  26. // Used for generating interrupts using CLK signal
  27. const int PinA = 2;
  28.  
  29. // Used for reading DT signal
  30. const int PinB = 3;  
  31.  
  32. // Used for the push button switch
  33. const int PinSW = 8;  
  34.  
  35. // Simple PWM LED pin
  36. #define PinLED 11
  37.  
  38. // Keep track of last rotary value
  39. int lastCount = 50;
  40.  
  41. // Updated by the ISR (Interrupt Service Routine)
  42. volatile int virtualPosition = 50;
  43.  
  44. // ------------------------------------------------------------------
  45. // INTERRUPT     INTERRUPT     INTERRUPT     INTERRUPT     INTERRUPT
  46. // ------------------------------------------------------------------
  47. void isr ()  {
  48.   static unsigned long lastInterruptTime = 0;
  49.   unsigned long interruptTime = millis();
  50.  
  51.   // If interrupts come faster than 5ms, assume it's a bounce and ignore
  52.   if (interruptTime - lastInterruptTime > 5) {
  53.     if (digitalRead(PinB) == LOW)
  54.     {
  55.       virtualPosition-- ; // Could be -5 or -10
  56.     }
  57.     else {
  58.       virtualPosition++ ; // Could be +5 or +10
  59.     }
  60.  
  61.     // Restrict value from 0 to +100
  62.     virtualPosition = min(100, max(0, virtualPosition));
  63.  
  64.     // Keep track of when we were here last (no more than every 5ms)
  65.     lastInterruptTime = interruptTime;
  66.   }
  67. }
  68.  
  69. // ------------------------------------------------------------------
  70. // SETUP    SETUP    SETUP    SETUP    SETUP    SETUP    SETUP    
  71. // ------------------------------------------------------------------
  72. void setup() {
  73.   // Just whilst we debug, view output on serial monitor
  74.   Serial.begin(9600);
  75.  
  76.   // Rotary pulses are INPUTs
  77.   pinMode(PinA, INPUT);
  78.   pinMode(PinB, INPUT);
  79.  
  80.   // Switch is floating so use the in-built PULLUP so we don't need a resistor
  81.   pinMode(PinSW, INPUT_PULLUP);
  82.  
  83.   // Attach the routine to service the interrupts
  84.   attachInterrupt(digitalPinToInterrupt(PinA), isr, LOW);
  85.  
  86.  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  87.   display.display();
  88.   delay(2);
  89.   display.clearDisplay();
  90.   display.setTextSize(3);
  91.   display.setTextColor(WHITE);
  92.   display.setCursor(0,0);
  93.  
  94.   display.println("Start");
  95.   display.display();
  96. }
  97.  
  98. // ------------------------------------------------------------------
  99. // MAIN LOOP     MAIN LOOP     MAIN LOOP     MAIN LOOP     MAIN LOOP
  100. // ------------------------------------------------------------------
  101. void loop() {
  102.  
  103.   display.clearDisplay();
  104.   display.setTextSize(3);
  105.   display.setTextColor(WHITE);
  106.   display.setCursor(0,0);
  107.  
  108.   // Is someone pressing the rotary switch?
  109.   if ((!digitalRead(PinSW))) {
  110.     virtualPosition = 50;
  111.     while (!digitalRead(PinSW))
  112.       delay(10);
  113.       display.setCursor(0,0);
  114.     display.println("Reset");
  115.     display.display();
  116.   }
  117.  
  118.   // If the current rotary switch position has changed then update everything
  119.   if (virtualPosition != lastCount) {
  120.    
  121.     // Our LED gets brighter or dimmer
  122.     analogWrite(PinLED, virtualPosition);
  123.  
  124.     // Write out to serial monitor the value and direction
  125.    
  126.     display.clearDisplay();
  127.   display.setTextSize(3);
  128.   display.setTextColor(WHITE);
  129.   display.setCursor(0,0);
  130.     display.print(virtualPosition > lastCount ? "Up  :" : "Down:");
  131.     display.println(virtualPosition);
  132.     display.display();
  133.  
  134.     // Keep track of this new value
  135.     lastCount = virtualPosition ;
  136.   }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement