Skip to content

Instantly share code, notes, and snippets.

@kaworu
Last active October 19, 2018 06:58
Show Gist options
  • Save kaworu/fa931c68b290b711cc4e399c1fcaa66c to your computer and use it in GitHub Desktop.
Save kaworu/fa931c68b290b711cc4e399c1fcaa66c to your computer and use it in GitHub Desktop.
/*
* simple led / push button based Arduino puzzle.
*/
#include <Arduino.h>
#include "Led.hh"
#include "Button.hh"
Led internal(13);
Led lamp[] = { Led(3), Led(4), Led(5), Led(7), Led(8) };
const int nlamp = sizeof(lamp) / sizeof(*lamp);
Button button[] = { Button(10), Button(11), Button(12) };
const int nbutton = sizeof(button) / sizeof(*button);
void setup() {
internal.turn_on();
// init
internal.turn_off();
}
void loop() {
const int btn0 = (button[0].probe() == Button::CHANGED_AND_PRESSED);
const int btn1 = (button[1].probe() == Button::CHANGED_AND_PRESSED);
const int btn2 = (button[2].probe() == Button::CHANGED_AND_PRESSED);
/* toggle 0, 1, 2 if button 0, 1, 2 is pressed (respectively) */
if (btn0)
lamp[0].toggle();
if (btn1)
lamp[1].toggle();
if (btn2)
lamp[2].toggle();
/* toggle 3 if any of the button is pressed */
if (btn0 | btn1 | btn2)
lamp[3].toggle();
/* toggle 4 if more than one button is pressed */
if (btn0 + btn1 + btn2 > 1)
lamp[4].toggle();
delay(100); /* FIXME */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment