Two Push Buttons and 3 Flashing LEDs

Hi Guys,

Any help is appreciated.

Currently when no button has been pressed a Red LED stays on. What I would Like to happen is the red LED flashes continuously, until a button is pressed then the command relating to that button proceeds to happen.

I have two codes, one where the red LED is stationary and doesn’t flash, but when a button is pressed one of the other commands proceeds to happen.

int  BlueButton    =  8  ;  //Telling What Pin they are in
int  RedButton    =  9  ;

int   BlueLED   =  10  ;
int   GreenLED    =  11  ;
int   RedLED    =  12  ;

int i = 0;              //variable used in for loop



// Setup - make sure this is complete

void setup()
{
  pinMode (  BlueButton  , INPUT);     //Telling Arduino what is what
  pinMode (  RedButton  , INPUT);

  pinMode (  BlueLED  , OUTPUT);
  pinMode (  GreenLED  , OUTPUT);
  pinMode (  RedLED  , OUTPUT);
}


void loop()
{
  if (digitalRead (BlueButton) == HIGH) //button1
  {
    digitalWrite (RedLED, LOW);                            //Turning REDLED off when button pushed
    for (i = 0; i < 10; i++)
    {
      digitalWrite (BlueLED, HIGH);                         //flash the blue LED 10 times using a for loop
      delay (500);
      digitalWrite (BlueLED, LOW);
      delay (500);
    }

  }

  if (digitalRead (RedButton) == HIGH) //button2          // if red button pushed
  {
    digitalWrite (RedLED, LOW);                           // REDLED off
    for (i = 0; i < 4; i++)
    {
      digitalWrite (BlueLED, HIGH);
      digitalWrite (GreenLED, LOW);
      delay (500);
      digitalWrite (BlueLED, LOW);
      digitalWrite (GreenLED, HIGH);
      delay (500);


    }
    digitalWrite (GreenLED, LOW);
  }

  else
  {
    digitalWrite (RedLED, HIGH);
  }
}

The other is my attempt at getting the red LED to flash when no button has been pressed. Here the blue LED just flashed continuously.

int  BlueButton    =  8  ;  //Tellint What Pin they are in
int  RedButton    =  9  ;

int   BlueLED   =  10  ;
int   GreenLED    =  11  ;
int   RedLED    =  12  ;

int i = 0;              //variable used in for loop



// Setup - make sure this is complete

void setup()
{
  pinMode (  BlueButton  , INPUT);     //Telling Arduino what is what
  pinMode (  RedButton  , INPUT);

  pinMode (  BlueLED  , OUTPUT);
  pinMode (  GreenLED  , OUTPUT);
  pinMode (  RedLED  , OUTPUT);
}


void loop()
{
  if (digitalRead (BlueButton) == HIGH) //button1
  {
    digitalWrite (RedLED, LOW);                            //Turning REDLED off when button pushed
    for (i = 0; i < 10; i++)
    {
      digitalWrite (BlueLED, HIGH);                         //flash the blue LED 10 times using a for loop
      delay (500);
      digitalWrite (BlueLED, LOW);
      delay (500);
    }

  }

  if (digitalRead (RedButton) == HIGH) //button2          // if red button pushed
  {
    digitalWrite (RedLED, LOW);                           // REDLED off
    for (i = 0; i < 4; i++)
    {
      digitalWrite (BlueLED, HIGH);
      digitalWrite (GreenLED, LOW);
      delay (500);
      digitalWrite (BlueLED, LOW);
      digitalWrite (GreenLED, HIGH);
      delay (500);


    }
    digitalWrite (GreenLED, LOW);
  }

  else

  {
    digitalWrite (RedLED, HIGH);
    delay (2000);
    digitalWrite (RedLED, LOW);
    delay(2000);
  }
}

I hope you can understand what I am trying to achieve, like I said any help is appreciated. If you could explain to me why it isn’t work, what the code actually means at the moment and come up with a solution, I will be extremely happy.

Thank you

If you could explain to me why it isn't work

The codes you posted DO work. That they do not do what you expect is NOT the same as the code not working.

What the code actually does, and what you expect, are undefined, so we can not explain why the code does not do what you want.

Using delay() to blink an LED is going to make the switches seem unresponsive. Look at the blink without delay example, or Robin2's "Several things at once" thread or the sticky at the top of this forum for how to NOT use delay() to blink an LED.

As Paul has pointed out you need to look into using millis() for timing as in the BlinkWithoutDelay example, Using millis() for timing. A beginners guide and Several things at the same time

Save the time (millis() value) when an event happens (the LED changes state) then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action (change the state of the LED)

PaulS:
The codes you posted DO work. That they do not do what you expect is NOT the same as the code not working.

Using delay() to blink an LED is going to make the switches seem unresponsive. Look at the blink without delay example, or Robin2's "Several things at once" thread or the sticky at the top of this forum for how to NOT use delay() to blink an LED.

UKHeliBob:
Save the time (millis() value) when an event happens (the LED changes state) then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action (change the state of the LED)

Thank you both for helping, really do appreciate it. But what if now I wanted the LED to be on more than it is off? At the moment, they are equal, blinking on and off equally for a total of the (previousMillis - currentMillis >= 1000).

Is there a way of adding another 'delay' after the LED has turned off to extend the time the LED is off without effecting the input of the buttons?

Thank you once again

But what if now I wanted the LED to be on more than it is off? At the moment, they are equal, blinking on and off equally for a total of the (previousMillis - currentMillis >= 1000).

Simple. Use a variable instead of a constant for the period and change its value when you change the state of the LED.