Last active
November 8, 2022 09:47
-
-
Save barwis/459b0798b6b4b01db89a3ecd949ca5e3 to your computer and use it in GitHub Desktop.
AiEsp32RotaryEncoder setup
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
#include <pins_arduino.h> | |
#include "AiEsp32RotaryEncoder.h" | |
#define KY040_SW D3 | |
#define KY040_CLK D4 | |
#define KY040_DT D5 | |
#define KY040_VCC -1 // -1 if rotary encoder is connected directly to 3.3V | |
#define KY040_STEPS 4 //depending on your encoder - try 1,2 or 4 to get expected behaviour | |
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(KY040_DT, KY040_CLK, KY040_SW, KY040_VCC, KY040_STEPS); | |
void rotary_loop() { | |
//dont print anything unless value changed | |
if (rotaryEncoder.encoderChanged()) { | |
int value = rotaryEncoder.readEncoder(); | |
// handle value change here | |
} | |
if (rotaryEncoder.isEncoderButtonClicked()) { | |
rotary_onButtonClick(); | |
} | |
} | |
// interrupt detect callback | |
void IRAM_ATTR readEncoderISR() { | |
rotaryEncoder.readEncoder_ISR(); | |
} | |
void rotary_onButtonClick() { | |
// handle click change here | |
} | |
void initRotaryEncoder() { | |
rotaryEncoder.begin(); | |
rotaryEncoder.setup(readEncoderISR); | |
bool circleValues = false; | |
rotaryEncoder.setBoundaries(rotatorBoundaries[0], rotatorBoundaries[1], circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa) | |
/*Rotary acceleration introduced 25.2.2021. | |
* in case range to select is huge, for example - select a value between 0 and 1000 and we want 785 | |
* without accelerateion you need long time to get to that number | |
* Using acceleration, faster you turn, faster will the value raise. | |
* For fine tuning slow down. | |
*/ | |
//rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it | |
rotaryEncoder.setAcceleration(150); //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration | |
} | |
void setup() { | |
initRotaryEncoder(); | |
} | |
void loop() { | |
rotary_loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment