I'm trying to setup two encoders and log the usage individually. I'm not very good at c++, so i'm using the built in example from this library (SimpleCounterWithButton): LennartHennigs/ESPRotary
LennartHennigs/ESPRotary
I'm able to get a single encoder working, but it doesn't register the second and I'm not sure why. I think it has to do with how I'm defining it in the setup or with the functions near the bottom. Here's the code:
/////////////////////////////////////////////////////////////////
#include "Button2.h"; // https://github.com/LennartHennigs/Button2
#include "ESPRotary.h";
/////////////////////////////////////////////////////////////////
#define ROTARY1_PIN1 4
#define ROTARY1_PIN2 6
#define BUTTON1_PIN 7
#define ROTARY2_PIN1 2
#define ROTARY2_PIN2 3
#define BUTTON2_PIN 8
#define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
/////////////////////////////////////////////////////////////////
ESPRotary r;
Button2 b;
int count;
/////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
delay(50);
Serial.println("\n\nSimple Counter");
r.begin(ROTARY1_PIN1, ROTARY1_PIN2, CLICKS_PER_STEP);
r.begin(ROTARY2_PIN1, ROTARY2_PIN2, CLICKS_PER_STEP);
r.setChangedHandler(rotate);
r.setLeftRotationHandler(showDirection);
r.setRightRotationHandler(showDirection);
b.begin(BUTTON1_PIN);
b.begin(BUTTON2_PIN);
b.setTapHandler(click);
b.setLongClickHandler(resetPosition);
}
void loop() {
r.loop();
b.loop();
}
/////////////////////////////////////////////////////////////////
// on change
void rotate(ESPRotary& r) {
Serial.println(r.getPosition());
}
// on left or right rotation
void showDirection(ESPRotary& r) {
Serial.println(r.directionToString(r.getDirection()));
}
// single click
void click(Button2& btn) {
Serial.println("Click!");
count++;
Serial.println(count);
}
// long click
void resetPosition(Button2& btn) {
r.resetPosition();
Serial.println("Reset!");
}
/////////////////////////////////////////////////////////////////