#define left_Sensor     1          // LDR sensors connected to analog pins 1 and 3

#define right_Sensor   3

 

#define left_Motor_Output1    4          // Left motor outputs connected to pin 4 & 2, PWM on pin 9

#define left_Motor_Output2    2

#define left_Motor_Pwm         9

 

#define right_Motor_Output1  7          // Right motor outputs connected to pin 7 & 5, PWM on pin 11

#define right_Motor_Output2  5

#define right_Motor_Pwm       11

 

int left_Sensor_Val = 0;  // Left and right LDR sensor initial values and mapped values

int right_Sensor_Val = 0;

int mapped_Left = 0;

int mapped_Right = 0;

 

int left_Motor_Speed = 0;       // Right and left motor speed inital values and minimum limit to check before changing direction

int right_Motor_Speed = 0;

int limit = 5;

 

void setup() {

 

  Serial.begin(9600); // Initialize serial communication @9600 bps

 

  pinMode(left_Motor_Output1, OUTPUT);      // sets all pins as output

  pinMode(left_Motor_Output2, OUTPUT);

  pinMode(left_Motor_Pwm, OUTPUT);

  pinMode(right_Motor_Output1, OUTPUT);

  pinMode(right_Motor_Output2, OUTPUT);

  pinMode(right_Motor_Pwm, OUTPUT);

}

 

void loop(){

 

  // Read values from left and right sensors, and then scale down the values to a range between 0 - 255. Intial values that are obtained are within 0 - 1024.

            left_Sensor_Val = analogRead(left_Sensor);

            right_Sensor_Val = analogRead(right_Sensor);  

            mapped_Left = map(left_Sensor_Val,0,1023,0,255);

            mapped_Right = map(right_Sensor_Val,0,1023,0,255);

 

  // Assign mapped values from the right sensor to left motor and vice versa. ie, if right sensor receives more light, readings will output higher values and this is passed on to the left motor to turn it faster than the right motor.

           

left_Motor_Speed = mapped_Right;

            right_Motor_Speed = mapped_Left;

 

 // Test code > trying to lower speed values of one motor to make it turn slower than the other...

 

int  left_Motor_Low = left_Motor_Speed / 2;

int  right_Motor_Low = right_Motor_Speed /2; 

 

 // Case 1: If left reading > right reading AND (left reading - right reading) > limit then send a low PWM pulse to LEFT motor, so that it turns slower.

 

if (mapped_Left > mapped_Right && (mapped_Left - mapped_Right) > limit) {                   

                        analogWrite(left_Motor_Pwm, left_Motor_Low); // PWM pulse sent through 'analogWrite'. note that 2nd value is 'left_Motor_Low'.

                        digitalWrite(left_Motor_Output1, HIGH);

                        digitalWrite(left_Motor_Output2,LOW);

                       

 // Right motor turns at speed propotional to reading from left sensor, which in this case is much higher than the right sensor.

                        analogWrite(right_Motor_Pwm, right_Motor_Speed); // 2nd value of analogWrite is 'right_Motor_Speed'.

                        digitalWrite(right_Motor_Output1, LOW);

                        digitalWrite(right_Motor_Output2,HIGH);

            }

 

 // Case 2: The exact opposite of Case 1. If the right sensor gets more light, then turn the RIGHT motor slower than the left.

 

else if (mapped_Right > mapped_Left && (mapped_Right - mapped_Left) > limit) {

                       

                        analogWrite(left_Motor_Pwm, left_Motor_Speed );

                        digitalWrite(left_Motor_Output1,LOW);

                        digitalWrite(left_Motor_Output2,HIGH);

                       

                        analogWrite(right_Motor_Pwm, right_Motor_Low);

                        digitalWrite(right_Motor_Output1,HIGH);

                        digitalWrite(right_Motor_Output2,LOW);

            }

 

 // Case 3: If both sensors receive almost the same light then both motors turn at equal speed.

 

 else  {

 

                        analogWrite(left_Motor_Pwm, left_Motor_Speed);

                        digitalWrite(left_Motor_Output1,LOW);

                        digitalWrite(left_Motor_Output2,HIGH);

                       

                        analogWrite(right_Motor_Pwm, right_Motor_Speed);

                        digitalWrite(right_Motor_Output1, LOW);

                        digitalWrite(right_Motor_Output2,HIGH);

            }

           

// test code that outputs sensor values, mapped values and motor speed to console

 

  Serial.print("left_Sensor_Val and mapped_Left = ");

  Serial.print(left_Sensor_Val);

  Serial.print("\t");

  Serial.println(mapped_Left);

 

  Serial.print("right_Sensor_Val and mapped_Right = ");

  Serial.print(right_Sensor_Val);

  Serial.print("\t");

  Serial.println(mapped_Right);

 

 Serial.print("Left motor speed and Low value = ");

 Serial.print(left_Motor_Speed);

 Serial.print("\t");

 Serial.println(left_Motor_Low);

 

 Serial.print("Right motor speed and Low value = ");

 Serial.print(right_Motor_Speed);

 Serial.print("\t");

 Serial.println(right_Motor_Low);

 Serial.println();

 

 delay(50);

}