Arduino: "traffic lights"

Hi. Since yesterday, I've been working on a "beginner in Arduino" project. Basically it's traffic lights, 3 (green, yellow and red) for cars and 2 (red and green) for pedestrians. The main issue being, I've put some sound with tone(); and I would like each of my 2 functions (one has a sound for the green pedestrians light and the other has one too) to be on when their according light are on (I've done that already). But I want to add a push button that could be pressed at any time of the program and still, there would be the sound of the light that is on at that moment. And I would like that "button function" to work for like a minute or so. Can you please tell me if that's possible and how? Tank you for your answers.
P.S.: I'm a beginner in Arduino, just in case you haven't understood :stuck_out_tongue:

It would help a lot if you could provide us with the code you've already written. It is really hard to help you based solely on your description. Simply post your code and we might be able to help you :wink:

OK, but it’s a bit long:

#define DO 261
#define RE 293
#define RES 311
#define MI 329
#define FA 349
#define FAS 369
#define SOL 392
#define SOLS 415
#define LA 440

int LED_red = 13;
int LED_yellow = 12;
int LED_green = 11;
int LED_Pg = 10;
int LED_Pr = 9;
int buzzer = 8;
int button = 7;

void setup() {
// put your setup code here, to run once:
pinMode(LED_red, OUTPUT);
pinMode(LED_yellow, OUTPUT);
pinMode(LED_green, OUTPUT);
pinMode(LED_Pg, OUTPUT);
pinMode(LED_Pr, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(button, INPUT);
}

void SV() {
tone(buzzer, RE);
delay(100);
tone(buzzer, RES);
delay(100);
tone(buzzer, MI);
delay(100);
tone(buzzer, FA);
delay(100);
tone(buzzer, FAS);
delay(100);
tone(buzzer, SOL);
delay(100);
tone(buzzer, SOLS);
delay(100);
tone(buzzer, LA);
delay(800);
tone(buzzer, FAS);
delay(800);
noTone(buzzer);
delay(150);
for (int x = 0; x < 10; x++) {
tone(buzzer, LA);
delay(1000);
noTone(buzzer);
delay(150);
tone(buzzer, LA);
delay(700);
tone(buzzer, MI);
delay(700);
}
noTone(buzzer);
}

void SR() {
tone(buzzer, DO);
tone(buzzer, RE);
tone(buzzer, FA);
delay(5000);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_Pg, LOW);
digitalWrite(LED_Pr, HIGH);
delay(100);
if(digitalRead(button) == 1){
SR();
digitalWrite(LED_red, LOW);
digitalWrite(LED_green, HIGH);
delay(30000);
digitalWrite(LED_green, LOW);
digitalWrite(LED_yellow, HIGH);
delay(5000);
digitalWrite(LED_yellow, LOW);
digitalWrite(LED_red, HIGH);
delay(4000);
noTone(buzzer);
digitalWrite(LED_Pr, LOW);
digitalWrite(LED_Pg, HIGH);
SV();
}
else{
delay(5000);
digitalWrite(LED_red, LOW);
digitalWrite(LED_green, HIGH);
delay(30000);
digitalWrite(LED_green, LOW);
digitalWrite(LED_yellow, HIGH);
delay(5000);
digitalWrite(LED_yellow, LOW);
digitalWrite(LED_red, HIGH);
delay(4000);
noTone(buzzer);
digitalWrite(LED_Pr, LOW);
digitalWrite(LED_Pg, HIGH);
delay(20950);
}
}

So technically, all I can do with it is to make it work like usual traffic lights, and if I want to put the sound on, I have to do it before "LED_Pr" turns on.

This is a good time to move on to the next lesson…
How to work without delay()
You’ll find many threads that will help you shift your timing across to use millis() - which will allow you to do several things at the same time.

delay() simply stops everthing until each of those delays has completed.

As well as millis() timing, look at ‘state-machines’ - perfectly suited to sequencing like traffic lights.

as the other guys said, when you are using delay it freezes everything so you cant detect button pushes or do anything else when you use delay. delay is really only good for super simple stuff.

arduinos have a function called millis(). its a counter running in the background of arduino boards. you can use it to create timers. this way you dont freeze your thread and can do multiple things at once.

here is a code example that creates a section of code that runs every second.

i put your song in array to make things super simple. i created another variable called "current" that keeps track of where you are at in your song.

anyways this should play a little tune and you should be able to detect button pushes as normal.
i'm not sure what kind of timing you are trying to do with your lights but you should be able to add that in a similar way. if you need more help adding lights or playing multiple songs let me know.

int song []{261,293,311,293,311,329,415,329};
int songlength = 8;
int current;

unsigned long int last;
void setup() {}

void loop() { 
  // this next line creates a timer that happens once every second
if(millis()-last>1000){last=millis();
  
  // play your notes
  tone( 7,song[current]);
  current++;
  if(current==songlength){current=0;}
  } 

// you can put button detection here
  }

Hmmm - musical traffic lights ? :slight_smile: