#include "OneButton.h"
#define ESP8266
#define PIN_INPUT 12
#define PIN_LED 13
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
OneButton button(PIN_INPUT, true);
// current LED state, staring with LOW (0)
int ledState = LOW;
// save the millis when a press has started.
unsigned long pressStartTime;
#if defined(ESP8266)
ICACHE_RAM_ATTR void checkTicks() {
button.tick(); // just call tick() to check the state.
}
#elif defined(ESP32)
void IRAM_ATTR checkTicks() {
// include all buttons here to be checked
button.tick(); }// just call tick() to check the state.
#endif
// this function will be called when the button was held down for 1 second or more.
void pressStart() {
Serial.println("pressStart()");
// pressStartTime = millis() - 500; // as set in setPressMs()
digitalWrite(PIN_LED, 1);
} // pressStart()
// this function will be called when the button was released after a long hold.
void pressStop() {
Serial.print("pressStop(");
Serial.println(") detected.");
digitalWrite(PIN_LED, 0);
} //pressStop()
// setup code here, to run once:
void setup() {
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
button.setPressMs(100); // that is the time when LongPressStart is called
button.attachLongPressStart(pressStart);
button.attachLongPressStop(pressStop);
}
void loop() {
// keep watching the push button, even when no interrupt happens:
button.tick();
}