Random code for a traffic light project

Hi, I was wondering if anyone could help me figure out how to write a code to control a red led (pin 2) yellow led (pin 3) and a green led (pin 4) to operate as a traffic light where I can set random times between 10,000 ms and 40,000 ms for my red and green times, yet to have my yellow led have a set time of always 4,000 ms. I would like the program to pick a random time with every cycle so my red and green times are not the same each time around the loop however the yellow would always be set at 4,000 ms. Any help would be greatly appreciated. Thanks

check the blink without delay sketch (example) for the coding technique to use,

furthermore you need something like:

redTime = random(10000, 40000);

and a small statemachine based upon

enum trafficLED = { INITIAL, RED, YELLOW, GREEN, BLINK }; //

and a switch() case construct to switch state and do the appropriate timing.

Rob is trying to get you into good habits. But that does make it slightly more complicated for a newbie. Here's the easy way, but understand that using delay() is a "bad habit" that works well and is easy in the short term but may well cause you problems in the future when you want to add more functionality to your sketch.

#define redPin 2
#define yelPin 3
#define grnPin 4

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(yelPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
}

void loop() {
  digitalWrite(redPin, HIGH);
  delay(random(10000, 40000));
  digitalWrite(redPin, LOW);
  
  digitalWrite(yelPin, HIGH);
  delay(4000);
  digitalWrite(yelPin, LOW);

  digitalWrite(grnPin, HIGH);
  delay(random(10000, 40000));
  digitalWrite(grnPin, LOW);
}

Paul

Thank you for your fast response. I have tried incorporating a push button into the code that will cause the yellow led to flash at the instant a push button is pressed using the flash code below.

digitalWrite(yelPin, HIGH);
delay(538);
digitalWrite(yelPin, LOW);
delay(538);

I would like to get it to where the whole while the button is pushed the yellow led will flash using the above code, and when the button is released it will go back into the normal operation code below...

digitalWrite(grnPin, HIGH);
delay(random(12000, 38000));
digitalWrite(grnPin, LOW);

digitalWrite(yelPin, HIGH);
delay(4000);
digitalWrite(yelPin, LOW);

digitalWrite(redPin, HIGH);
delay(random(20000, 4500));
digitalWrite(redPin, LOW);

where could I look for an idea of how to code that push button to give me a constant yellow flash while a push button input is high (button pushed), and I get my normal red yellow green timing when the push button input is low (button released)
Thanks again for your help as I am just on day 2 with my Arduino.

where could I look for an idea of how to code that push button to give me a constant yellow flash while a push button input is high (button pushed),

By reading and understanding reply #1 you can do this.

This might help you with the concept:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html