buttons push to run different modes

Ok, So I am off to try and start a new project and would like some guidance on where to look to get an idea of how this would be possible. My project will consist of 1 push button and 3 LED lights (red, yellow, green) I want My program when started to run the following code on loop.

Startup on "traffic light mode" (loop until interrupted):

  digitalWrite(red, LOW);   
  digitalWrite(yellow, LOW);     // GREEN TIME
  digitalWrite(green, HIGH); 
  delay(12000); 
  digitalWrite(red, LOW);   
  digitalWrite(yellow, HIGH);    // YELLOW TIME
  digitalWrite(green, LOW); 
  delay(4000); 
  digitalWrite(red, HIGH);   
  digitalWrite(yellow, LOW);     // RED TIME
  digitalWrite(green, LOW); 
  delay(18000);

Than at the push of the push button, Flash the Yellow LED (loop until interrupted):

If the button is pushed again...

Flash the RED LED (loop until interrupted):

Than if the button is pushed again...

Return to "traffic light mode"

and the next button push flash yellow again, next button push flash red... and so on.

Where could I look as to get an idea of how to write this code?

Thanks again for all your help guys

Where could I look as to get an idea of how to write this code?

Start with the state change detection example. When you can detect that the switch has changed state (to pressed), increment a counter. Base you actions on the value in the counter.

Be prepared to hold the switch down for a long time.

Or, look at the blink with delay example, or any of the 40 million Arduino traffic light sketches, to learn how NOT to use delay().

Thanks for your reply, unfortunately I have not been able to find any arduino traffic light sketches after searching for a few hours online that didn't use the delay feature to get the timing. I have studied the blink without delay and I can't seem to figure how I could write that to control 3 leds all without delay.

Also, Could I still use the random command without using delay?

I have an interavctive traffic light code you can browse through:
it uses Padestrian lights a push button and a set of traffic lights :slight_smile:

int carRed = 12; // assigning the fraffic lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 8; // assigning the pedestrian lights
int pedGreen = 9;
int button = 2; // button pin
int crossTime = 5000; //time allowed to cross
unsigned long changeTime; // time since button pressed

void setup()
{
	pinMode(carRed, OUTPUT);
	pinMode(carYellow, OUTPUT);
	pinMode(carGreen, OUTPUT);
	pinMode(pedRed, OUTPUT);
	pinMode(pedGreen, OUTPUT);
	pinMode(button, INPUT); // button is on pin 2
	// turn on the green light
	digitalWrite(carGreen, HIGH);
	digitalWrite(pedRed, HIGH);
	}

void loop()
{
	int state = digitalRead(button);
	/*check if the button is pressed and it is over 5 second since it was pressed last time*/
	if (state == HIGH && (millis() - changeTime) > 5000){
		// call the function to change the lights
		changeLights();

	}
}

void changeLights(){
	digitalWrite(carGreen, LOW); // green off
	digitalWrite(carYellow, HIGH); // yellow on
	delay(2000); // wait 2 seconds

	digitalWrite(carYellow, LOW);//yellow off
	digitalWrite(carRed, HIGH);//red on
	delay(1000); //wait 1 second until safe

	digitalWrite(pedRed, LOW);// ped red off
	digitalWrite(pedGreen, HIGH);// ped green on
	delay(crossTime); //wait for preset time

	// flash the ped green
	for (int x = 0; x<10; x++) {
		digitalWrite(pedGreen, HIGH);
		delay(250);
		digitalWrite(pedGreen, LOW);
		delay(250);
	}
	// turn ped red on
	digitalWrite(pedRed, HIGH);
	delay(500);

	digitalWrite(carYellow, HIGH); // YELLOW ON
	digitalWrite(carRed, LOW); // red off
	delay(1000);
	digitalWrite(carGreen, HIGH);
	digitalWrite(carYellow, LOW); // yellow off

	// record the time since last change of lights

	changeTime = millis();
	//then returnm to main loop
}

it uses Padestrian lights a push button and a set of traffic lights

and delay() in several places.

So does anyone have an example of a traffic light code without delay?

Also is it possibile to use Random without delay?

You might find this video explanation of blink without delay useful.