- Нажата черная кнопка на контакте D6
- Нажата красная кнопка на контакте D7.
- Кнопки кабины нажаты не менее 2 секунд.
Я использую библиотеку Bounce2 для лучшего распознавания сигналов. Это прекрасно работает.
Вот мой отдельный класс ButtonController:
ButtonController.h
#include
#include
class ButtonController {
public:
using Callback = std::function;
ButtonController(int pinBlack, int pinRed, int comboDelay = 2000);
void begin();
void update();
// Event-Handler-Setter
void onButtonBlackPressed(Callback callback);
void onButtonRedPressed(Callback callback);
void onCombinationPressed(Callback callback);
private:
int pinBlack;
int pinRed;
int gracePeriodStart = 0;
unsigned long comboDelay;
void resetFlags();
Bounce buttonBlack;
Bounce buttonRed;
// Timestamp for combination recognition
unsigned long comboStartTime = 0;
// callback-functions
Callback blackPressedCallback;
Callback redPressedCallback;
Callback comboPressedCallback;
};
ButtonController.cpp
#include "ButtonController.h"
ButtonController::ButtonController(int pinBlack, int pinRed, int comboDelay)
: pinBlack(pinBlack), pinRed(pinRed), comboDelay(comboDelay) {}
void ButtonController::begin() {
pinMode(pinBlack, INPUT_PULLUP);
pinMode(pinRed, INPUT_PULLUP);
// Initialize Bounce-Objects
buttonBlack.attach(pinBlack);
buttonRed.attach(pinRed);
buttonBlack.interval(50); // Debounce-Zeit in ms
buttonRed.interval(50);
}
void ButtonController::update() {
// Update Bounce-Objects
buttonBlack.update();
buttonRed.update();
bool blackPressed = buttonBlack.read() == LOW;
bool redPressed = buttonRed.read() == LOW;
// When a button is pressed, a “Grace Period” time window starts
if (buttonBlack.fell()) {
gracePeriodStart = millis();
}
if (buttonRed.fell()) {
gracePeriodStart = millis();
}
// Check whether both buttons are pressed within the time window of 200 ms
if (blackPressed && redPressed && (millis() - gracePeriodStart = 5000) {
// If both buttons remain pressed for 5 seconds, trigger combined event
if (comboPressedCallback) {
comboPressedCallback();
}
comboStartTime = 0; // reset timer
gracePeriodStart = 0; // reset Grace Period
}
} else {
// If one of the buttons is released before the combination event is triggered, reset
comboStartTime = 0;
// Trigger individual events only if the combined event is not running
if (buttonBlack.fell() && !redPressed) {
if (blackPressedCallback) {
blackPressedCallback();
}
}
if (buttonRed.fell() && !blackPressed) {
if (redPressedCallback) {
redPressedCallback();
}
}
}
}
void ButtonController::onButtonBlackPressed(Callback callback) {
blackPressedCallback = callback;
}
void ButtonController::onButtonRedPressed(Callback callback) {
redPressedCallback = callback;
}
void ButtonController::onCombinationPressed(Callback callback) {
comboPressedCallback = callback;
}
Вот как я использую этот класс в своем main.cpp
#include "ButtonController.h"
const int PIN_BUTTON_BLACK = D6;
const int PIN_BUTTON_RED = D7;
const int BUTTON_COMBO_DELAY = 2000;
ButtonController buttonController(PIN_BUTTON_BLACK, PIN_BUTTON_RED, BUTTON_COMBO_DELAY);
void handleBlackButtonPress() {
Serial.println("BLACK Button pressed");
}
void handleRedButtonPress() {
Serial.println("RED Button pressed");
}
void handleCombinationPress() {
Serial.println("Combo-Event");
}
void setup() {
Serial.begin(9600);
buttonController.begin();
buttonController.onButtonBlackPressed(handleBlackButtonPress);
buttonController.onButtonRedPressed(handleRedButtonPress);
buttonController.onCombinationPressed(handleCombinationPress);
}
void loop() {
buttonController.update();
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ombo-event