#include <Arduino.h>

// Define the pins for controlling the traffic lights
const int pinGreenA = 2;
const int pinYellowA = 4;
const int pinRedA = 16;
const int pinGreenB = 17;
const int pinYellowB = 5;
const int pinRedB = 18;
const int pinGreenC = 19;
const int pinYellowC = 21;
const int pinRedC = 22;
const int pinGreenD = 23;
const int pinYellowD = 15;
const int pinRedD = 13;

// Define the durations for each light phase in milliseconds
const unsigned long greenDuration = 10000; // 10 seconds
const unsigned long yellowDuration = 1500; // 1.5 seconds
const unsigned long redDuration = 10000; // 10 seconds
const unsigned long zebra_crossDuration = 5000; // 5 seconds

// Function prototypes
// void setLights(int pinGreen, int pinYellow, int pinRed, bool green, bool yellow, bool red);

void setup() {
  // Initialize the traffic light pins as outputs
  pinMode(pinGreenA, OUTPUT);
  pinMode(pinYellowA, OUTPUT);
  pinMode(pinRedA, OUTPUT);
  pinMode(pinGreenB, OUTPUT);
  pinMode(pinYellowB, OUTPUT);
  pinMode(pinRedB, OUTPUT);
  pinMode(pinGreenC, OUTPUT);
  pinMode(pinYellowC, OUTPUT);
  pinMode(pinRedC, OUTPUT);
  pinMode(pinGreenD, OUTPUT);
  pinMode(pinYellowD, OUTPUT);
  pinMode(pinRedD, OUTPUT);
}

void loop() {
  // Phase 1: A and C green, B and D red
  setLights(pinGreenA, pinYellowA, pinRedA, true, false, false);
  setLights(pinGreenB, pinYellowB, pinRedB, false, false, true);
  setLights(pinGreenC, pinYellowC, pinRedC, true, false, false);
  setLights(pinGreenD, pinYellowD, pinRedD, false, false, true);
  delay(greenDuration);

  // Phase 2: A and C yellow, B and D red
  setLights(pinGreenA, pinYellowA, pinRedA, false, true, false);
  setLights(pinGreenB, pinYellowB, pinRedB, false, false, true);
  setLights(pinGreenC, pinYellowC, pinRedC, false, true, false);
  setLights(pinGreenD, pinYellowD, pinRedD, false, false, true);
  delay(yellowDuration);

  // Phase 3: A and C red, B and D green
  setLights(pinGreenA, pinYellowA, pinRedA, false, false, true);
  setLights(pinGreenB, pinYellowB, pinRedB, true, false, false);
  setLights(pinGreenC, pinYellowC, pinRedC, false, false, true);
  setLights(pinGreenD, pinYellowD, pinRedD, true, false, false);
  delay(redDuration);

  // Phase 4: A and C red, B and D yellow
  setLights(pinGreenA, pinYellowA, pinRedA, false, false, true);
  setLights(pinGreenB, pinYellowB, pinRedB, false, true, false);
  setLights(pinGreenC, pinYellowC, pinRedC, false, false, true);
  setLights(pinGreenD, pinYellowD, pinRedD, false, true, false);
  delay(yellowDuration);

  // Phase 5: All red
  setLights(pinGreenA, pinYellowA, pinRedA, false, false, true);
  setLights(pinGreenB, pinYellowB, pinRedB, false, false, true);
  setLights(pinGreenC, pinYellowC, pinRedC, false, false, true);
  setLights(pinGreenD, pinYellowD, pinRedD, false, false, true);
  delay(zebra_crossDuration);
}

void setLights(int pinGreen, int pinYellow, int pinRed, bool green, bool yellow, bool red) {
  digitalWrite(pinGreen, green);
  digitalWrite(pinYellow, yellow);
  digitalWrite(pinRed, red);
}