/*
OneButon - several functions
https://forum.arduino.cc/t/taste-fur-verschiedene-funktionen-verwenden/1115508
2023-04-15 by noiasca
to be deleted: 2023-06
code in forum
*/
#include "OneButton.h" //we need the OneButton library
const uint8_t buttonPin = A0;
bool test = 0;
OneButton button(buttonPin, true, true);
void doA(){
Serial.println(F("doA"));
}
void doB() {
static uint8_t counter = 0;
counter++;
if (counter > 5) counter = 0;
Serial.println(counter);
}
void setup()
{
Serial.begin(115200);
Serial.println("\nOneButton Example.");
Serial.println("Please press and hold the button for a few seconds.");
// link functions to be called on events.
button.attachClick(singleclick); // link the function to be called on a singleclick event.
button.setDebounceMs(50); // time to pass by before a click is assumed stable.
button.setClickMs(200); // time to pass by before a click is detected.
button.setPressMs(1000); // that is the time when LongPressStart is called
button.setLongPressIntervalMs(1000); //set interval in msecs between calls of the DuringLongPress event. 0 ms is the fastest events calls.
button.attachLongPressStart(LongPressStart);
button.attachDuringLongPress(DuringLongPress);
button.attachLongPressStop(LongPressStop);
} // setup
// main code here, to run repeatedly:
void loop()
{
// keep watching the push button:
button.tick();
//Serial.print(test);
test = 0;
Serial.println(button.getPressedMs());
// You can implement other code in here or just wait a while
delay(10);
} // loop
void singleclick(void *oneButton)
{
test = 1;
Serial.print(test);
}
// this function will be called when the button started long pressed.
void LongPressStart(void *oneButton)
{
Serial.print(((OneButton *)oneButton)->getPressedMs());
Serial.println("\t - LongPressStart()");
}
// this function will be called when the button is released.
void LongPressStop(void *oneButton)
{
Serial.print(((OneButton *)oneButton)->getPressedMs());
Serial.println("\t - LongPressStop()\n");
}
// this function will be called when the button is held down.
void DuringLongPress(void *oneButton)
{
Serial.print(button.getPressedMs());
Serial.print(" ");
Serial.print(((OneButton *)oneButton)->getPressedMs());
Serial.println("\t - DuringLongPress()");
}
// End