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. ![]()
