Skip to content

Instantly share code, notes, and snippets.

@rafacouto
Created July 31, 2014 22:41
Show Gist options
  • Save rafacouto/bb762b3f1f72fc651281 to your computer and use it in GitHub Desktop.
Save rafacouto/bb762b3f1f72fc651281 to your computer and use it in GitHub Desktop.
State button and flashing led
const int LED_PIN = 3;
const int BTN_PIN = 4;
const int FLASH_MILLIS = 100;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT);
}
bool button_state = false;
bool led_flash = false;
bool led_state = false;
int last_change = 0;
void loop() {
int now = millis();
if (digitalRead(BTN_PIN) == HIGH ^ button_state)
{
button_state = !button_state;
if (button_state)
{
led_flash = !led_flash;
delay(100);
}
}
if (led_flash || led_state)
{
if (now - last_change > FLASH_MILLIS)
{
led_state = !led_state;
digitalWrite(LED_PIN, led_state ? HIGH : LOW);
last_change = now;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment