const int trigPin = 9; // Trig pin of ultrasonic sensor connected to digital pin 9
const int echoPin = 10; // Echo pin of ultrasonic sensor connected to digital pin 10
const int redPin = 3; // Red LED connected to digital pin 3
const int yellowPin = 5; // Yellow LED connected to digital pin 5
const int greenPin = 6; // Green LED connected to digital pin 6
const int trigIkan = 11;
const int echoIkan = 12;

int carCount1 = 0; // Variable to store the number of cars detected
int carCount2 = 0;
int greenDuration = 10; // Duration of the green light in seconds

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigIkan, OUTPUT);
  pinMode(echoIkan, INPUT);
  Serial.begin(9600); // Start serial communication for debugging
}

void loop() {
  long duration1, distance1;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration1 = pulseIn(echoPin, HIGH);
  distance1 = duration1 * 0.034 / 2;
  Serial.print("Distance1 : ");
  Serial.println(distance1); // Print the distance for debugging

  long duration2, distance2;
  digitalWrite(trigIkan, LOW);
  delayMicroseconds(2);
  digitalWrite(trigIkan, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigIkan, LOW);
  duration2 = pulseIn(echoIkan, HIGH);
  distance2 = duration2 * 0.034 / 2;
  Serial.print("Distance2: ");
  Serial.println(distance2); // Print the distance for debugging

  digitalWrite(yellowPin, LOW);
  digitalWrite(redPin, HIGH);

  if (distance1 < 50) { // If a car is detected within 50 cm
    carCount1++;
    Serial.println("is there car in sensor 1 : yes");
  }else{
    carCount1==0;
    Serial.println("is there car in sensor 1 : no");
  }
  if (distance2 < 50) { // If a car is detected within 50 cm
    carCount2++;
    Serial.println("is there car in sensor 2 : yes");
  }else{
    carCount2==0;
    Serial.println("is there car in sensor 2 : no");
  }
    // Adjust the green duration based on the number of cars detected
  if (carCount1==1) {
    greenDuration = 30; 
  } else{
    if(carCount2==1){
      greenDuration = 20;
    }else{
      greenDuration = 10;
      }
  }

  delay(7000);

  if (carCount1 = 1) {
    digitalWrite(greenPin, HIGH); // Turn on the green light
    digitalWrite(yellowPin, LOW); // Turn off the yellow light
    digitalWrite(redPin, LOW); // Turn off the red light
    delay(greenDuration * 1000); // Wait for the green duration in seconds
    digitalWrite(greenPin, LOW); // Turn on the green light
    digitalWrite(yellowPin, HIGH); // Turn off the yellow light
    digitalWrite(redPin, LOW); // Turn off the red light
    delay(3000);
  } else if (carCount1 ==0 && carCount2 == 1) { // If between 5 and 10 cars have been detected
    digitalWrite(greenPin, HIGH); // Turn on the green light
    digitalWrite(yellowPin, LOW); // Turn off the yellow light
    digitalWrite(redPin, LOW); // Turn off the red light
    delay(greenDuration * 1000); // Wait for the green duration in seconds
    digitalWrite(greenPin, LOW); // Turn on the green light
    digitalWrite(yellowPin, HIGH); // Turn off the yellow light
    digitalWrite(redPin, LOW); // Turn off the red light
    delay(3000);
  } else { // If 10 or more cars have been detected
    digitalWrite(greenPin, HIGH); // Turn on the green light
    digitalWrite(yellowPin, LOW); // Turn off the yellow light
    digitalWrite(redPin, LOW); // Turn off the red light
    delay(greenDuration * 1000); // Wait for the green duration in seconds
    digitalWrite(greenPin, LOW); // Turn on the green light
    digitalWrite(yellowPin, HIGH); // Turn off the yellow light
    digitalWrite(redPin, LOW); // Turn off the red light
    delay(3000);
  }
  carCount1 = 0; // Reset the car count
  carCount2 = 0;
  greenDuration=10; //Reset the green duration
}