Please help me find a way if pressing the button once it will loop the traffic light. Been doing this for a while and its my 1st activity

const int buttonPin = 2;
const int green =  8;
const int yellow =  7;
const int red =  4;

int buttonState = 0; 

void setup() {
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(buttonPin, INPUT);
  
  digitalWrite(green, 0);
  digitalWrite(yellow, 0);
  digitalWrite(red, 0);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) 
  {
    digitalWrite(green, HIGH);
    digitalWrite(red, HIGH);
    delay(2000);
    digitalWrite(green, LOW);
    digitalWrite(red, LOW);

    digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(10000);                       // wait for a second
    digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);                       // wait for a second
    digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(3000);                       // wait for a second
    digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);                       // wait for a second
    digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(5000);                       // wait for a second
    digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);       
  } 
  else 
  {
    digitalWrite(green, LOW);
    digitalWrite(red, LOW);
  }
}

Thank you for using code tags :+1: .

Always show us a good schematic of your proposed circuit.

Show us a good image of your ‘actual’ wiring.

Give links to components.


Write in words exactly what the sketch is supposed to do and how inputs change the operation ?


I need to make a traffic System using 3 LEDS, 1 Button. Every time that we will click the button the Green and Red LED should blink else the traffic system should be displayed

The purple wire should be connected to pin 2 not to GND.

Currently you are shorting the power supply when you press the button.

AND

your pulldown resistor is not connected to anything... it should be connected to GND. Looks like the blue wire is in the wrong track... it's also shorting the power supply.


Heres an update on it

It is still not right...

Now the Red wire to the button is not connected to the White wire (5v).
The blue wire (GND) is connected to the White wire(5v) - shorting the power supply.

Are the LEDs supposed to be connected to common GND or common 5V ?

Look carefully at this version that works...

image

1 Like

@ruberr
Here's a tutorial with the code and circuit to implement a traffic light:
https://create.arduino.cc/projecthub/techno_z/arduino-traffic-light-simulator-2ec9f7

And here's a tutorial for a button:

Together you should be able to get this working. Looks like @red_car already helped you with the circuit so you can now focus on the code. Good luck!

1 Like

Are you missing code for the yellow led?

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