Using ATTiny85,
I am trying to turn ON 3 LEDs in sequence with 1s delay when I press the button. When I press the button again, I want to turn all 3 LEDs OFF simultaneously. And then, the cycle repeats again.
This is what I have: When I press the button for the first time, the initial sequence turning LEDS ON with 1s delay starts OK. But then, I can not turn LEDs OFF when I press the button again. Any help? I am not sure if I am supposed to write "delay(1000)" in setMode. Thanks.
const int LED1 = 0;
const int LED2 = 1;
const int LED3 = 2;
const int button = 3;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledMode = 0;
void setup ()
{
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
pinMode (button, INPUT);
}
boolean debounce (boolean last)
{
boolean current = digitalRead (button);
if (last != current)
{
delay (5);
current = digitalRead (button);
}
return current;
}
void setMode (int mode)
{
if (mode == 1)
{
digitalWrite (LED1, HIGH);
delay(1000);
digitalWrite (LED2, HIGH);
delay(1000);
digitalWrite (LED3, HIGH);
}
else if (mode == 2)
{
digitalWrite (LED1, LOW);
digitalWrite (LED2, LOW);
digitalWrite (LED3, LOW);
}
else
{
digitalWrite (LED1, LOW);
digitalWrite (LED2, LOW);
digitalWrite (LED3, LOW);
}
}
void loop ()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
ledMode++;
}
lastButton = currentButton;
if (ledMode == 2 ) ledMode = 0;
setMode (ledMode);
}