Arduino Light Following robot

Hi I am new to arduino wonder if somebody could help me with my code for my light following robot, I am having issues with my robot it just isn’t working and I was wondering if I had done something wrong in the code? The robot is using two motors and an L293D motor chip. Thanks for any responses.

int motor1Enable1 = 0; // pin 1 on L293D
int motor1Input1 = 1; // pin 2 on L293D
int motor1Input2 = 2; // pin 7 on L293D
int motor2Enable2 = 3; // pin 9 on L293D
int motor2Input3 = 4; // pin 10 on L293D
int motor2Input4 = 5; // pin 15 on L293D
int rightLightSensor = A0;
int leftLightSensor = A1;
int pushButton = 6;
// Variables to store ADC Value:
int sensorValueRight = 0;
int sensorValueLeft = 0;

void setup ()
{
// Set sensors and button as inputs.
pinMode(rightLightSensor, INPUT);
pinMode(leftLightSensor, INPUT);
pinMode(pushButton, INPUT);

// Set all motor pins as outputs.
pinMode(motor1Enable1, OUTPUT);
pinMode(motor1Input1, OUTPUT);
pinMode(motor1Input2, OUTPUT);
pinMode(motor2Enable2, OUTPUT);
pinMode(motor2Input3, OUTPUT);
pinMode(motor2Input4, OUTPUT);

// Turn motors off.
digitalWrite(motor1Enable1, HIGH);
digitalWrite(motor1Input1, LOW);
digitalWrite(motor1Input2, LOW);
digitalWrite(motor2Enable2, HIGH);
digitalWrite(motor2Input3, LOW);
digitalWrite(motor2Input4, LOW);
}

void loop ()
{

// read the value from the right sensor:
sensorValueRight = analogRead(rightLightSensor);
// read the value from the left sensor:
sensorValueLeft = analogRead(leftLightSensor);
// Compares sensor values if one is great different sub routine occurs
if (sensorValueRight >= sensorValueLeft)
{
turningRight();
}
else
{
turningLeft();
}
}

void turningRight ()
{
// The integer “difference” stores the value obtained by the subtraction of analog read Right and Left.
int difference = sensorValueRight - sensorValueLeft;
if (difference > 50)
{
turnRight();
}

if (50 > difference > 25)
{
driveForward();
}

if (difference < 25)
{
stopMoving();
}
}

void turningLeft ()
{
// The integer “difference” stores the value obtained by the subtraction of analog read Left and Right.
int difference = sensorValueLeft - sensorValueRight;
if (difference > 50)
{
turnLeft();
}
if (50 > difference > 25)
{
driveForward();
}

if (difference < 25)
{
stopMoving();
}
}

void turnRight ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2, HIGH);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, LOW);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
int difference = sensorValueRight - sensorValueLeft;
if (50 > difference > 25)
{
driveForward();
}
if (difference < 25)
{
stopMoving();
}
}

void turnLeft ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2,LOW);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, HIGH);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
int difference = sensorValueLeft - sensorValueRight;
if (50 > difference > 25)
{
driveForward();
}
if (difference < 25)
{
stopMoving();
}
}

void driveForward ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2,HIGH);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, HIGH);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
int difference = sensorValueLeft - sensorValueRight;
if (sensorValueRight >= sensorValueLeft)
{
turningRight();
}
else
{
turningLeft();
}
}

void stopMoving ()
{
digitalWrite (motor1Enable1, HIGH);
digitalWrite (motor1Input1, LOW);
digitalWrite (motor1Input2,LOW);
digitalWrite (motor2Enable2, HIGH);
digitalWrite (motor2Input3, LOW);
digitalWrite (motor2Input4, LOW);
sensorValueRight = analogRead(rightLightSensor);
sensorValueLeft = analogRead(leftLightSensor);
if (sensorValueRight >= sensorValueLeft)
{
turningRight();
}
else
{
turningLeft();
}
}

  if (50 > difference > 25)

This does not mean what you seem to think it means. It will evaluate (50 > difference) to get a 0 (false) or 1 (true) answer. It will then compare that 0 or 1 against 25 and the result will always be 0 (false).

You probably want:

  if (difference > 25 && difference < 50)

Thanks a lot! Yeah I see I misunderstood what that meant. Seems to have fixed my issues with the robot.

The robot seems to be working okay although after about 30 seconds it just stops responding to changes in light, I would gratefully appreciate any advice on refining my code so that it runs properly. And again thanks for any responses!

You changed your code. You need to post the current code.

You need to post the current code.

Properly, this time, please.

Hi here is the current code below:

int motor1Enable1 = 0; // pin 1 on L293D
int motor1Input1 = 1; // pin 2 on L293D
int motor1Input2 = 2; // pin 7 on L293D
int motor2Enable2 = 3; // pin 9 on L293D
int motor2Input3 = 4; // pin 10 on L293D
int motor2Input4 = 5; // pin 15 on L293D
int rightLightSensor = A0;
int leftLightSensor = A1;
// Variables to store ADC Value:
int sensorValueRight = 0;
int sensorValueLeft = 0;

