Help - Problem with Green LED not staying lit

Hi! Can you please help me with my code?

I am trying to keep my Green light lit for as long as possible until the button is pressed which will then go to Yellow and Red and then back to Green again.

The code I have used is:

//declare LEDs
int red = 2;
int yellow = 6;
int green = 7;

//declare button
int btnPedestrian = 4;

void setup() {
// put your setup code here, to run once:

pinMode(green, OUTPUT); //sets the digital pin 7 as OUTPUT
pinMode(yellow, OUTPUT); //sets the digital pin 6 as OUTPUT
pinMode(red, OUTPUT); //sets the digital pin 2 as OUTPUT
//turn LEDs off
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
//set the button to INPUT
pinMode(btnPedestrian, INPUT);
//For the serial monitor
Serial.begin(9600);

}

void loop() {

// put your main code here, to run repeatedly:

while (digitalRead(4)==LOW)
{
digitalWrite(green, HIGH);

//digitalWrite(red, LOW);
//digitalWrite(green, HIGH);
/1/Serial.println("GREEN");
//greenLight();
//delay(3000);

//digitalWrite(green, LOW);
//digitalWrite(yellow, HIGH);
//Serial.println("\nYELLOW");
//delay(5000);

//digitalWrite(yellow, LOW);
//digitalWrite(red, HIGH);
//Serial.println("RED");
//delay(25000);

}

/*void greenLight(){

//checking for pedestrain button
for (int i = 1; i < 31; i++)
{
int state = digitalRead(btnPedestrian);
if (state==0)
{
Serial.print(i);
delay(1000);
Serial.print(", ");
}
else if (state==1)
{
Serial.print("Pedestrian Crossing");
delay(5000);
return;
}
}
}*/

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

int btnPedestrian = 4;

You have given a meaningful name to pin 4 but later

  while (digitalRead(4) == LOW)

You don't use the name. Why ? It is not a problem in itself but is sloppy

As to the code, what is keeping pin 4 LOW ?
Have you got a pulldown resistor in place keeping it a known state or is it floating at an unknown state, maybe HIGH, maybe LOW ?

consider

#undef MyHW
#ifdef MyHW
int red    = 11;
int yellow = 12;
int green  = 13;
int btnPedestrian = A1;

#else
//declare LEDs
int red = 2;
int yellow = 6;
int green = 7;
//declare button
int btnPedestrian = 4;
#endif

void setup() {
    // put your setup code here, to run once:
    pinMode(green, OUTPUT); //sets the digital pin 7 as OUTPUT
    pinMode(yellow, OUTPUT); //sets the digital pin 6 as OUTPUT
    pinMode(red, OUTPUT); //sets the digital pin 2 as OUTPUT

    //turn LEDs off
    digitalWrite(green, HIGH);
    digitalWrite(yellow, LOW);
    digitalWrite(red, LOW);

    pinMode(btnPedestrian, INPUT_PULLUP);

    Serial.begin(9600);
}

void loop() {
    // put your main code here, to run repeatedly:
    if (digitalRead(btnPedestrian)==LOW)
    {
        digitalWrite(green,  LOW);
        digitalWrite(yellow, HIGH);

        delay(3000);

        digitalWrite(yellow, LOW);
        digitalWrite(red,    HIGH);

        delay(3000);

        digitalWrite(red,    LOW);
        digitalWrite(green,  HIGH);
    }
}

yes pull down resistor

presumably the other momentary switch terminal is connect to +V and the code should be checking for the input to be HIGH when checking if the switch is pressed in the code below

Recommend using internal pullup resistor and connect switch button to GND. Then button pressed is LOW.