/*Test to detect double click of button
*
* Uses MultiButton to detect double-click
* Github page: https://github.com/poelstra/arduino-multi-button
*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rotary-encoder
*/
// ---> #include <ezButton.h> // the library to use for SW pin
#include <PinButton.h>
#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4
#define DIRECTION_CW 0 // clockwise direction
#define DIRECTION_CCW 1 // counter-clockwise direction
int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
// ---> ezButton button(SW_PIN); // create ezButton object that attach to pin 4
PinButton button(SW_PIN);
void setup() {
Serial.begin(9600);
// configure encoder pins as inputs
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);
// ---> button.setDebounceTime(50); // set debounce time to 50 milliseconds
// ---> button.setCountMode(COUNT_FALLING);
// read the initial state of the rotary encoder's CLK pin
prev_CLK_state = digitalRead(CLK_PIN);
}
void loop() {
// MUST call the button function first
// ---> button.loop();
button.update();
// read current state of the rotary encoder's CLK pin
CLK_state = digitalRead(CLK_PIN);
// If the state of CLK is changed, then pulse occurred
// React to only the rising edge (from LOW to HIGH) to avoid double count
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
// if the DT state is HIGH
// the encoder is rotating in counter-clockwise direction => decrease the counter
if (digitalRead(DT_PIN) == HIGH) {
counter--;
direction = DIRECTION_CCW;
} else {
// the encoder is rotating in clockwise direction => increase the counter
counter++;
direction = DIRECTION_CW;
}
Serial.print("DIRECTION: ");
if (direction == DIRECTION_CW)
Serial.print("Clockwise");
else
Serial.print("Counter-clockwise");
Serial.print(" | COUNTER: ");
Serial.println(counter);
}
// save last CLK state
prev_CLK_state = CLK_state;
if (button.isSingleClick()) {
Serial.println("The button is pressed - once");
}
if (button.isDoubleClick()) {
Serial.println("The button is pressed - twice");
}
if (button.isLongClick()) {
Serial.println("The button is held for a long click");
}
}