Beginner- Help Needed (Pretty please)

I was trying to recreate the traffic light project but had to modify as it was outdated. I have no idea what's wrong. Here is the code:

//////Pedestrian trafic light circut V1.0//////
// put your setup code here, to apply for whole code:
int wait= 1000;//time between change
int pause= 3000;//green phase ped
int flag= 0; //How To Use a Push Button suggestion
int buttonState= 0;//variables will change
const int PIN_redP= 7;//red pin LED
const int buttonPin= 2;
const int PIN_greenP= 6;//green pin LED
const int PIN_redC= 5;//red car pin LED
const int PIN_yellowC= 4;
const int PIN_greenC= 3;
//////constants created//////
void setup() {
pinMode(PIN_redP, OUTPUT); //Define as imputs and outputs
pinMode(PIN_greenP, OUTPUT);
pinMode(PIN_redC, OUTPUT);
pinMode(PIN_yellowC, OUTPUT);
pinMode(PIN_greenC, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
//Start state ped traffic light red- car green
digitalWrite(PIN_redP, HIGH);
digitalWrite(PIN_greenP, LOW);
digitalWrite(PIN_redC, LOW);
digitalWrite(PIN_yellowC, LOW);
digitalWrite(PIN_greenC, HIGH);
//button
}
void loop() {// put your main code here, to run repeatedly:
 buttonState = digitalRead(buttonPin);
  if(buttonPin == HIGH){ //Only when switch is pressed
   if ( flag == 0){
    delay(init);
    digitalWrite(PIN_A3, LOW);//Green C
    digitalWrite(PIN_A4, HIGH);//Yellow C
    delay(init);
    digitalWrite(PIN_A4, LOW);//Yellow C
    digitalWrite(PIN_A5, HIGH);//Red C
    delay(init);
    digitalWrite(PIN_A7, LOW);//Red P
    digitalWrite(PIN_A6, HIGH);//Green P
    delay(pause); //it was being difficult
    digitalWrite(PIN_A6, LOW);//Green P
    digitalWrite(PIN_A7, HIGH);//Red P
    delay(init);
    digitalWrite(PIN_A5, LOW);//Red C
    digitalWrite(PIN_A4, HIGH);//Yellow C
    delay(init);
    digitalWrite(PIN_A4, LOW);//Yellow C
    digitalWrite(PIN_A3, HIGH);//Green C
    flag= 1;
  }
  else if (flag == 1){
   digitalWrite(PIN_A3, HIGH);//Green C
   digitalWrite(PIN_A4, LOW);//Yellow C
   digitalWrite(PIN_A5, LOW);//Red C
   digitalWrite(PIN_A6, LOW);//Green P
   digitalWrite(PIN_A7, HIGH);//Red P
   flag= 0;
   }
 }
 delay(init);}

And here are photos of the project:



I’ll respond similarly to how I answered this question before: Use code tag as many of us can’t follow your unformulated text. Provide an annotated schematic that clearly shows how everything is wired, along with links to the technical details for each hardware component. Make sure to include all connections power, ground, power sources, external components and indicate any wires longer than 10 inches (25 cm).

Is this a class assignment?

1 Like

Reply #2 spells out most of it.
Here's a link You are supposed to read and use: How to get the best out of this forum - Development Tools / IDE 1.x - Arduino Forum

Why didn't You use it?

pinMode(buttonPin, INPUT_PULLUP);

INPUT_PULLUP means that the state of the button pin will be HIGH when the button is not pressed

I assume that the following line of code

if(buttonPin == HIGH){ //Only when switch is pressed

is an attempt to check whether the state of the button pin is HIGH.

Two problems

  1. This is not checking the state of the button pin. You need to use digitalRead() to do that
  2. Even if you do read the state of the button pin it will he HIGH when the button is not pressed

1 Like

I see, it is not for a class. I wish to learn how to code on my own.

I didn't realize this existed. My b.

Thanks

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