#include "OneButton.h"
const byte rotarySwitchPin = 4;
OneButton rotarySwitch(rotarySwitchPin);
void singleClick() {
Serial.println("SINGLE CLICK");
}
void doubleClick() {
Serial.println("DOUBLE CLICK");
}
void longClickStart() {
Serial.println("LONG CLICK START");
}
void longClickOngoing() {
Serial.println("ONGOING LONG CLICK");
}
void longPressStop() {
Serial.println("LONG CLICK STOP");
}
void setup() {
pinMode(rotarySwitchPin, INPUT_PULLUP); // necesary on some boards as OneButton does this in the constructor which might be undone during init phase
Serial.begin(115200);
rotarySwitch.attachClick(singleClick);
rotarySwitch.attachDoubleClick(doubleClick);
rotarySwitch.attachLongPressStart(longClickStart);
rotarySwitch.setLongPressIntervalMs(100); // will call the ongoing callback every 100ms
rotarySwitch.attachDuringLongPress(longClickOngoing);
rotarySwitch.attachLongPressStop(longPressStop);
}
void loop() {
rotarySwitch.tick();
// you can do other stuff here
}