void setup ()
{
   // Set sensors and button as inputs.
  pinMode(rightLightSensor, INPUT);
  pinMode(leftLightSensor, INPUT);
  
  // Set all motor pins as outputs.
  pinMode(motor1Enable1, OUTPUT);
  pinMode(motor1Input1, OUTPUT);
  pinMode(motor1Input2, OUTPUT);
  pinMode(motor2Enable2, OUTPUT);
  pinMode(motor2Input3, OUTPUT);
  pinMode(motor2Input4, OUTPUT);
  
  // Turns motors off
  digitalWrite (motor1Enable1, HIGH);
  digitalWrite (motor1Input1, HIGH);
  digitalWrite (motor1Input2, HIGH);
  digitalWrite (motor2Enable2, HIGH);
  digitalWrite (motor2Input3, HIGH);
  digitalWrite (motor2Input4, HIGH);
  delay(250);

}
  
  void loop ()
  {

      // read the value from the right sensor:
      sensorValueRight = analogRead(rightLightSensor);
      // read the value from the left sensor:
      sensorValueLeft = analogRead(leftLightSensor);
      // Compares sensor values if one is great different sub routine occurs
      if (sensorValueRight >= sensorValueLeft)
       {
        turningRight();
       }
      else
     {
      turningLeft();
     }
      
  } 
  
  void turningRight ()
  {
    sensorValueRight = analogRead(rightLightSensor);
    sensorValueLeft = analogRead(leftLightSensor);
    // The integer "difference" stores the value obtained by the subtraction of analog read Right and Left.
    int difference = sensorValueRight - sensorValueLeft; 
    if (difference > 50)
      {
        turnRight();
      }
    
     if (difference > 25 && difference < 50) 
      {
        driveForward();
      }
      
     if (difference < 25)
      {
        stopMoving();
      }
  } 
  
  void turningLeft ()
  {
    sensorValueRight = analogRead(rightLightSensor);
    sensorValueLeft = analogRead(leftLightSensor);
  // The integer "difference" stores the value obtained by the subtraction of analog read Left and Right.
  int difference = sensorValueLeft - sensorValueRight; 
  if (difference > 50) 
      {
        turnLeft();
      }
     if (difference > 25 && difference < 50) 
      {
        driveForward();
      }
      
     if (difference < 25)
      {
        stopMoving();
      }
  } 
  
  void turnRight ()
  {
    digitalWrite (motor1Enable1, HIGH);
    digitalWrite (motor1Input1, HIGH);
    digitalWrite (motor1Input2, LOW);
    digitalWrite (motor2Enable2, HIGH);
    digitalWrite (motor2Input3, LOW);
    digitalWrite (motor2Input4, LOW);
    sensorValueRight = analogRead(rightLightSensor);
    sensorValueLeft = analogRead(leftLightSensor);
    int difference = sensorValueRight - sensorValueLeft;
     if (difference > 25 && difference < 50)  
    {
      driveForward();
    }
    if (difference < 25)
    {
      stopMoving();
    }
  }
  
  void turnLeft ()
  {
    digitalWrite (motor1Enable1, HIGH);
    digitalWrite (motor1Input1, LOW);
    digitalWrite (motor1Input2,LOW);
    digitalWrite (motor2Enable2, HIGH);
    digitalWrite (motor2Input3, HIGH);
    digitalWrite (motor2Input4, LOW);  
    sensorValueRight = analogRead(rightLightSensor);
    sensorValueLeft = analogRead(leftLightSensor); 
    int difference = sensorValueLeft - sensorValueRight;
     if (difference > 25 && difference < 50) 
      {
        driveForward();
      }
    if (difference < 25)
      {
        stopMoving();
      }
  }
    
  void driveForward ()
  {
    digitalWrite (motor1Enable1, HIGH);
    digitalWrite (motor1Input1, HIGH);
    digitalWrite (motor1Input2,LOW);
    digitalWrite (motor2Enable2, HIGH);
    digitalWrite (motor2Input3, HIGH);
    digitalWrite (motor2Input4, LOW);
    sensorValueRight = analogRead(rightLightSensor);
    sensorValueLeft = analogRead(leftLightSensor); 
  int difference = sensorValueLeft - sensorValueRight;
     if (sensorValueRight >= sensorValueLeft)   
       {
        turningRight();
       }
      else
      {
      turningLeft();
      }
  }
  
  void stopMoving ()
  { 
    digitalWrite (motor1Enable1, HIGH);
    digitalWrite (motor1Input1, LOW);
    digitalWrite (motor1Input2,LOW);
    digitalWrite (motor2Enable2, HIGH);
    digitalWrite (motor2Input3, LOW);
    digitalWrite (motor2Input4, LOW);
    sensorValueRight = analogRead(rightLightSensor);
    sensorValueLeft = analogRead(leftLightSensor);  
  if (sensorValueRight >= sensorValueLeft)
       {
        turningRight();
       }
      else
     {
      turningLeft();
     }
  }
      // read the value from the right sensor:
      sensorValueRight = analogRead(rightLightSensor);
      // read the value from the left sensor:
      sensorValueLeft = analogRead(leftLightSensor);

Are you getting
the correct values?
Where are your Serial.print()
statements?

Why does
your code wander
all over the place?

Use Tools + Auto Format before posting code.

Your biggest problem is all the recursive calls. loop() might call turningRight() which could call turnRight() which could call driveForward() which could call turningRight() which starts the recursive process.

Stop that crap.

Thanks for the advice! I have tested before to ensure im getting correct analog values. I get what your saying with the recursive code, but I'm not sure how to do it instead, What is an alternative?

What is an alternative?

The loop() function makes all the decisions, and calls the appropriate (simple) function - forward(), stop(), turnLeft(), turnRight(), etc. The direction control functions do NOT read the sensors and they do NOT make any decisions.

PaulS:
Where are your Serial.print() statements?

Can’t use Serial because pins 0 and 1 (the Serial I/O pins) are being used for motor control:

int motor1Enable1 = 0; // pin 1 on L293D
int motor1Input1 = 1; // pin 2 on L293D

bro! can you help me with the codes i m using arduino uno and motor driver l293d .

is your light follower working??? i need an urgent help to fix it.

Tons of info about this online... Try www.google.com then come back when you have some attempted code for us to examine.