const int leftPhotocellPin = A0;
const int rightPhotocellPin = A1;
#define enAright 3 
#define enBleft 6 
void setup() {
  Serial.begin(9600); // Initialize the serial communication
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);  
  pinMode(enAright, OUTPUT);
  pinMode(enBleft, OUTPUT);
}

void loop() {
  int leftValue = analogRead(leftPhotocellPin);
  int rightValue = analogRead(rightPhotocellPin);
  analogWrite(enAright, 85);
  analogWrite(enBleft,85);

  // Calculate the difference between the left and right sensor values
  int sensorDifference = leftValue - rightValue;

  // Set a threshold value to determine when the difference is significant
  int threshold = 60; // Adjust this threshold as needed

  // Compare the sensor difference to the threshold
  if (abs(sensorDifference) > threshold) {
    if (sensorDifference > 0) {
      // Right sensor sees more black, turn RIGHT
      turnRight();
      Serial.println("TURN RIGHT");
    } else {
      // Left sensor sees more black, turn LEFT
      turnLeft();
      Serial.println("TURN LEFT");
    }
  } else {
    // No significant difference, move forward
    moveForward();
    Serial.println("Move forward");
  }

  // Add a delay or control the car's speed as needed
}

void turnLeft() {
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
}

void turnRight() {
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
}

void moveForward() {
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
}
