Advertisement
Pavel1TU

Tlačítka

Jan 27th, 2023
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //IO
  3. #define BTN1 2
  4. #define BTN2 3
  5.  
  6. bool presentButton1, presentButton2; //actual state of buttons and inputs
  7. bool lastButton1, lastButton2; //last state button s
  8. bool edgeButton1, edgeButton1; //edge state buttons
  9.  
  10. bool LEDstatus;
  11.  
  12. void setup() {
  13.  
  14.   //IO
  15.   //there is internal pull-up used - read variables are negated
  16.   pinMode(BTN1, INPUT_PULLUP);
  17.   pinMode(BTN2, INPUT_PULLUP);
  18.  
  19.   pinMode(LED_BUILTIN, OUTPUT);
  20.  
  21. }
  22.  
  23. void loop() {
  24.   //store input to variarbe (and nagete because pullups are used)
  25.   presentButton1 = !digitalRead(BTN1);
  26.   presentButton2 = !digitalRead(BTN2);
  27.  
  28.  
  29.   //edge detection
  30.   edgeButton1 = (presentButton1 ^ lastButton1) & !presentButton1;
  31.   edgeButton2 = (presentButton2 ^ lastButton2) & !presentButton2;
  32.  
  33.  
  34.     if (edgeButton1 && LEDstatus == 0) {
  35.     digitalWrite(LED_BUILTIN, HIGH);
  36.     LEDstatus = 1;
  37.     }
  38.     if (edgeButton2 && LEDstatus == 0) {
  39.     digitalWrite(LED_BUILTIN, HIGH);
  40.     LEDstatus = 1;
  41.     }
  42.     if (LEDstatus == 1) {
  43.     delay(2000);
  44.     digitalWrite(LED_BUILTIN, LOW);
  45.     LEDstatus = 0;
  46.  
  47.     }
  48.  
  49.   lastButton1 = presentButton1; //save current state to last state
  50.   lastButton2 = presentButton2; //save current state to last state
  51.  
  52.  
  53. }
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement