Traffic lighting system with pedestrian button and signal

i am trying to code a traffic light system with pedestrian signals as well as sound a buzzer. it is not actually working, it is just running through "mainStop()" and "mainGo()" without the need for pressing the button. any help is appreciated!

//labeling pins used
const int redLED = 12;                   //pin 12 labeled as redLED
const int yellowLED = 11;                //pin 11 labeled as yellowLED
const int greenLED = 10;                 //pin 10 labeled as greenLED

const int pedRed = 8;                    //pin 8 labeled as pedRed
const int pedGreen = 7;                  //pin 7 labeled as pedGreen
const int buzzer = 6;

const int button = 4;                    //pin 5 labeled as button

//declare constant for button
int buttonPress = 0;                     //state of the button being pressed is 0 (off)
int sound = 500;

void setup()
{
  //stating outputs and inputs
  pinMode(redLED, OUTPUT);               //red LED is an output
  pinMode(yellowLED, OUTPUT);            //yellow LED is an output
  pinMode(greenLED, OUTPUT);             //green LED is an output
  pinMode(pedRed, OUTPUT);               //pedestrian stop LED is an output
  pinMode(pedGreen, OUTPUT);             //pedestrian go LED is an output
  pinMode(button, INPUT);                //button is an input
}
void loop()
{
  if (digitalRead(button) == HIGH)
  {
  mainStop();                            //run the mainStop() function
  delay(15000);                          //wait for 15 seconds for pedestrians to cross
  mainGo();                              //run the mainGo() function
  }
  else
  {
    digitalWrite(greenLED, HIGH);
    digitalWrite(pedRed, HIGH);
    digitalWrite(redLED, LOW);
    digitalWrite(yellowLED, LOW);
    digitalWrite(pedGreen, LOW);
  }
}

void mainStop()
{
//cycle through the "stop" phase for cars
    digitalWrite(greenLED, HIGH);               //green LED on
    digitalWrite(pedRed, HIGH);                 //pedestrian stop LED on
    delay(3000);                                //wait quarter a second
    digitalWrite(yellowLED, HIGH);              //yellow LED on
    delay(3000);                                //wait 3 seconds
    digitalWrite(greenLED, LOW);                //green LED off
    digitalWrite(yellowLED, LOW);               //yellow LED off
    digitalWrite(redLED, HIGH);                 //red LED on
    digitalWrite(pedGreen, HIGH);               //pedestrian go LED on
    digitalWrite(pedRed, LOW);                  //pedestrian stop LED off
    //this is to countdown the repetitions of the buzzer sounding
      while(pedGreen == HIGH)
      {
      tone(buzzer, 500);
      delay(100);
      tone(buzzer, 1000);
      delay(100);
      }
}

void mainGo()
{
//cycle through the "go" phase for cars
  digitalWrite(redLED, HIGH);                   //red LED on
  digitalWrite(yellowLED, HIGH);                //yellow LED on
  digitalWrite(greenLED, LOW);                  //green LED off
  digitalWrite(pedGreen, LOW);                  //pedestrian go LED off
  digitalWrite(pedRed, HIGH);                   //pedestrian stop LED on
  delay(3000);                                  //wait 3 seconds
  digitalWrite(redLED, LOW);                    //red LED off
  digitalWrite(yellowLED, LOW);                 //yellow LED off
  digitalWrite(greenLED, HIGH);                 //green LED on
  delay(100);                                   //wait 1ms
}

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

1 Like

Hello silvermystic26

Welcome to the world's best Arduino forum ever.

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code as diagnostic prints to see the values of variables and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

Welcome! I hope this is not an assignment due this year. Posting your code properly is a great start. Can you post an annotated schematic showing exactly how it is wired? There are a lot of examples on the web did you check?

Thanks for the help so far, the wiring is just components connected to pins and the negative rail. I have added the “INPUT_PULLUP” into my code which hasn’t worked. It is currently cycling through the two functions with no inputs.

I have also been looking back at notes and websites and other arduino projects pages, but not much gained. The buzzer is also just not sounding. Also, where can I add “serialPrint()” to my code?

are you still checking for HIGH instead of LOW

current circuit diagram

wooo! its working. all thats left to fix now is the buzzer not sounding. the buzzer is meant to sound whenever the LED labelled "pedGreen" is on, which i have tried to achieve with the "while" command...

the code is now working! the buzzer is sounding, but it is non-stop. i assume it is something to do with the lines 58 to 64, the buzzer section. any suggestions on making it stop when the rest of the "mainStop()" function is done?

how does the loop ever exit if the value of pedGreen doesn't change

it doesn't, thats what i'm struggling to find out and fix...

each iteration thru loop, you can test if pedGreen is set turn on a buzzer or not, along with the traffic light logic

Read about state-machines and millis() timing.

Don’t start until you understand them.
Then, arrays and structs.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.