### Initialize OneButton with Deferred Setup Source: https://github.com/mathertel/onebutton/blob/master/README.md Declare a global instance and initialize it within the setup function to handle hardware initialization order issues. ```cpp OneButton btn; ``` ```cpp btn.setup( BUTTON_PIN, // Input pin for the button INPUT_PULLUP, // INPUT and enable the internal pull-up resistor true // Button is active LOW ); ``` -------------------------------- ### Button Configuration Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Configure timing parameters for debouncing and event recognition within the setup() function. ```cpp // Configuration in setup() btn.setDebounceMs(50); // Debouncing delay (default: 20ms) btn.setClickMs(400); // Time to recognize single click (default: 400ms) btn.setPressMs(800); // Time to trigger press event (default: 800ms) btn.setLongPressIntervalMs(100); // Interval for repeated long-press callbacks (default: 0) btn.setIdleMs(3000); // Auto-idle timeout (optional) ``` -------------------------------- ### Deferred Initialization Pattern Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Declare buttons globally and initialize them within the setup() function to ensure correct hardware configuration. ```cpp // Declaration OneButton btn; // In setup() btn.setup(BUTTON_PIN, INPUT_PULLUP, true); // pin, mode, activeLow ``` -------------------------------- ### Single Button with Multiple Events Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Example implementation handling various button events for a single instance. ```cpp OneButton btn; void setup() { btn.setup(BUTTON_PIN, INPUT_PULLUP, true); btn.attachClick(handleClick); btn.attachDoubleClick(handleDoubleClick); btn.attachLongPressStart(handleLongStart); btn.attachLongPressStop(handleLongStop); } void loop() { btn.tick(); } void handleClick() { /* single click logic */ } void handleDoubleClick() { /* double click logic */ } void handleLongStart() { /* long press started */ } void handleLongStop() { /* long press ended */ } ``` -------------------------------- ### attachLongPressStart - Long Press Start Event Source: https://context7.com/mathertel/onebutton/llms.txt Shows how to attach a callback that fires once when a long press is initiated, marking the beginning of a sustained press action. ```APIDOC ## attachLongPressStart - Long Press Start Event Attaches a callback that fires once when the button has been held down for the press duration (default 800ms). This marks the beginning of a long press action. ```cpp #include OneButton button(2, true, true); unsigned long pressStartTime; void handleLongPressStart() { pressStartTime = millis(); Serial.println("Long press started - hold to continue..."); } void setup() { Serial.begin(115200); button.attachLongPressStart(handleLongPressStart); button.setPressMs(1000); // Require 1 second hold for long press } void loop() { button.tick(); } ``` ``` -------------------------------- ### Create OneButton Instance Source: https://context7.com/mathertel/onebutton/llms.txt Instantiates a OneButton object to manage button input. Configure the pin, active state, and pull-up resistor during creation or later in setup(). ```cpp #include // Button connected to pin 2, active LOW (pressed = LOW), internal pullup enabled OneButton button(2, true, true); // Button connected to VCC (active HIGH), no internal pullup OneButton buttonVCC(4, false, false); // Deferred initialization - create instance first, configure later OneButton deferredButton; void setup() { // Initialize the deferred button in setup() deferredButton.setup(2, INPUT_PULLUP, true); } ``` -------------------------------- ### OneButtonTiny Setup for Memory-Constrained Devices Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Initializes the OneButtonTiny library for detecting single clicks, double clicks, and long presses. Note that MultiClick and LongPressStop events are not available in the Tiny version. ```cpp #include OneButtonTiny btn; void setup() { btn.setup(BUTTON_PIN, INPUT_PULLUP, true); btn.attachClick(handleClick); // ✓ Available btn.attachDoubleClick(handleDblClick); // ✓ Available btn.attachLongPressStart(handleLong); // ✓ Available // Note: MultiClick, LongPressStop NOT available in Tiny version } ``` -------------------------------- ### Attach Long Press Start Event Handler Source: https://context7.com/mathertel/onebutton/llms.txt Attaches a callback that fires once when the button is held down for the configured press duration. Use setPressMs() to customize this duration. ```cpp #include OneButton button(2, true, true); unsigned long pressStartTime; void handleLongPressStart() { pressStartTime = millis(); Serial.println("Long press started - hold to continue..."); } void setup() { Serial.begin(115200); button.attachLongPressStart(handleLongPressStart); button.setPressMs(1000); // Require 1 second hold for long press } void loop() { button.tick(); } ``` -------------------------------- ### Initialize OneButton Instance (Global) Source: https://github.com/mathertel/onebutton/blob/master/README.md Create a global OneButton instance with hardware configuration defined at compile time. ```cpp // Declare and initialize OneButton btn = OneButton( BUTTON_PIN, // Input pin for the button true, // Button is active LOW true // Enable internal pull-up resistor ); ``` -------------------------------- ### Attach Button Click and Multi-Click Events Source: https://github.com/mathertel/onebutton/blob/master/README.md Demonstrates attaching static handler functions and lambdas for single clicks, double clicks, and multi-clicks. For multi-click, it shows how to pass the button instance as a parameter. ```CPP // Handler function for a single click: static void handleClick() { Serial.println("Clicked!"); } // Single Click event attachment btn.attachClick(handleClick); // Double Click event attachment with lambda btn.attachDoubleClick([]() { Serial.println("Double Pressed!"); }); // Handler function for MultiClick the button with self pointer as a parameter static void handleMultiClick(OneButton *oneButton) { Serial.println("MultiClick pin=%d debouncedValue=%d!", oneButton->pin(), oneButton->debouncedValue()); } // MultiClick button event attachment with self pointer as a parameter btn.attachMultiClick(handleMultiClick, &btn); ``` -------------------------------- ### OneButton Constructor Source: https://context7.com/mathertel/onebutton/llms.txt Demonstrates how to create and initialize a OneButton object to manage pushbutton input, including options for active state and pull-up resistors. ```APIDOC ## Constructor - Create OneButton Instance Creates a new OneButton object to manage button input. The constructor accepts the pin number, active state configuration (LOW when pressed vs HIGH when pressed), and whether to enable the internal pull-up resistor. ```cpp #include // Button connected to pin 2, active LOW (pressed = LOW), internal pullup enabled OneButton button(2, true, true); // Button connected to VCC (active HIGH), no internal pullup OneButton buttonVCC(4, false, false); // Deferred initialization - create instance first, configure later OneButton deferredButton; void setup() { // Initialize the deferred button in setup() deferredButton.setup(2, INPUT_PULLUP, true); } ``` ``` -------------------------------- ### Initialize Button to GND Source: https://github.com/mathertel/onebutton/blob/master/README.md Configure a OneButton instance for a button connected to a digital pin and GND, using the internal pull-up resistor. ```CPP #define BUTTON_PIN 4 /** * Initialize a new OneButton instance for a button * connected to digital pin 4 and GND, which is active low * and uses the internal pull-up resistor. */ OneButton btn = OneButton( BUTTON_PIN, // Input pin for the button true, // Button is active LOW true // Enable internal pull-up resistor ); ``` -------------------------------- ### Implement Parameterized Callbacks Source: https://context7.com/mathertel/onebutton/llms.txt Demonstrates passing context data to event handlers, enabling class-based button management. ```cpp #include class Button { private: OneButton button; int counter = 0; public: explicit Button(uint8_t pin) : button(pin) { // Use lambda with 'this' pointer as context button.attachClick([](void *scope) { ((Button *)scope)->onClick(); }, this); button.attachDoubleClick([](void *scope) { ((Button *)scope)->onDoubleClick(); }, this); button.attachLongPressStart([](void *scope) { ((Button *)scope)->onLongPress(); }, this); } void onClick() { counter++; Serial.print("Click! Counter: "); Serial.println(counter); } void onDoubleClick() { counter = 0; Serial.println("Double click - counter reset!"); } void onLongPress() { Serial.print("Long press - final counter: "); Serial.println(counter); } void handle() { button.tick(); } }; Button myButton(2); void setup() { Serial.begin(115200); } void loop() { myButton.handle(); } ``` -------------------------------- ### Include OneButton Library Source: https://github.com/mathertel/onebutton/blob/master/README.md Include the necessary headers to use the OneButton library in your Arduino project. ```CPP #include #include ``` -------------------------------- ### Manage Button State and Idle Detection Source: https://context7.com/mathertel/onebutton/llms.txt Utilize reset() to clear state machines and isIdle() for power management in battery-operated projects. ```cpp #include OneButton button(2, true, true); void setup() { Serial.begin(115200); button.attachClick([]() { Serial.println("Click - resetting state"); button.reset(); // Clear any pending state }); button.setIdleMs(5000); // Consider idle after 5 seconds button.attachIdle([]() { Serial.println("Button idle - entering sleep mode"); // Enter low-power sleep mode here }); } void loop() { button.tick(); if (button.isIdle()) { // No button activity - safe for power-down // Can check this before entering sleep modes } } ``` -------------------------------- ### Initialize Button to VCC Source: https://github.com/mathertel/onebutton/blob/master/README.md Configure a OneButton instance for a button connected to VCC, active high, without internal pull-up resistors. ```CPP #define BUTTON_PIN 4 /** * Initialize a new OneButton instance for a button * connected to digital pin 4, which is active high. * As this does not use any internal resistor * an external resistor (4.7k) may be required to create a LOW signal when the button is not pressed. */ OneButton btn = OneButton( BUTTON_PIN, // Input pin for the button false, // Button is active high false // Disable internal pull-up resistor ); ``` -------------------------------- ### Simulate Single Button Press for Testing Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md This function simulates a single button press and release within a mock or test environment. It includes delays to mimic real-world timing and allow for click detection. ```cpp // Simulate button press duration (in mock/test environment) void testSingleClick(OneButton &btn) { // Simulate 100ms press pressButton(btn); delay(100); releaseButton(btn); delay(500); // Wait for single-click detection } ``` -------------------------------- ### attachPress - Immediate Press Event Source: https://context7.com/mathertel/onebutton/llms.txt Demonstrates attaching a callback function that triggers immediately upon detecting a button press. ```APIDOC ## attachPress - Immediate Press Event Attaches a callback that fires immediately when a button press is detected (before determining click type). Useful for providing immediate feedback on button interaction. ```cpp #include OneButton button(2, true, true); void handlePress() { Serial.println("Button pressed down!"); } void setup() { Serial.begin(115200); button.attachPress(handlePress); } void loop() { button.tick(); } ``` ``` -------------------------------- ### Configure Timing Thresholds Source: https://context7.com/mathertel/onebutton/llms.txt Adjusts debounce, click detection, and long press timing to suit specific hardware requirements. ```cpp #include OneButton button(2, true, true); void setup() { Serial.begin(115200); // Set debounce time (default: 50ms) // Higher values for noisy switches, lower for responsive buttons button.setDebounceMs(30); // Set click timeout (default: 400ms) // Time window for detecting double/multi clicks button.setClickMs(300); // Set long press threshold (default: 800ms) // How long to hold before triggering long press button.setPressMs(1000); // Set interval for DuringLongPress callbacks (default: 0ms = every tick) button.setLongPressIntervalMs(200); // Set idle timeout (default: 1000ms) button.setIdleMs(2000); // Negative debounce: immediate press detection, debounce only on release // button.setDebounceMs(-25); button.attachClick([]() { Serial.println("Click"); }); button.attachDoubleClick([]() { Serial.println("Double"); }); button.attachLongPressStart([]() { Serial.println("Long press"); }); } void loop() { button.tick(); } ``` -------------------------------- ### Multiple Buttons with Shared Handler Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Manage multiple buttons using an array and a shared parameterized callback to identify the source button. ```cpp #define NUM_BUTTONS 3 OneButton buttons[NUM_BUTTONS]; int btn_pins[NUM_BUTTONS] = {2, 3, 4}; void setup() { for (int i = 0; i < NUM_BUTTONS; i++) { buttons[i].setup(btn_pins[i], INPUT_PULLUP, true); buttons[i].attachClick(handleButtonClick, (void*)i); } } void loop() { for (int i = 0; i < NUM_BUTTONS; i++) { buttons[i].tick(); } } void handleButtonClick(void *param) { int buttonId = (int)param; // Handle click for specific button } ``` -------------------------------- ### Callback Pattern Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Define handlers as simple functions or parameterized functions to pass context data. ```cpp // Simple callback (preferred for single-purpose buttons) void handleClick() { // Handle event } // Parameterized callback (better for multiple buttons with shared handler) void handleClick(void *param) { int buttonId = (int)param; // Handle event based on buttonId } ``` -------------------------------- ### Implement OneButtonTiny for Memory-Constrained Devices Source: https://context7.com/mathertel/onebutton/llms.txt Use this version for ATtiny processors to save program space. Note that it lacks parameterized callbacks and specific long-press events. ```cpp #include // OneButtonTiny supports: Click, DoubleClick, MultiClick, LongPressStart // Does NOT support: parameterized callbacks, DuringLongPress, LongPressStop, attachPress OneButtonTiny button(2, true, true); void handleClick() { // Toggle LED or perform action PORTB ^= (1 << PB0); // Direct port manipulation for ATtiny } void handleDoubleClick() { // Different action for double click } void handleLongPress() { // Long press action } void setup() { DDRB |= (1 << PB0); // Set PB0 as output button.attachClick(handleClick); button.attachDoubleClick(handleDoubleClick); button.attachLongPressStart(handleLongPress); // Timing configuration available button.setDebounceMs(50); button.setClickMs(400); button.setPressMs(800); } void loop() { button.tick(); } ``` -------------------------------- ### Custom Input Source with tick(bool) Source: https://context7.com/mathertel/onebutton/llms.txt Use tick(bool) to provide button state from any input source, not just digital pins. This enables using analog inputs, touch sensors, capacitive buttons, or networked inputs. ```cpp #include // Create button without pin assignment OneButton button; void setup() { Serial.begin(115200); // Set up your own input source pinMode(A0, INPUT); button.attachClick([](void *s) { Serial.print("Click from: "); Serial.println((char *)s); }, (void *)"custom input"); button.attachDoubleClick([]() { Serial.println("Double click!"); }); } void loop() { // Read from any input source // Example: analog threshold, touch sensor, I2C expander, etc. int analogValue = analogRead(A0); bool isPressed = (analogValue < 512); // Custom threshold logic // Pass the state directly to tick() button.tick(isPressed); } ``` -------------------------------- ### Attach Immediate Press Event Handler Source: https://context7.com/mathertel/onebutton/llms.txt Attaches a callback that triggers immediately upon detecting a button press, before classifying the click type. Useful for instant user feedback. ```cpp #include OneButton button(2, true, true); void handlePress() { Serial.println("Button pressed down!"); } void setup() { Serial.begin(115200); button.attachPress(handlePress); } void loop() { button.tick(); } ``` -------------------------------- ### Main Loop Integration Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md The tick() method must be called repeatedly within the main loop to process button state transitions. ```cpp void loop() { btn.tick(); // Usually called on every iteration // Other code... } ``` -------------------------------- ### Attach Click Event with Contextual Lambda Source: https://github.com/mathertel/onebutton/blob/master/README.md Shows how to use parameterizedCallbackFunction to attach a lambda that captures context, passing 'this' as the context pointer to be retrieved within the lambda. ```CPP okBtn.attachClick([](void *ctx){Serial.println(*((BtnHandler*)ctx) -> state);}, this); ``` -------------------------------- ### attachDoubleClick - Double Click Event Source: https://context7.com/mathertel/onebutton/llms.txt Explains how to attach a callback function for detecting and handling double-click events. ```APIDOC ## attachDoubleClick - Double Click Event Attaches a callback function that fires when two clicks occur within the click timeout period (default 400ms). When a double-click handler is attached, single click detection is delayed to distinguish between the two events. ```cpp #include OneButton button(2, true, true); void handleDoubleClick() { Serial.println("Double click detected!"); } void setup() { Serial.begin(115200); button.attachDoubleClick(handleDoubleClick); // Or use lambda button.attachDoubleClick([]() { Serial.println("Double Pressed!"); }); } void loop() { button.tick(); } ``` ``` -------------------------------- ### Managing Multiple Buttons Source: https://context7.com/mathertel/onebutton/llms.txt Manage multiple buttons by creating separate OneButton instances for each physical button. Each button operates independently with its own event handlers. Ensure to call tick() for all buttons in the loop. ```cpp #include #define PIN_BUTTON1 2 #define PIN_BUTTON2 3 OneButton button1(PIN_BUTTON1, true, true); OneButton button2(PIN_BUTTON2, true, true); void click1() { Serial.println("Button 1 click"); } void click2() { Serial.println("Button 2 click"); } void doubleClick1() { Serial.println("Button 1 double click"); } void doubleClick2() { Serial.println("Button 2 double click"); } void longPress1() { Serial.println("Button 1 long press start"); } void longPress2() { Serial.println("Button 2 long press start"); } void longPressStop1() { Serial.println("Button 1 long press stop"); } void longPressStop2() { Serial.println("Button 2 long press stop"); } void setup() { Serial.begin(115200); Serial.println("Two Button Example"); // Configure button 1 button1.attachClick(click1); button1.attachDoubleClick(doubleClick1); button1.attachLongPressStart(longPress1); button1.attachLongPressStop(longPressStop1); // Configure button 2 button2.attachClick(click2); button2.attachDoubleClick(doubleClick2); button2.attachLongPressStart(longPress2); button2.attachLongPressStop(longPressStop2); } void loop() { // Must tick ALL buttons in the loop button1.tick(); button2.tick(); delay(10); } ``` -------------------------------- ### Simulate Double Button Press for Testing Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md This function simulates a double button press and release sequence for testing purposes. It involves precise timing of press and release events to trigger the double-click detection. ```cpp void testDoubleClick(OneButton &btn) { pressButton(btn); delay(50); releaseButton(btn); delay(100); // Brief interval between clicks pressButton(btn); delay(50); releaseButton(btn); } ``` -------------------------------- ### OneButton Status Query Functions Source: https://github.com/mathertel/onebutton/blob/master/README.md Functions to retrieve the current status, timing, and configuration of a OneButton instance. ```APIDOC ## GET /isLongPressed ### Description Detect whether or not the button is currently inside a long press. ### Response - **bool** - Returns true if the button is in a long press state, false otherwise. ## GET /getPressedMs ### Description Get the current number of milliseconds that the button has been held down for. ### Response - **int** - The duration in milliseconds. ## GET /pin ### Description Get the OneButton pin. ### Response - **int** - The pin number associated with the button. ## GET /state ### Description Get the OneButton state. ### Response - **int** - The current state of the button. ## GET /debouncedValue ### Description Get the OneButton debounced value. ### Response - **int** - The current debounced value. ``` -------------------------------- ### attachClick - Single Click Event Source: https://context7.com/mathertel/onebutton/llms.txt Shows how to attach a callback function to handle single click events detected by the OneButton library. ```APIDOC ## attachClick - Single Click Event Attaches a callback function that fires when a single click is detected. The library waits for the click timeout period to confirm no double-click follows before triggering this event. ```cpp #include OneButton button(2, true, true); int ledState = LOW; void handleClick() { Serial.println("Single click detected!"); ledState = !ledState; digitalWrite(13, ledState); } void setup() { Serial.begin(115200); pinMode(13, OUTPUT); // Attach using function pointer button.attachClick(handleClick); // Alternative: attach using lambda // button.attachClick([]() { Serial.println("Clicked!"); }); } void loop() { button.tick(); } ``` ``` -------------------------------- ### OneButton State Management Source: https://github.com/mathertel/onebutton/blob/master/README.md Methods for updating the button logic and resetting internal states. ```APIDOC ## POST /tick ### Description Updates the button logic. You can specify a logic level to skip reading the pin and use that level instead. ### Parameters #### Request Body - **level** (bool) - Optional - The logic level to use instead of reading the pin. ## POST /reset ### Description Resets the internal state of the button instance. ``` -------------------------------- ### Essential tick() Method Call in Loop Source: https://github.com/mathertel/onebutton/blob/master/README.md Illustrates the critical placement of the btn.tick() call within the main loop() function, which is required for the OneButton library to process events correctly. ```CPP void loop() { btn.tick(); // Do other things... } ``` -------------------------------- ### attachMultiClick - Multiple Clicks Event Source: https://context7.com/mathertel/onebutton/llms.txt Details how to attach a callback for detecting three or more consecutive clicks and retrieving the click count. ```APIDOC ## attachMultiClick - Multiple Clicks Event Attaches a callback for detecting three or more consecutive clicks. Use `getNumberClicks()` to retrieve the actual click count within the callback. ```cpp #include OneButton button(2, true, true); void handleMultiClick() { int clicks = button.getNumberClicks(); if (clicks == 3) { Serial.println("Triple click - Mode 1"); } else if (clicks == 4) { Serial.println("Quadruple click - Mode 2"); } else { Serial.print("Multi-click detected: "); Serial.print(clicks); Serial.println(" clicks"); } } void setup() { Serial.begin(115200); button.attachMultiClick(handleMultiClick); } void loop() { button.tick(); } ``` ``` -------------------------------- ### Event Attachment Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Attach callback functions to specific button events such as clicks, double clicks, and long presses. ```cpp // Single click btn.attachClick(handleClick); btn.attachClick(handleClickWithParam, (void*)myData); // Double click btn.attachDoubleClick(handleDoubleClick); // Press events btn.attachPress(handlePress); // Long press btn.attachLongPressStart(handleLongPressStart); btn.attachLongPressStop(handleLongPressStop); // Multi-click (requires specific configuration) btn.attachMultiClick(handleMultiClick); ``` -------------------------------- ### Attach Multi-Click Event Handler Source: https://context7.com/mathertel/onebutton/llms.txt Attaches a callback for detecting three or more consecutive clicks. Use getNumberClicks() within the callback to determine the exact number of clicks. ```cpp #include OneButton button(2, true, true); void handleMultiClick() { int clicks = button.getNumberClicks(); if (clicks == 3) { Serial.println("Triple click - Mode 1"); } else if (clicks == 4) { Serial.println("Quadruple click - Mode 2"); } else { Serial.print("Multi-click detected: "); Serial.print(clicks); Serial.println(" clicks"); } } void setup() { Serial.begin(115200); button.attachMultiClick(handleMultiClick); } void loop() { button.tick(); } ``` -------------------------------- ### Attach Single Click Event Handler Source: https://context7.com/mathertel/onebutton/llms.txt Attaches a callback function to handle single click events. The library waits for the click timeout to confirm no double-click occurs before triggering this. ```cpp #include OneButton button(2, true, true); int ledState = LOW; void handleClick() { Serial.println("Single click detected!"); ledState = !ledState; digitalWrite(13, ledState); } void setup() { Serial.begin(115200); pinMode(13, OUTPUT); // Attach using function pointer button.attachClick(handleClick); // Alternative: attach using lambda // button.attachClick([]() { Serial.println("Clicked!"); }); } void loop() { button.tick(); } ``` -------------------------------- ### Checking Button State Source: https://github.com/mathertel/onebutton/blob/master/copilot-instructions.md Use boolean methods to check the current physical state of the button. ```cpp // Active state (requires getPressedMs() or related methods) if (btn.isPressed()) { } // Button currently held down if (btn.isLongPressed()) { } // Long press is active ``` -------------------------------- ### Interrupt-Based Detection Source: https://context7.com/mathertel/onebutton/llms.txt Use hardware interrupts for more responsive button detection, especially useful when the main loop has delays or heavy processing. The interrupt service routine should be kept minimal. ```cpp #include #define PIN_INPUT 2 #define PIN_LED 13 OneButton button(PIN_INPUT, true, true); int ledState = LOW; // Interrupt service routine - keep minimal, no Serial! #if defined(ESP32) void IRAM_ATTR checkTicks() { button.tick(); } #elif defined(ESP8266) ICACHE_RAM_ATTR void checkTicks() { button.tick(); } #else void checkTicks() { button.tick(); } #endif void singleClick() { Serial.println("Single click"); } void doubleClick() { ledState = !ledState; digitalWrite(PIN_LED, ledState); Serial.println("Double click - LED toggled"); } void setup() { Serial.begin(115200); pinMode(PIN_LED, OUTPUT); // Attach interrupt for immediate state changes attachInterrupt(digitalPinToInterrupt(PIN_INPUT), checkTicks, CHANGE); button.attachClick(singleClick); button.attachDoubleClick(doubleClick); button.setPressMs(1000); button.attachLongPressStart([]() { Serial.println("Long press!"); }); } void loop() { // Still call tick() as backup button.tick(); // Can have delays without missing button events delay(100); } ``` -------------------------------- ### Attach Double Click Event Handler Source: https://context7.com/mathertel/onebutton/llms.txt Attaches a callback function for double click events, which occur within the default 400ms timeout. Single click detection is delayed when a double click handler is active. ```cpp #include OneButton button(2, true, true); void handleDoubleClick() { Serial.println("Double click detected!"); } void setup() { Serial.begin(115200); button.attachDoubleClick(handleDoubleClick); // Or use lambda button.attachDoubleClick([]() { Serial.println("Double Pressed!"); }); } void loop() { button.tick(); } ``` -------------------------------- ### Attach Continuous Long Press Callback Source: https://context7.com/mathertel/onebutton/llms.txt Registers a function to execute repeatedly while a button is held down. Use setLongPressIntervalMs to define the frequency of the callback. ```cpp #include OneButton button(2, true, true); int brightness = 0; void handleDuringLongPress() { brightness = min(brightness + 5, 255); analogWrite(9, brightness); Serial.print("Brightness: "); Serial.println(brightness); } void setup() { Serial.begin(115200); pinMode(9, OUTPUT); button.attachDuringLongPress(handleDuringLongPress); button.setLongPressIntervalMs(100); // Call every 100ms during hold } void loop() { button.tick(); } ``` -------------------------------- ### Querying Button State Source: https://context7.com/mathertel/onebutton/llms.txt Query the current button state for conditional logic in your main loop or other parts of your application. Functions like isIdle() and isLongPressed() provide insights into the button's current status. ```cpp #include OneButton button(2, true, true); void setup() { Serial.begin(115200); button.attachLongPressStart([]() { Serial.println("Entering adjustment mode..."); }); button.attachLongPressStop([]() { Serial.print("Adjustment complete. Duration: "); Serial.print(button.getPressedMs()); Serial.println(" ms"); }); } void loop() { button.tick(); // Check if button is idle (no interaction in progress) if (button.isIdle()) { // Safe to enter low-power mode } // Check if currently in long press state if (button.isLongPressed()) { // Perform continuous action while held Serial.print("Holding: "); Serial.print(button.getPressedMs()); Serial.println(" ms"); } // Get debug information // button.pin() - Returns the configured pin number // button.state() - Returns internal FSM state // button.debouncedValue() - Returns current debounced input level delay(50); } ``` -------------------------------- ### Attach Long Press Release Callback Source: https://context7.com/mathertel/onebutton/llms.txt Registers a callback for when a button is released after a long press, utilizing getPressedMs to determine the duration of the hold. ```cpp #include OneButton button(2, true, true); void LongPressStart(void *oneButton) { Serial.print(((OneButton *)oneButton)->getPressedMs()); Serial.println(" ms - Long press started"); } void LongPressStop(void *oneButton) { Serial.print(((OneButton *)oneButton)->getPressedMs()); Serial.println(" ms - Long press ended"); } void DuringLongPress(void *oneButton) { Serial.print(((OneButton *)oneButton)->getPressedMs()); Serial.println(" ms - Still pressing..."); } void setup() { Serial.begin(115200); // Attach with parameter (button reference) button.attachLongPressStart(LongPressStart, &button); button.attachDuringLongPress(DuringLongPress, &button); button.attachLongPressStop(LongPressStop, &button); button.setLongPressIntervalMs(1000); // Report every second } void loop() { button.tick(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.