Sure! I can help you with the coding for a 2-way traffic light using Arduino. Below is an example code for both the versions - with switch and without switch.
1. Two-Way Traffic Light with Switch:
C++:
const int red1Pin = 2; // Red LED pin for road 1
const int yellow1Pin = 3; // Yellow LED pin for road 1
const int green1Pin = 4; // Green LED pin for road 1
const int red2Pin = 5; // Red LED pin for road 2
const int yellow2Pin = 6; // Yellow LED pin for road 2
const int green2Pin = 7; // Green LED pin for road 2
const int switchPin = 8; // Switch pin
void setup() {
pinMode(red1Pin, OUTPUT);
pinMode(yellow1Pin, OUTPUT);
pinMode(green1Pin, OUTPUT);
pinMode(red2Pin, OUTPUT);
pinMode(yellow2Pin, OUTPUT);
pinMode(green2Pin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
}
void loop() {
// Check if the switch is pressed
if (digitalRead(switchPin) == LOW) {
// If switch is pressed, road 1 gets green light and road 2 gets red
digitalWrite(red1Pin, LOW);
digitalWrite(yellow1Pin, LOW);
digitalWrite(green1Pin, HIGH);
digitalWrite(red2Pin, HIGH);
digitalWrite(yellow2Pin, LOW);
digitalWrite(green2Pin, LOW);
delay(5000); // Green light for road 1 for 5 seconds
// Road 1 transitions from green to yellow
digitalWrite(yellow1Pin, HIGH);
delay(2000); // Yellow light for road 1 for 2 seconds
// Road 1 gets red light and road 2 gets green
digitalWrite(green1Pin, LOW);
digitalWrite(yellow1Pin, LOW);
digitalWrite(red1Pin, HIGH);
digitalWrite(red2Pin, LOW);
digitalWrite(yellow2Pin, LOW);
digitalWrite(green2Pin, HIGH);
delay(5000); // Green light for road 2 for 5 seconds
// Road 2 transitions from green to yellow
digitalWrite(yellow2Pin, HIGH);
delay(2000); // Yellow light for road 2 for 2 seconds
// Road 2 gets red light and road 1 gets green
digitalWrite(green2Pin, LOW);
digitalWrite(yellow2Pin, LOW);
digitalWrite(red2Pin, HIGH);
digitalWrite(red1Pin, LOW);
digitalWrite(yellow1Pin, LOW);
digitalWrite(green1Pin, HIGH);
delay(5000); // Green light for road 1 for 5 seconds
}
}
2. Two-Way Traffic Light without Switch:
C++:
const int red1Pin = 2; // Red LED pin for road 1
const int yellow1Pin = 3; // Yellow LED pin for road 1
const int green1Pin = 4; // Green LED pin for road 1
const int red2Pin = 5; // Red LED pin for road 2
const int yellow2Pin = 6; // Yellow LED pin for road 2
const int green2Pin = 7; // Green LED pin for road 2
void setup() {
pinMode(red1Pin, OUTPUT);
pinMode(yellow1Pin, OUTPUT);
pinMode(green1Pin, OUTPUT);
pinMode(red2Pin, OUTPUT);
pinMode(yellow2Pin, OUTPUT);
pinMode(green2Pin, OUTPUT);
}
void loop() {
// Road 1 gets green light and road 2 gets red
digitalWrite(red1Pin, LOW);
digitalWrite(yellow1Pin, LOW);
digitalWrite(green1Pin, HIGH);
digitalWrite(red2Pin, HIGH);
digitalWrite(yellow2Pin, LOW);
digitalWrite(green2Pin, LOW);
delay(5000); // Green light for road 1 for 5 seconds
// Road 1 transitions from green to yellow
digitalWrite(yellow1Pin, HIGH);
delay(2000); // Yellow light for road 1 for 2 seconds
// Road 1 gets red light and road 2 gets green
digitalWrite(green1Pin, LOW);
digitalWrite(yellow1Pin, LOW);
digitalWrite(red1Pin, HIGH);
digitalWrite(red2Pin, LOW);
digitalWrite(yellow2Pin, LOW);
digitalWrite(green2Pin, HIGH);
delay(5000); // Green light for road 2 for 5 seconds
// Road 2 transitions from green to yellow
digitalWrite(yellow2Pin, HIGH);
delay(2000); // Yellow light for road 2 for 2 seconds
// Road 2 gets red light and road 1 gets green
digitalWrite(green2Pin, LOW);
digitalWrite(yellow2Pin, LOW);
digitalWrite(red2Pin, HIGH);
digitalWrite(red1Pin, LOW);
digitalWrite(yellow1Pin, LOW);
digitalWrite(green1Pin, HIGH);
delay(5000); // Green light for road 1 for 5 seconds
}
In the code, you need to connect the LEDs to the corresponding pins specified in the code. You can also modify the delays and timings as per your requirements. Let me know if you need any further assistance!