Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //IO
- #define BTN1 2
- #define BTN2 3
- bool presentButton1, presentButton2; //actual state of buttons and inputs
- bool lastButton1, lastButton2; //last state button s
- bool edgeButton1, edgeButton1; //edge state buttons
- bool LEDstatus;
- void setup() {
- //IO
- //there is internal pull-up used - read variables are negated
- pinMode(BTN1, INPUT_PULLUP);
- pinMode(BTN2, INPUT_PULLUP);
- pinMode(LED_BUILTIN, OUTPUT);
- }
- void loop() {
- //store input to variarbe (and nagete because pullups are used)
- presentButton1 = !digitalRead(BTN1);
- presentButton2 = !digitalRead(BTN2);
- //edge detection
- edgeButton1 = (presentButton1 ^ lastButton1) & !presentButton1;
- edgeButton2 = (presentButton2 ^ lastButton2) & !presentButton2;
- if (edgeButton1 && LEDstatus == 0) {
- digitalWrite(LED_BUILTIN, HIGH);
- LEDstatus = 1;
- }
- if (edgeButton2 && LEDstatus == 0) {
- digitalWrite(LED_BUILTIN, HIGH);
- LEDstatus = 1;
- }
- if (LEDstatus == 1) {
- delay(2000);
- digitalWrite(LED_BUILTIN, LOW);
- LEDstatus = 0;
- }
- lastButton1 = presentButton1; //save current state to last state
- lastButton2 = presentButton2; //save current state to last state
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement