Building a traffic light system, with a button and speaker

Hi Ardunioes.

For a school project, I have to build a traffic light system that does the following:

When the pedestrian button is pressed, the car light turns to yellow(amber) and then to green.
The speaker will then make a sound to indicate it is safe to cross.
After a suitable time, the red light will flash (3 times), stay red, and then the car green light will turn back on.

So, I've done it.
Except it doesn't work. The button will simply not be read by the circuit and random LEDs will stay on and do nothing else.


So, here's the code:

//integers
int buttonState = 0; //set buttonState
int buttonPin = 2; //set button
int speakerPin = 7; //set speaker
int pedGreen = 8; //set pedestrian LEDs
int pedRed = 9;
int carGreen = 10; //set car LEDs
int carYellow = 11;
int carRed = 12;

//input and output devices
void setup(){
  pinMode(buttonPin, INPUT); //set button
  pinMode(speakerPin, OUTPUT); //set speaker
  pinMode(pedGreen, OUTPUT); //set pedestrian LEDs
  pinMode(pedRed, OUTPUT);
  pinMode(carGreen, OUTPUT); //set car LEDs
  pinMode(carYellow, OUTPUT);
  pinMode(carRed, OUTPUT);
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
    delay(5000)
    digitalWrite(carGreen, LOW); 
    digitalWrite(carYellow, HIGH); 
    delay(1000); 
    digitalWrite(carYellow, LOW); 
    digitalWrite(carRed, HIGH);
    delay(1000);
    tone(speakerPin, 3000, 1000)
    digitalWrite(pedGreen, HIGH);
    delay(5000);
    digitalWrite(pedGreen, LOW);
    digitalWrite(pedRed, HIGH); 
    delay(250);
    digitalWrite(pedRed, LOW);
    delay(250);
    digitalWrite(pedRed, HIGH);
    delay(250);
    digitalWrite(pedRed, HIGH);
    digitalWrite(carGreen, HIGH);
    
  }
  else {
    digitalWrite(carGreen, HIGH);
    digitalWrite(pedRed, LOW);
  }
}

Yes, it's not pretty, except I don't really understand using other voids and things to create multiple loops.
If you could help me with that as well, that'd be great!

And a picture of my circuit:

edit: just noticed the green resistor for the pedestrians is different to the rest. It's not meant to be like that. Just a little mistake.


Many thanks to anyone that can help me with this. :slight_smile:

necr0_bro:
Hi Ardunioes.

For a school project, I have to build a traffic light system that does the following:

Many thanks to anyone that can help me with this. :slight_smile:

I'm not sure about the functionality of your code, but to get a clean compile, you will have to add a couple of semi-colons.

Find these two lines and put semi-colons at the end of them.

delay(5000);
tone(speakerPin, 3000, 1000);

rootboy:
Find these two lines and put semi-colons at the end of them.

delay(5000);
tone(speakerPin, 3000, 1000);

Whoops.
Must've done something whilst editing. Added, but still the same issue. Thanks though.

You need to rewrite your code so that it doesn't need the delay() statements. Nothing can happen while they are operating. You need to use the concept in the Blink Without Delay example sketch. I wrote a more extensive example here Demonstration code for several things at the same time - Project Guidance - Arduino Forum

...R

Note this thread - a bit of a concern.

This fellow must have been reading a previous thread regarding using the piezo sounder, or else the requirement for a 220 ohm series resistor is in his course notes.

Two students sharing an IP? - quite possible, if they were working from the school (Matthew suggests his age is 16), though posting on Saturday? I suppose the school may offer facilities then as would a university.

I do have a mind to run up a comprehensive implementation of this project as it is such an obvious exercise and such niceties as duplicating the (Australian) "blip" sounds for the blind assistance are quite enticing. It won't be in time for the current deadline to be sure, as it is not exactly high on my priorities. :smiley:

If we could determine which educational institution it actually is, I could submit it for credit!