I recently got a larger, better breadboard and I've been trying to make an LED sequence of 3 reds switch to the three yellows when my button is pressed.
I have tried multiple thing; however, I am just a beginner with the Arduino and its programming, so I really have no clue where to start.
I have tried setting it like this:
int ledsPinRed = 12;
int ledsPinAmb = 11;
int buttonPin = 4;
int buttonState;
void setup()
{
pinMode(ledsPinRed, OUTPUT);
pinMode(ledsPinAmb, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
digitalWrite (ledsPinRed, HIGH);
delay(8000);
}
else
{
digitalWrite (ledsPinRed, LOW);
}
if (buttonState == HIGH)
{
digitalWrite (ledsPinAmb, HIGH);
}
else
{
digitalWrite (ledsPinAmb, LOW);
}
}
I know, it's terrible
But this doesn't work - so any help would be greatly appreciated. Thanks!!
OK. I read that closer. You are turning both the yellow and red LEDs on when the button is pressed. The red LEDs come on, then there is a 8 second delay, then the yellow LEDs are turned on.
Basically, I press the button, (2 of the 3 LEDs come on, that's probably my fault), and after 8 seconds they turn off, but then when I press it again, I want the yellow LEDs to turn on.
However, the delay is just for testing purposes, I do want the to stay on until the button is pressed again.
You'll need to read the switch state each pass through loop, and compare the current state to the previous state, and do something only if the states are not the same. This means that you'll need to keep track of the previous button state.
You'll need to debounce the switch, too.
When the switch is pressed, and it was not before, and its not a spurious press, turn off the LEDs that are on, and turn on the LEDs that are off. This means that you need to keep track of which LEDs are on, at any given time.