EDIT: a third design option would be to add a run method on each button and, instead of only running the tested "pseudo-event" method, save the current state and event in variables, testing those variables in loop() (property button.wasPressed instead of method button.wasPressed(), for example).
EDIT: a third design option would be to add a run method on each button and, instead of only running the tested "pseudo-event" method, save the current state and event in variables, testing those variables in loop() (property button.wasPressed instead of method button.wasPressed(), for example).
How to detect thatif more than one button was pressed
PiscaPisca.cpp (the state-machine itself)
#include "Arduino.h" #include "PwmPin.cpp" #include "Beeper.cpp" typedef enum { NONE = 0, RIGHT_LIGHT = 1, LEFT_LIGHT = 2, BOTH = 3 }; class PiscaPisca { PwmPin _left; PwmPin _right; Beeper _beeper; long _timeReference = 0; const int PERIOD = 350; boolean _running = false, _lightState = false; int _sides_to_turn = NONE; public : void configure(int leftPin, int rightPin, int buzzerPin) { _left.configure(leftPin); _right.configure(rightPin); _beeper.configure(buzzerPin); } public : void run() { evaluateBlink(); } public : void toggleLeft() { checkRestart(LEFT_LIGHT); } public : void toggleRight() { checkRestart(RIGHT_LIGHT); } void checkRestart(int lightSide) { _timeReference = 0; // some clever bit-twiddling can never hurt too much: _sides_to_turn = lightSide & ~_sides_to_turn; Serial.println(lightSide); Serial.println(_sides_to_turn); if (_sides_to_turn > 0) { _running = true; } else { lightsOff(); _running = false; _lightState = false; } } void evaluateBlink() { if (!_running) { return; } else { long currentMillis = millis(); if (currentMillis - _timeReference > PERIOD) { _timeReference = currentMillis; _lightState = !_lightState; performBlink(); } } } void performBlink() { if (_lightState) { _beeper.beepIn(); lightsOn(); } else { _beeper.beepOut(); lightsOff(); } } void lightsOn() { if (isLightSet(LEFT_LIGHT)) { _left.on(); } if (isLightSet(RIGHT_LIGHT)) { _right.on(); } } boolean isLightSet(int lightSide) { return (_sides_to_turn & lightSide) == lightSide; } void lightsOff() { _left.off(); _right.off(); } }; PiscaPisca.cpp (the state-machine itself)
#include "Arduino.h" #include "PwmPin.cpp" #include "Beeper.cpp" typedef enum { NONE = 0, RIGHT_LIGHT = 1, LEFT_LIGHT = 2, BOTH = 3 }; class PiscaPisca { PwmPin _left; PwmPin _right; Beeper _beeper; long _timeReference = 0; const int PERIOD = 350; boolean _running = false, _lightState = false; int _sides_to_turn = NONE; public : void configure(int leftPin, int rightPin, int buzzerPin) { _left.configure(leftPin); _right.configure(rightPin); _beeper.configure(buzzerPin); } public : void run() { evaluateBlink(); } public : void toggleLeft() { checkRestart(LEFT_LIGHT); } public : void toggleRight() { checkRestart(RIGHT_LIGHT); } void checkRestart(int lightSide) { _timeReference = 0; // some clever bit-twiddling can never hurt too much: _sides_to_turn = lightSide & ~_sides_to_turn; Serial.println(lightSide); Serial.println(_sides_to_turn); if (_sides_to_turn > 0) { _running = true; } else { lightsOff(); _running = false; _lightState = false; } } void evaluateBlink() { if (!_running) { return; } else { long currentMillis = millis(); if (currentMillis - _timeReference > PERIOD) { _timeReference = currentMillis; _lightState = !_lightState; performBlink(); } } } void performBlink() { if (_lightState) { _beeper.beepIn(); lightsOn(); } else { _beeper.beepOut(); lightsOff(); } } void lightsOn() { if (isLightSet(LEFT_LIGHT)) { _left.on(); } if (isLightSet(RIGHT_LIGHT)) { _right.on(); } } boolean isLightSet(int lightSide) { return (_sides_to_turn & lightSide) == lightSide; } void lightsOff() { _left.off(); _right.off(); } }; Loading
lang-cpp