uint32_t debounceDelay = 50;
uint32_t lastDebounceTimeUp;
uint32_t currentCLKUp;
uint32_t previousCLKUp;
uint32_t lastDebounceTimeDown;
uint32_t currentCLKDown;
uint32_t previousCLKDown;
bool encoderCCW, encoderCW;
uint8_t rockerUpPin = A0;
uint8_t rockerDownPin = A1;
int counter;
void setup() {
Serial.begin(115200);
pinMode(rockerDownPin, INPUT_PULLUP);
pinMode(rockerUpPin, INPUT_PULLUP);
}
void loop() {
Rotate();
if (encoderCW) {
encoderCW = false;
Serial.println((counter++));
}
if (encoderCCW) {
encoderCCW = false;
Serial.println((counter--));
}
}
void Rotate() {
unsigned long currentTime = millis();
// Logic for rockerUpPin (Clockwise)
if ((currentTime - lastDebounceTimeUp) > debounceDelay) {
int currentCLKUp = digitalRead(rockerUpPin);
if (currentCLKUp == HIGH && previousCLKUp == LOW) {
encoderCCW = true; // Clockwise rotation detected
}
previousCLKUp = currentCLKUp; // Update previous state
lastDebounceTimeUp = currentTime; // Update debounce time for rockerUpPin
}
// Logic for rockerDownPin (Counterclockwise)
if ((currentTime - lastDebounceTimeDown) > debounceDelay) {
int currentCLKDown = digitalRead(rockerDownPin);
if (currentCLKDown == HIGH && previousCLKDown == LOW) {
encoderCW = true; // Counterclockwise rotation detected
}
previousCLKDown = currentCLKDown; // Update previous state
lastDebounceTimeDown = currentTime; // Update debounce time for rockerDownPin
}
}
CCW
CW