Make Line  Follower in 3 easy steps 

This post is the second instalment of the previously posted Line Follower .

  1. HARDWARE
  2. PROGRAMMING
  3. TROUBLESHOOTING

LINE FOLLOWER :Programming and Concepts

This section illustrates the program that runs on Arduigeek bot.Copy the program below, or click on the link below to download it as pdf.

Two sensors line follower program

int left, right;

void setup()

{

pinMode(6,INPUT);//left sensor

pinMode(7,INPUT);//right sensor

pinMode(9,OUTPUT);//left motor

pinMode(3,OUTPUT);//left motor

pinMode(10,OUTPUT);//right motor

pinMode(5,OUTPUT);//right motor

//  Serial.begin(9600);

}

void read_sensors()

{

left=digitalRead(6);

right= digitalRead(7);

}

void move_forward()

{

analogWrite(9,130);//3,9 for left motor

digitalWrite(3,LOW);

analogWrite(10,130);//10,5 for right motor

digitalWrite(5,LOW);

}

void turn_left()

{

digitalWrite(9,LOW);//9,3 for left motor

digitalWrite(3,LOW);

analogWrite(10,250);//10,5 for right motor

digitalWrite(5,LOW);

}

void turn_right()

{

analogWrite(9,250 );// 9,3 for left motor

digitalWrite(3,LOW);

digitalWrite(10,LOW);// 10,5 for right motor

digitalWrite(5,LOW);

}

void halt()

{

digitalWrite(9,LOW);// 9,3 for left motor

digitalWrite(3,LOW);

digitalWrite(10,LOW);// 10,5 for right motor

digitalWrite(5,LOW);

}

void print_readings()

{

Serial.print(” leftsensor”);

Serial.print(“\t”);

Serial.print(left);

Serial.print(“rightsensor”);

Serial.print(“\t”);

Serial.print(right);

Serial.println();

}

void loop()

{

read_sensors();

while((left==0)&&(right==1)) // left sensor is over black line

{

turn_left();

read_sensors();

print_readings();

}

while((left==1)&&(right==0)) // right sensor is over black line

{

turn_right();

read_sensors();

print_readings();

}

while((left==0)&&(right==0)) // both sensors over the back line

{

halt();

read_sensors();

print_readings();

}

while((left==1)&&(right==1))// no sensor over black line

{

move_forward();

read_sensors();

print_readings();

}

}

DESCRIPTION OF FUNCTIONS USED:

  1. read_sensors(): It takes the readings of both the sensors and stores them in the variables left and right.
  2. move_forward(): When the arduino executes this function , both the motors move in the forward direction.
  3. turn_left():Left motor stops. Bot turns left.
  4. turn_right():Right motor stops. Bot turns right.
  5. halt():Bot stops.
  6. print_readings():Displays readings of the sensors on serial monitor. For this you have to uncomment “Serial.begin(9600)” in void setup.

SENSOR READINGS:

SENSOR OVER LINE SENSOR READINGS
LEFT RIGHT
LEFT SENSOR 0 1
RIGHT SENSOR 1 0
NONE 1 1
BOTH 0 0

SPEED CONTROL:

If the speed of the motors is too high ,it might happen that before arduino interprets the sensor signals, robot loses the line. In short , bot does not follow line due to high speed. Obviously, you would want to decrease speed, and PWM is the key.

Read about PWM here.   PWM AND HOW TO DO IT IN ARDUINO.

Just decrease the numerical value in the function given below to decrease speed. Note that, you can vary it between 0 and 255 only.

Syntax: analogWrite(pin, value)

Example:  analogWrite(9,169)

0 <= value <= 255

Now, you are good to go! Give your feedback and queries on these posts in the comments. All your queries will be answered in the next post, the troubleshooting part.

One thought on “Make Line  Follower in 3 easy steps 

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: