This project is proudly brought to you by the team at AvantMaker.com.
Visit us at AvantMaker.com where we've crafted a comprehensive collection of Reference and Tutorial materials for the ESP32, a mighty microcontroller that powers countless IoT creations.
AvantDigitalRead is an advanced digital input management library for ESP32, enhancing the standard Arduino digitalRead functionality. It provides multi-pin management, event detection, and gesture recognition, making it ideal for complex interactive scenarios such as user interfaces and sensor data acquisition.
- Multi-pin Management: Dynamically add and remove pins for unified control.
- Built-in Debouncing: Customizable debounce time for stable readings.
- Rich Event Detection: Detects state changes, rising edges, and falling edges.
- Advanced Button Gestures: Recognizes single-press, double-press, and long-press events with configurable timings and repeat options.
- Delayed Callbacks: Supports delayed execution of callback functions.
- Non-Blocking Design: Ensures the main program flow is not interrupted.
- Unified Callback Format: Simplifies code and reduces the learning curve.
- Open the Arduino IDE.
- Go to
Tools>Manage Libraries.... - Search for
AvantDigitalRead. - Click the
Installbutton.
- Download the ZIP file of this repository.
- Open the Arduino IDE.
- Go to
Sketch>Include Library>Add .ZIP Library.... - Select the downloaded ZIP file.
Copy the AvantDigitalRead.h and AvantDigitalRead.cpp files from the src folder into your project folder.
Here is a simple example demonstrating how to use the AvantDigitalRead library to detect button events (the button is attached to GPIO Pin 5 of ESP32):
#include "AvantDigitalRead.h"
// Define the pin to monitor
#define BUTTON_PIN 5
// Create an instance of AvantDigitalRead
AvantDigitalRead pinManager;
void setup() {
// Initialize serial communication
Serial.begin(115200);
while (!Serial) {
; // Wait for serial port to connect
}
// Print welcome message
Serial.println("BasicButtonMonitor Example Starting...");
Serial.print("Monitoring pin for button presses: ");
Serial.println(BUTTON_PIN);
Serial.println("Press the button to see single/double/long press notifications.");
Serial.println("----------------------------------------");
// Initialize the pin to monitor
if (pinManager.addPin(BUTTON_PIN, INPUT_PULLUP)) {
Serial.println("Pin initialized successfully");
} else {
Serial.println("Failed to initialize pin");
while (1) {
delay(100); // Halt execution if pin initialization fails
}
}
// Set debounce time to prevent false triggers (30 milliseconds)
pinManager.setDebounceTime(BUTTON_PIN, 30);
// Register the callback functions for single, double, and long press events
pinManager.onSinglePress(BUTTON_PIN, singlePressCallback);
pinManager.onDoublePress(BUTTON_PIN, doublePressCallback);
pinManager.onLongPress(BUTTON_PIN, longPressCallback);
Serial.println("Ready for input.");
Serial.println("----------------------------------------");
}
void loop() {
// Must call update() regularly to process events
pinManager.update();
// Small delay to prevent excessive CPU usage
delay(10);
}
// Callback function for single press events
void singlePressCallback(int pin, PinState newState, PinState oldState,
EventType event, unsigned long timestamp) {
// Print pin information
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" SINGLE PRESS detected: ");
Serial.print(oldState);
Serial.print(" -> ");
Serial.print(newState);
// Print timestamp
Serial.print(" at ");
Serial.print(timestamp);
Serial.println(" ms");
// Additional information
Serial.println(" Event: Button pressed and released once");
}
// Callback function for double press events
void doublePressCallback(int pin, PinState newState, PinState oldState,
EventType event, unsigned long timestamp) {
// Print pin information
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" DOUBLE PRESS detected: ");
Serial.print(oldState);
Serial.print(" -> ");
Serial.print(newState);
// Print timestamp
Serial.print(" at ");
Serial.print(timestamp);
Serial.println(" ms");
// Additional information
Serial.println(" Event: Button pressed and released twice in quick succession");
}
// Callback function for long press events
void longPressCallback(int pin, PinState newState, PinState oldState,
EventType event, unsigned long timestamp) {
// Print pin information
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" LONG PRESS detected: ");
Serial.print(oldState);
Serial.print(" -> ");
Serial.print(newState);
// Print timestamp
Serial.print(" at ");
Serial.print(timestamp);
Serial.println(" ms");
// Additional information
Serial.println(" Event: Button held down for extended duration");
}addPin(int pin, int mode): Initializes a specified pin and adds it to the management list.removePin(int pin): Removes a pin from the management list and releases its resources.isInitialized(int pin): Checks if a pin has been initialized.getPinMode(int pin): Gets the input mode of a specified pin.readPin(int pin): Reads the current state of a specified pin.
setDebounceTime(int pin, unsigned long debounceMs): Sets the debounce time for a specified pin.getDebounceTime(int pin): Gets the debounce time setting for a specified pin.
onChange(int pin, PinCallback callback, unsigned long delayMs = 0): Sets the callback function for pin state changes.onRising(int pin, PinCallback callback, unsigned long delayMs = 0): Sets the callback function for rising edges.onFalling(int pin, PinCallback callback, unsigned long delayMs = 0): Sets the callback function for falling edges.
onSinglePress(int pin, PinCallback callback, unsigned long delayMs = 0): Sets the callback function for single-press detection.setClickParameters(int pin, unsigned long minPressMs = 50, unsigned long maxPressMs = 300): Sets the parameters for single/double-press detection.onDoublePress(int pin, PinCallback callback, unsigned long delayMs = 0, unsigned long maxIntervalMs = 500): Sets the callback function for double-press detection.onLongPress(int pin, PinCallback callback, unsigned long delayMs = 0, unsigned long pressDurationMs = 1000, bool repeat = false): Sets the callback function for long-press detection.
enablePinEvents(int pin): Enables all event detection for a specified pin.disablePinEvents(int pin): Disables all event detection for a specified pin.enableAllEvents(): Enables event detection for all initialized pins.disableAllEvents(): Disables event detection for all pins.
update(): Processes the state detection and event triggering for all pins. Must be called regularly inloop().
- All pins must be initialized with
addPin()before use. - The
update()function must be called regularly inloop(), preferably with an interval of no more than 50ms. - Callback functions should be kept as short as possible to avoid delaying other operations.
- For high-precision detection, it is recommended to reduce the
update()call interval. - In memory-constrained environments,
removePin()can be used to release resources for pins that are no longer needed.
This library is released under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please feel free to submit issues and pull requests.
💡 Check out our other ESP32 libraries at AvantMaker GitHub!
