Traffic Light
Emulate a traffic light, that behaves the way it would in Bahrain. Feel free to change it in any way it would in your country!Component List:
- 1 x Arduino Uno R3
- 3 x LED (any color)
- 3 x Resistor
- Jumper Wires
- 1 x Breadboard
- 1 x Ultrasonic Sensor HC-04
Wiring Guide:
- Connect the anode (longer leg) of each LED to a separate digital pin on the Arduino (pins 2, 3, and 4).
- Connect the cathode (shorter leg) of each LED to a separate resistor.
- Connect the other end of each resistor to the ground (GND) on the Arduino.
- Connect the ground (GND) of the Arduino to the ground rail of the breadboard.
- Connect the power (5V) of the Arduino to the power rail of the breadboard.
Code:
// Pin definitions
const int redPin = 2;
const int yellowPin = 3;
const int greenPin = 4;
// Time delays in milliseconds
const int redDelay = 5000; // 5 seconds for red
const int yellowDelay = 2000; // 2 seconds for yellow
const int greenDelay = 5000; // 5 seconds for green
const int blinkDelay = 400; // Half second delay for blinking
void setup() {
// Set the LED pins as OUTPUT
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
// Red light
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
delay(redDelay);
// Yellow light
digitalWrite(yellowPin, HIGH);
delay(yellowDelay);
// Green light
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
delay(greenDelay);
for (int i = 0; i <= 5; i++)
{
digitalWrite(greenPin, LOW);
delay(blinkDelay);
digitalWrite(greenPin, HIGH);
delay(blinkDelay);
}
// Yellow light
digitalWrite(greenPin, LOW);
digitalWrite(yellowPin, HIGH);
delay(yellowDelay);
}
Looking to build this project? Check out our custom, Bahraini Assembled, starter kit here!
https://www.bitware.store/product/arduino-uno-r3-compatible-with-arduino-ide/
[bitware_arabic_portfolio_content]
Project Details | |
| Level: | Beginner (1/5) |
| Category: | Miniature Model |
| Platform: | Arduino |
| Creator: | BitWare Store |