const int irPin1 = 3;
const int irPin2 = 5;
const int irPin3 = 7;
int ir1State = 0;
int ir2State = 0;
int ir3State = 0;

const int pinI1=8;     // motor 1 pin1
const int pinI2=11;    //motor 1 pin2
const int speedpinA=9; //EA(PWM)to control the motor_1 speed
const int pinI3=12;    //motor 2 pin 1
const int pinI4=13;    //motor 2 pin 2
const int speedpinB=10;//EB(PWM]) to control the motor_2 speed
void setup()
{
    pinMode(pinI1,OUTPUT); //set to output
    pinMode(pinI2,OUTPUT);
    pinMode(speedpinA,OUTPUT);
    pinMode(pinI3,OUTPUT);
    pinMode(pinI4,OUTPUT);
    pinMode(speedpinB,OUTPUT);
}
void loop()
{
    ir1State = digitalRead(irPin1);
    ir2State = digitalRead(irPin2);
    ir3State = digitalRead(irPin3);
    if((ir1State != 0)&(ir2State == 0)&(ir3State == 0)){
      //if(ir1State != 1){
      Test_Load_Left();

      }
    if((ir1State == 0)&(ir2State != 0)&(ir3State == 0)){
      //if(ir2State != 1){
      go_straight();
     
      }
    if((ir1State == 0)&(ir2State == 0)&(ir3State != 0)){
     //if(ir3State != 1){
       Test_Load_Right();
       
      }
    if((ir1State != 1)&(ir2State != 1)&(ir3State != 1))
    {
      clean_Output();
      }
    //delay(20);
     
}
//Set the speed of motor1 faster than motor2, with speed 150
void Test_Load_Left()
{
  analogWrite(speedpinA,50);
  analogWrite(speedpinB,100);
  digitalWrite(pinI1,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI4,LOW);

}
//Set the speed of motor2 faster than motor1, with speed 150
void Test_Load_Right()
{
  analogWrite(speedpinA,100);
  analogWrite(speedpinB,50);
  digitalWrite(pinI1,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI4,LOW);

  }

//Stop the motors
void clean_Output()
{
  digitalWrite(speedpinA,LOW);// full PWM 255
  digitalWrite(speedpinB,LOW);
}

//Set the same speed for two motor
void go_straight()
{
  analogWrite(speedpinA,100);
  analogWrite(speedpinB,100);  
  digitalWrite(pinI1,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI4,LOW);
}
