1

So I want to optimize this code and add a loop to the two red LEDs. The process is green LED is flashing, press button, yellow LED blink for 3 secs and then alternate flashing between the two red LEDs until the button is released.

 int switchState = 0;
    int green = 3;
    int yellow = 4;
    int red1 = 5;
    int red2 = 6;
    int button = 2;
    
    void setup()
    {
      pinMode(green, OUTPUT) ;
      pinMode(yellow, OUTPUT) ;
      pinMode(red1, OUTPUT) ;
      pinMode(red2, OUTPUT) ;
      pinMode(button, INPUT) ;
    }
    
    void loop()
    
    { //start void loop 1
      switchState = digitalRead(button);
    
      if (switchState == LOW)         //button not pressed
      {
        digitalWrite(green, HIGH);      //green LED on
        digitalWrite(yellow, LOW);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
    
        delay(500);
    
        digitalWrite(green, LOW);     //blink green LED
    
        delay(500);
      }//end if
    
      else        //Button pressed
      {
    
        digitalWrite(green, LOW);
        digitalWrite(yellow, HIGH);   //Blink yellow LED for 3 sec
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
    
        delay(250);
    
        digitalWrite(yellow, LOW);
    
        delay(250);
    
        digitalWrite(yellow, HIGH);
    
        delay(250);
    
        digitalWrite(yellow, LOW);
    
        delay(250);
    
        digitalWrite(yellow, HIGH);
    
        delay(250);
    
        digitalWrite(yellow, LOW);
    
        delay(250);
    
        digitalWrite(yellow, HIGH);
    
        delay(250);
    
        digitalWrite(yellow, LOW);
    
        delay(250);
    
        digitalWrite(yellow, HIGH);
    
        delay(250);
    
        digitalWrite(yellow, LOW);
    
        delay(250);
    
        digitalWrite(yellow, HIGH);
    
        delay(250);
    
        digitalWrite(yellow, LOW);
    
        delay(250);
    
        digitalWrite(yellow, HIGH);
    
        delay(250);
    
        digitalWrite(yellow, LOW);
    
        void loop()
        {
    
          digitalWrite(red1, HIGH);
          digitalWrite(red2, LOW);
    
          delay(250);
    
          digitalWrite(red1, LOW);
          digitalWrite(red2, HIGH);
    
          delay(250);
    
        }     //end void loop 2
    
      }     //end button press
    
    }     //end void loop 1
12
  • what is your question? – jsotola Oct 28 '20 at 7:34
  • what problems are you facing? – jsotola Oct 28 '20 at 7:35
  • 1
    Ok so I have a green LED that is blinking when my button is not pressed but when you press the button I want to have the yellow LED to flash for 3 seconds and then alternate flashing between the two red LEDs. – Gavin McLeod Oct 28 '20 at 8:34
  • 1
    there is two void loops cause i thought that it might do what I wanted but it did not so I came here from some help or guidance – Gavin McLeod Oct 28 '20 at 8:35
  • 1
    please, take a codding course – Juraj Oct 28 '20 at 10:18
0

You could use a for loop so you don't need to repeat the yellow blinking led code 7 times.

To keep the red leds alternating, you use a while loop.

int switchState = 0;
int green = 3;
int yellow = 4;
int red1 = 5;
int red2 = 6;
int button = 2;

void setup()
{
  pinMode(green, OUTPUT) ;
  pinMode(yellow, OUTPUT) ;
  pinMode(red1, OUTPUT) ;
  pinMode(red2, OUTPUT) ;
  pinMode(button, INPUT) ;
}

void loop()

{ //start void loop 1
  switchState = digitalRead(button);

  if (switchState == LOW)         //button not pressed
  {
    digitalWrite(green, HIGH);      //green LED on
    digitalWrite(yellow, LOW);
    digitalWrite(red1, LOW);
    digitalWrite(red2, LOW);

    delay(500);

    digitalWrite(green, LOW);     //blink green LED

    delay(500);
  }//end if

  else        //Button pressed
  {

    digitalWrite(green, LOW);
    digitalWrite(red1, LOW);
    digitalWrite(red2, LOW);

    //Blink yellow LED for 3 sec
    for (byte i=0; i<7; i++)
    {
      digitalWrite(yellow, HIGH);   
      delay(250);
      digitalWrite(yellow, LOW);
      delay(250);
    }


    // alternate the two red leds, while the button is pressed  
    while (digitalRead(button) == HIGH) 
    {
      digitalWrite(red1, HIGH);
      digitalWrite(red2, LOW);
      delay(250);

      digitalWrite(red1, LOW);
      digitalWrite(red2, HIGH);
      delay(250);

    }     //end while loop

  }     //end button press

}     //end void loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.