#define R1 23  // Red LED for Intersection 1
#define Y1 22  // Yellow LED for Intersection 1
#define G1 21  // Green LED for Intersection 1

#define R2 19  // Red LED for Intersection 2
#define Y2 18  // Yellow LED for Intersection 2
#define G2 5   // Green LED for Intersection 2

#define R3 17  // Red LED for Intersection 3
#define Y3 16  // Yellow LED for Intersection 3
#define G3 4   // Green LED for Intersection 3

#define pot1 34
#define pot2 36
#define pot3 32

#define MODE_BUTTON 25  // Button to toggle between manual and automatic mode
#define INTERSECTION1_BUTTON 26  // Button to control Intersection 1 manually
#define INTERSECTION2_BUTTON 27  // Button to control Intersection 2 manually
#define INTERSECTION3_BUTTON 14  // Button to control Intersection 3 manually

bool manualMode = true;

void setup() {
  Serial.begin(115200);

  pinMode(R1, OUTPUT);
  pinMode(Y1, OUTPUT);
  pinMode(G1, OUTPUT);

  pinMode(R2, OUTPUT);
  pinMode(Y2, OUTPUT);
  pinMode(G2, OUTPUT);

  pinMode(R3, OUTPUT);
  pinMode(Y3, OUTPUT);
  pinMode(G3, OUTPUT);

  pinMode(pot1, INPUT);
  pinMode(pot2, INPUT);
  pinMode(pot3, INPUT);

  pinMode(MODE_BUTTON, INPUT_PULLUP);
  pinMode(INTERSECTION1_BUTTON, INPUT_PULLUP);
  pinMode(INTERSECTION2_BUTTON, INPUT_PULLUP);
  pinMode(INTERSECTION3_BUTTON, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(MODE_BUTTON) == LOW) {
    manualMode = !manualMode;
    Serial.println(manualMode ? "Switched to Manual Mode" : "Switched to Automatic Mode");
    delay(250);
  }

  if (manualMode) {
    manualControl();
  } else {
    automaticControl();
  }

  delay(100);
}

void manualControl() {
  if (digitalRead(INTERSECTION1_BUTTON) == LOW) {
    Serial.println("Manual control: Intersection 1 selected");
    cycleTrafficLight(R1, Y1, G1);
  } else if (digitalRead(INTERSECTION2_BUTTON) == LOW) {
    Serial.println("Manual control: Intersection 2 selected");
    cycleTrafficLight(R2, Y2, G2);
  } else if (digitalRead(INTERSECTION3_BUTTON) == LOW) {
    Serial.println("Manual control: Intersection 3 selected");
    cycleTrafficLight(R3, Y3, G3);
  }
}

void automaticControl() {
  int pot1Value = analogRead(pot1);
  int pot2Value = analogRead(pot2);
  int pot3Value = analogRead(pot3);

  int G_Time1 = (pot1Value < 2048) ? 30000 : 60000;  // 30 or 60 seconds
  int G_Time2 = (pot2Value < 2048) ? 30000 : 60000;  // 30 or 60 seconds
  int G_Time3 = (pot3Value < 2048) ? 30000 : 60000;  // 30 or 60 seconds

  if (pot1Value > 0) {
    Serial.println("Automatic control: Intersection 1 has more vehicles");
    setTrafficLightAuto(G1, Y1, R1, G_Time1);
  } else if (pot2Value > 0) {
    Serial.println("Automatic control: Intersection 2 has more vehicles");
    setTrafficLightAuto(G2, Y2, R2, G_Time2);
  } else if (pot3Value > 0) {
    Serial.println("Automatic control: Intersection 3 has more vehicles");
    setTrafficLightAuto(G3, Y3, R3, G_Time3);
  } else {
    Serial.println("Automatic control: No vehicles detected, all intersections are red");
    allRed();
  }
}

void cycleTrafficLight(int R_pin, int Y_pin, int G_pin) {
  Serial.println("Cycling traffic light manually");
  digitalWrite(G_pin, HIGH);
  digitalWrite(Y_pin, LOW);
  digitalWrite(R_pin, LOW);
  delay(1000);
  
  digitalWrite(G_pin, LOW);
  digitalWrite(Y_pin, HIGH);
  delay(1000);
  
  digitalWrite(Y_pin, LOW);
  digitalWrite(R_pin, HIGH);
}

void setTrafficLightAuto(int G_pin, int Y_pin, int R_pin, int G_Time) {
  Serial.print("Setting traffic light automatically: Green for ");
  Serial.print(G_Time / 1000);
  Serial.println(" seconds");
  
  digitalWrite(G_pin, HIGH);
  digitalWrite(Y_pin, LOW);
  digitalWrite(R_pin, LOW);
  delay(G_Time);

  Serial.println("Switching to Yellow for 5 seconds");
  digitalWrite(G_pin, LOW);
  digitalWrite(Y_pin, HIGH);
  delay(5000);  // Yellow light for 5 seconds

  Serial.println("Switching to Red");
  digitalWrite(Y_pin, LOW);
  digitalWrite(R_pin, HIGH);
  delay(5000);  // Red light (or other lights) based on the sequence
}

void allRed() {
  Serial.println("All intersections are red");

  digitalWrite(R1, HIGH);
  digitalWrite(R2, HIGH);
  digitalWrite(R3, HIGH);
  
  digitalWrite(Y1, LOW);
  digitalWrite(Y2, LOW);
  digitalWrite(Y3, LOW);

  digitalWrite(G1, LOW);
  digitalWrite(G2, LOW);
  digitalWrite(G3, LOW);
}
$abcdeabcde151015202530fghijfghij