toggle between 2 leds with 1 momentary swith

Hello all! I am a noob to the codding world, but my background is electronic engineering, so i am keen to learn.

This problem is urgent, so any help would be appreciated.

I have written some very basic code that does every thing i need with the exception of one thing.

I have a UNO with one momentary button and 2 leds, one green, the other red.

The code currently works as follows:

1/- At power up, the green light is in the on state and remains that way.

2/- When the momentary switch is held on, the green light goes out and the red light comes on.

3/- If the switch is released, the red light goes out and the green light is restored.

The problem that i have hit now is that i need only the red led to continually flash when it is active. The green led must remain non flashing or constant, when its active. i have tried several versions of code and looked around online for something that would help, but i am struggling.

As this is urgent, i would be grateful for any help.

/*
  





 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPingreen =  9;      // the number of the LED pin
const int ledPinred =  8;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPingreen, OUTPUT);
  pinMode(ledPinred, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPingreen, LOW);
    digitalWrite(ledPinred, HIGH);   
  } else {
    // turn LED off:
    digitalWrite(ledPingreen, HIGH);
    digitalWrite(ledPinred, LOW);   
  }
}

Why is this urgent?

Don’t see any code that you tried where flashing is attempted.

.

The millis() function in the arduino API returns the number of milliseconds since the board was last reset. It's always ticking over. You can use this do do flashing.

Here's a quickie 4Hz flasher, FWIW:

const byte led = 13; 

void setup()
{
  pinMode(led,OUTPUT);
}

void loop()
{
  static unsigned long fStart;
  static bool flash;
  byte fInterval = 125;
  if(millis() - fStart > fInterval)
  {
    flash = !flash;
    fStart += fInterval;
  }
  digitalWrite(led,flash);      
}

You could put 756E6C's blink without delay style code in where you turn the red led on.

OTOH, maybe you don't want to stand with your finger on the button so the code below implements two discrete states, ST_GREEN and ST_RED and a click of the button toggles between the two. In each state it does the right stuff with the leds, but checks for the button's state to change ala this example.

My button is from pin to ground with INPUT_PULLUP so the logic is upside-down from yours: pressed is LOW.

//http://forum.arduino.cc/index.php?topic=466368.0

#define greenLed 2
#define redLed 3
#define button 14 //just convenient to be on the analog side

bool buttonVal;
bool prevButtonVal;
unsigned long previousMillisRedLed;
int redLedInterval = 500; //ms
bool redLedState;

enum {ST_GREEN, ST_RED} currentState = ST_GREEN; //so ST_GREEN is 0, ST_RED is 1 in the switch..case

//comment next line in or out to get serial prints
#define DEBUG

void setup()
{
  // put your setup code here, to run once:
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(button, INPUT_PULLUP); //active low, button from pin to ground

#ifdef DEBUG
  Serial.begin(9600);
  Serial.println("setup");
#endif

}//setup

void loop()
{

  buttonVal = digitalRead(button);

  // put your main code here, to run repeatedly:
  switch (currentState)
  {
    case (ST_GREEN):
      //solid on
      digitalWrite(greenLed, HIGH);

      //check if button is pressed to take us out of this state
      if (buttonVal != prevButtonVal) //button changed...
      {
        if (!buttonVal) //.... to pressed
        {
#ifdef DEBUG
          Serial.println("going to red");
#endif
          digitalWrite(greenLed, LOW);
          currentState = ST_RED;
        }//button pressed
      }//button changed
      prevButtonVal = buttonVal;
      break;

    case (ST_RED):
      //blink
      if (millis() - previousMillisRedLed >= redLedInterval)
      {
        redLedState = !redLedState;
        digitalWrite(redLed, redLedState);
        previousMillisRedLed = millis();
      }

      //check if button is pressed to take us out of this state
      if (buttonVal != prevButtonVal) //button changed...
      {
        if (!buttonVal) //.... to pressed
        {
#ifdef DEBUG
          Serial.println("going to green");
#endif
          digitalWrite(redLed, LOW);
          currentState = ST_GREEN;
        }//button pressed
      }//button changed
      prevButtonVal = buttonVal;
      break;

  }//switch


}//loop