Trying to make a Robot follow a light

Hello, I'm fairly new to arduino but university is trying to make me learn it. I'm currently building a robot that follow a light using 2 LDR sensor placed in 45degree on the front end of the robot to sense the light moving from left to right.

I have attached the code to this topic. The problem seems fairly simple but I cannot see it.

What I want to happen: right servo slow down then Right sensor is above the offset, meaning the light shines on the right side, and same thing for left side. The robot has only 2 wheel on stiff joint so it basically turn like a tank, a ball on the front end stabilize the robot.

What's currently happening: The servo starts smoothly and reach the maximum value of 180, but if i cover the light sensor with my hand or shine a flashlight on it, the servo will keep running at max speed.

I've been looking at my code for an hour or so and ran out of idea on how to fix this. So I'm turning to this forum!

any help or advice is appreciated! :slight_smile:

#include <Servo.h>

Servo leftServo;
Servo rightServo;

int LLDR = analogRead(2);
int RLDR = analogRead(3);
int startSpeed = 180;
int lspeed = startSpeed;
int rspeed = startSpeed;
int rotate = 5;
int offset = 100;

void LRoffset()// adjust to the light movement left/right
{
if (LLDR > (RLDR + offset))
{
lspeed -= rotate;
leftServo.write(lspeed);
delay(100);
}
if (RLDR > (LLDR + offset))
{
rspeed -= rotate;
rightServo.write(rspeed);
delay(100);
}
lspeed = startSpeed;
rspeed = startSpeed;
leftServo.write(lspeed);
rightServo.write(rspeed);
}

void setup()
{
Serial.begin(9600);
leftServo.attach(9);
rightServo.attach(8);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
for (int pos = 90; pos <= 180; pos += 2)
{
leftServo.write(pos);
rightServo.write(pos);
delay(100);
}
}

void loop()
{
LRoffset();
Serial.println(LLDR);
}

arduinoproject1.txt (950 Bytes)

the servo will keep running at max speed.

  lspeed = startSpeed;
  rspeed = startSpeed;
  leftServo.write(lspeed);
  rightServo.write(rspeed);

That is what you ask for.

You have to asses that the bot is in the right direction at all times. Both servos should never go at full speed unless the light is straight ahead.

Jacques

EDIT: The ratio of light recieved by the left & right sensors should be proportional to the speed at which the left and right servos turn at all times. (Or inversley proportional if the freewheel is at the back)

I see, that does give me some idea on how to work around the code to make it properly work. I'll rebuild it and see what I can do!

Thank you :slight_smile:

For a differential drive robot like yours, first you have to adjust the relative speeds of right and left so that it travels straight ahead. Call these two numbers lspeed and rspeed (and they should be somewhere in the middle of the speed range).

Then, one way to follow a light is to frequently, i.e. every time through the loop, calculate the difference between left and right illumination and adjust the motor speeds differentially.

For example:

  float K = 0.5; //adjust this value to get reasonable response
  delta_illum = right_illum - left illum;
  delta_speed = K*(delta_illum);
  leftServo.write(lspeed + delta_speed);
  rightServo.write(rspeed - delta_speed);

Note that if right_illum is greater than left_illum you need to speed up the left motor and slow down the right.

Finally, you will have to put in some checks so that nonsensical speed values are not written to the motors.

Your code only reads the light sensors one time. The amount of light falling on the sensors after that has no affect on what the robot does. Surely that is not what you want.

In LRoffset(), I'd expect you to call analogRead() as the FIRST thing you do. And, the second thing, too, to read the other sensor.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hi,
I think you need to do some online arduino tutorials.

int LLDR = analogRead(2);[color=#222222][/color]
int RLDR = analogRead(3);

Where you have placed it, this will only read once and never again.

You need to learn the program structure.

Tom... :o