// Moch Rijki Supriyatna:(MRS) ("IKY")
// Define pins for traffic lights
int redPins[4] = {13, 10, 5, 2};
int yellowPins [4] = {12, 9, 6, 3};
int greenPins[4] = {11, 8, 7, 4};
// Time for each light (in milliseconds)
int redTime = 1000;
int yellowTime = 500;
int greenTime = 2000;
void setup() {
// Set pin as OUTPUT
for (int i = 0; i < 4; i++) {
pinMode(redPins[i], OUTPUT);
pinMode(yellowPins[i], OUTPUT);
pinMode(greenPins[i], OUTPUT);
}
}
void loop() {
for (int lane = 0; lane < 4; lane++) {
setTrafficLight(lane);
}
}
// Function to set traffic lights for a specific lane
void setTrafficLight(int lane) {
// Turn off all the lights first
for (int i = 0; i < 4; i++) {
digitalWrite(greenPins[i], LOW);
digitalWrite(yellowPins[i], LOW);
digitalWrite(redPins[i], HIGH);
}
// Turn on the green light for the active line
digitalWrite(greenPins [lane], HIGH);
digitalWrite(redPins [lane], LOW);
// Turns on the green light for the specified time.
delay(greenTime)
// Turn off the green light
;digitalWrite (greenPins [lane], LOW);
// Turns on the yellow light for the active lane
digitalWrite(yellowPins [lane], HIGH);
delay(yellowTime)
// Turn off the yellow light
;digitalWrite(yellowPins [lane], LOW);
}