Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Rotary Encoder Sketch
- *
- * learnelectronics
- * 14 APR 2018
- * www.youtube.com/c/learnelectronics
- * email: [email protected]
- *
- * Original Sketch by Ralph Bacon
- *
- */
- #include <Wire.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- // Used for generating interrupts using CLK signal
- const int PinA = 2;
- // Used for reading DT signal
- const int PinB = 3;
- // Used for the push button switch
- const int PinSW = 8;
- // Simple PWM LED pin
- #define PinLED 11
- // Keep track of last rotary value
- int lastCount = 50;
- // Updated by the ISR (Interrupt Service Routine)
- volatile int virtualPosition = 50;
- // ------------------------------------------------------------------
- // INTERRUPT INTERRUPT INTERRUPT INTERRUPT INTERRUPT
- // ------------------------------------------------------------------
- void isr () {
- static unsigned long lastInterruptTime = 0;
- unsigned long interruptTime = millis();
- // If interrupts come faster than 5ms, assume it's a bounce and ignore
- if (interruptTime - lastInterruptTime > 5) {
- if (digitalRead(PinB) == LOW)
- {
- virtualPosition-- ; // Could be -5 or -10
- }
- else {
- virtualPosition++ ; // Could be +5 or +10
- }
- // Restrict value from 0 to +100
- virtualPosition = min(100, max(0, virtualPosition));
- // Keep track of when we were here last (no more than every 5ms)
- lastInterruptTime = interruptTime;
- }
- }
- // ------------------------------------------------------------------
- // SETUP SETUP SETUP SETUP SETUP SETUP SETUP
- // ------------------------------------------------------------------
- void setup() {
- // Just whilst we debug, view output on serial monitor
- Serial.begin(9600);
- // Rotary pulses are INPUTs
- pinMode(PinA, INPUT);
- pinMode(PinB, INPUT);
- // Switch is floating so use the in-built PULLUP so we don't need a resistor
- pinMode(PinSW, INPUT_PULLUP);
- // Attach the routine to service the interrupts
- attachInterrupt(digitalPinToInterrupt(PinA), isr, LOW);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.display();
- delay(2);
- display.clearDisplay();
- display.setTextSize(3);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.println("Start");
- display.display();
- }
- // ------------------------------------------------------------------
- // MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP
- // ------------------------------------------------------------------
- void loop() {
- display.clearDisplay();
- display.setTextSize(3);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- // Is someone pressing the rotary switch?
- if ((!digitalRead(PinSW))) {
- virtualPosition = 50;
- while (!digitalRead(PinSW))
- delay(10);
- display.setCursor(0,0);
- display.println("Reset");
- display.display();
- }
- // If the current rotary switch position has changed then update everything
- if (virtualPosition != lastCount) {
- // Our LED gets brighter or dimmer
- analogWrite(PinLED, virtualPosition);
- // Write out to serial monitor the value and direction
- display.clearDisplay();
- display.setTextSize(3);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.print(virtualPosition > lastCount ? "Up :" : "Down:");
- display.println(virtualPosition);
- display.display();
- // Keep track of this new value
- lastCount = virtualPosition ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement