Step 2: Connection From PUSHBUTTON To Board:
- Connect the positive pin to the 7 of the Arduino board with the positive side. Connect the 5v of the board.
- Connect the negative side to the GND of the board.
Programming
- int one = 03;
- int two = 04;
- int three = 05;
- int btn = 7;
- int state = 0;
- void setup()
- {
- pinMode(one, OUTPUT);
- pinMode(two, OUTPUT);
- pinMode(three, OUTPUT);
- }
- void loop()
- {
- if (digitalRead(btn))
- {
- if (state == 0)
- {
- setLights(HIGH, LOW, LOW);
- state = 1;
- } else if (state == 1)
- {
- setLights(HIGH, HIGH, LOW);
- state = 2;
- } else if (state == 2)
- {
- setLights(LOW, LOW, HIGH);
- state = 3;
- } else if (state == 3)
- {
- setLights(LOW, HIGH, LOW);
- state = 0;
- }
- delay(1000);
- }
- }
- void Traffic(int red, int yellow, int green)
- {
- digitalWrite(one, red);
- digitalWrite(two, yellow);
- digitalWrite(three, green);
- }
Explanation:
- It can glow the lights automatically with the delay times.
- We can set the delay time and watch the LEDs/lights, it changes automatically.
- When the first light glows, the second and third remains off.
- Second light glows the third and first is off.
- Third light glows the first and second is off.
Read more articles on Arduino: