Basic LED sequence program

Hello everybody,

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!!

If your switch is wired correctly, it should.

"It doesn't work" is pretty vague. Do the red LEDs not go off? Do the yellos LEDs not come on? What happens, or doesn't happen?

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.

I presume that is not what you want happening.

So you want the red LEDs to be on and when the button is pushed the red turn off and the yellow turn on?

Do you want the yellow to stay on after the button is pressed or do you want them to stay on only as long at the button is being pushed?

Thanks for the replies

Sorry about being rather vague.

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.

Thanks in advance! :slight_smile:

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.

There is a button library that will make dealing with the switch stuff a lot easier for you:
http://www.arduino.cc/playground/Code/Button

Keeping track of the LEDs that are lit requires defining a boolean variable, and setting it true of the red LEDs are on, and false if they are off.