Guest User

Untitled

a guest
Oct 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. /*
  2. * simple led / push button based Arduino puzzle.
  3. */
  4. #include <Arduino.h>
  5.  
  6. #include "Led.hh"
  7. #include "Button.hh"
  8.  
  9. Led internal(13);
  10.  
  11. Led lamp[] = { Led(3), Led(4), Led(5), Led(7), Led(8) };
  12. const int nlamp = sizeof(lamp) / sizeof(*lamp);
  13.  
  14. Button button[] = { Button(10), Button(11), Button(12) };
  15. const int nbutton = sizeof(button) / sizeof(*button);
  16.  
  17. void setup() {
  18. internal.turn_on();
  19. // init
  20. internal.turn_off();
  21. }
  22.  
  23.  
  24. void loop() {
  25. const int btn0 = (button[0].probe() == Button::CHANGED_AND_PRESSED);
  26. const int btn1 = (button[1].probe() == Button::CHANGED_AND_PRESSED);
  27. const int btn2 = (button[2].probe() == Button::CHANGED_AND_PRESSED);
  28.  
  29. /* toggle 0, 1, 2 if button 0, 1, 2 is pressed (respectively) */
  30. if (btn0)
  31. lamp[0].toggle();
  32. if (btn1)
  33. lamp[1].toggle();
  34. if (btn2)
  35. lamp[2].toggle();
  36.  
  37. /* toggle 3 if any of the button is pressed */
  38. if (btn0 | btn1 | btn2)
  39. lamp[3].toggle();
  40.  
  41. /* toggle 4 if more than one button is pressed */
  42. if (btn0 + btn1 + btn2 > 1)
  43. lamp[4].toggle();
  44.  
  45. delay(100); /* FIXME */
  46. }
Add Comment
Please, Sign In to add comment