Created
January 3, 2022 16:15
-
-
Save antwal/84dadfe67516cafa07d4c1c5204cbe40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Rotary Encoder Accelleration | |
* - Debug for KY-040 | |
* | |
* interrupt pins: | |
* 2, 3 - Arduino UNO/Nano | |
* D5,D6 - ESP8266 NodeMCU | |
*/ | |
#define DEBUG false | |
// Rotary Encoder Inputs | |
#if defined (ARDUINO_AVR_UNO) || defined (ARDUINO_AVR_NANO) | |
#define CLK 2 | |
#define DT 3 | |
#elif defined (ESP8266) | |
#define CLK D5 | |
#define DT D6 | |
#endif | |
#define SW 4 | |
#define PRESS_TIME_SHORT 20 // [ms] short press | |
#define PRESS_TIME_LONG 2000 // [ms] long press | |
#define ROTARY_FAST 15 // [ms] rotary speed fast | |
#define ROTARY_SLOW 200 // [ms] rotary speed slow | |
#define INCREMENT_SLOW 1 | |
#define INCREMENT_FAST 10 | |
int counter = 0, increment = 0; | |
int currentStateCLK, currentStateSW, lastStateCLK, lastStateSW; | |
String currentDir = ""; | |
unsigned long lastButtonPress = 0, lastButtonTime; | |
void setup() { | |
// Set encoder pins as inputs | |
pinMode(CLK,INPUT); | |
pinMode(DT,INPUT); | |
pinMode(SW, INPUT_PULLUP); | |
// Setup Serial Monitor | |
if (DEBUG) Serial.begin(9600); | |
// Read the initial state of CLK | |
lastStateCLK = digitalRead(CLK); | |
lastStateSW = digitalRead(SW); | |
if (DEBUG) Serial.println("Rotary Encoder Debug:"); | |
} | |
void loop() { | |
// Read the current state of CLK | |
currentStateCLK = digitalRead(CLK); | |
// If last and current state of CLK are different, then pulse occurred | |
// React to only 1 state change to avoid double count | |
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) { | |
if((millis() - lastButtonTime) > ROTARY_SLOW) { | |
increment = INCREMENT_SLOW; | |
if (DEBUG) Serial.print("[SLOW]> "); | |
} | |
else if((millis() - lastButtonTime) > ROTARY_FAST) { | |
increment = INCREMENT_FAST; | |
if (DEBUG) Serial.print("[FAST]> "); | |
} | |
else { | |
if (DEBUG) Serial.print("[ROT BOUNCED]"); | |
increment = 0; | |
} | |
lastButtonTime = millis(); | |
// If the DT state is different than the CLK state then | |
// the encoder is rotating CCW so decrement | |
if (digitalRead(DT) != currentStateCLK) { | |
counter = counter - increment; | |
if (DEBUG) Serial.print(" Decrement: "); | |
if (DEBUG) Serial.print(increment); | |
currentDir ="CCW"; | |
} else { | |
// Encoder is rotating CW so increment | |
counter = counter + increment; | |
if (DEBUG) Serial.print(" Increment: "); | |
if (DEBUG) Serial.print(increment); | |
currentDir ="CW"; | |
} | |
if (DEBUG) Serial.print(" | Direction: "); | |
if (DEBUG) Serial.print(currentDir); | |
if (DEBUG) Serial.print(" | Counter: "); | |
if (DEBUG) Serial.println(counter); | |
} | |
// Remember last CLK state | |
lastStateCLK = currentStateCLK; | |
// Read the button state | |
int btnState = digitalRead(SW); | |
//If we detect LOW signal, button is pressed | |
if ((lastStateSW == HIGH) && (btnState == LOW)) lastButtonPress = millis(); | |
if ((lastStateSW == LOW) && (btnState == HIGH)) { | |
// Check button pressed time | |
if (millis() - lastButtonPress > PRESS_TIME_LONG) { | |
if (DEBUG) Serial.println("Long Button pressed!"); | |
} else if (millis() - lastButtonPress > PRESS_TIME_SHORT) { | |
if (DEBUG) Serial.println("Short Button pressed!"); | |
} | |
// Remember last button press event | |
lastButtonPress = millis(); | |
} | |
lastStateSW = btnState; | |
// Put in a slight delay to help debounce the reading | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment