Thursday, 30 November 2017

LINE FOLLOWER Part 4 :Coding and Run

LINE FOLLOWER Part 4 :Coding and Run

Coming to the final blog of Line Follower I have given the code for this,Copy this and directly paste it in Arduino IDE and modify according your needs.
Code is for only 3 sensor input and its good for Beginners.
Here's the Code:
_________________________________________________________________________________
//Code is for 3 sensor input//code by-Https://ganeshgadgets.blogspot.com

int ena = 11;//a-LEFT motor PWM output  
int enb = 10;//b-RIGHT motor PWM output
int ar = 12;//a reverse//
int af = 13;// aforward//
int bf = 9;//b forward//
int br = 8;//b reverse//

int s1 = 14, s2 =15, s3 =16; //3 sensors connected to analog a0,a1,a2 pins(digital equivalent)

void setup() {
  // put your setup code here, to run once:
  // declaring output pins
  pinMode(ena,OUTPUT);// declaring output pins
  pinMode(enb,OUTPUT);
  pinMode(ar,OUTPUT);
  pinMode(af,OUTPUT);
  pinMode(bf,OUTPUT);
  pinMode(br,OUTPUT);
  // declaring input pins
  pinMode(s1,INPUT);
  pinMode(s2,INPUT);
  pinMode(s3,INPUT);
  }

void loop() {
  // put your main code here, to run repeatedly:
  //case-1 FORWARD 
  if((digitalRead(s1)==HIGH) && (digitalRead(s2)==LOW) && (digitalRead(s3)==HIGH))
  {
  digitalWrite(ena, HIGH);// BOTH ENABLE PIN WILL BE HIGH FOR ALL CASES
  digitalWrite(enb, HIGH);

  digitalWrite(af, HIGH);
  digitalWrite(ar, LOW);
  digitalWrite(bf, HIGH);
  digitalWrite(br, LOW);
  }
  //case-2 SMOOTH TURN RIGHT  
  if((digitalRead(s1)==HIGH) && (digitalRead(s2)==HIGH) && (digitalRead(s3)==LOW))
  {
  digitalWrite(ena, HIGH);
  digitalWrite(enb, HIGH);

  digitalWrite(af, HIGH);
  digitalWrite(ar, LOW);
  digitalWrite(bf, HIGH);// GIVING BOTH HIGH RESULTS IN STOPPING RIGHT MOTOR
  digitalWrite(br, HIGH);
  }
  //case-3 SMOOTH TURN LEFT   
  if((digitalRead(s1)==LOW) && (digitalRead(s2)==HIGH) && (digitalRead(s3)==HIGH))
  {
  digitalWrite(ena, HIGH);
  digitalWrite(enb, HIGH);

  digitalWrite(af, HIGH);// GIVING BOTH HIGH RESULTS IN STOPPING LEFT MOTOR 
  digitalWrite(ar, HIGH);
  digitalWrite(bf, HIGH);
  digitalWrite(br, LOW);
  }
  //case-4 STOP OR START
  if((digitalRead(s1)==HIGH) && (digitalRead(s2)==HIGH) && (digitalRead(s3)==HIGH))
  {
  digitalWrite(ena, HIGH);
  digitalWrite(enb, HIGH);

  digitalWrite(af, HIGH);// GIVING ALL HIGH BOTH MOTORS WILL BE STOPPED
  digitalWrite(ar, HIGH);
  digitalWrite(bf, HIGH);
  digitalWrite(br, HIGH);
  }
  //case-5 STOP OR START
  if((digitalRead(s1)==LOW) && (digitalRead(s2)==LOW) && (digitalRead(s3)==LOW))
  {
  digitalWrite(ena, HIGH);
  digitalWrite(enb, HIGH);

  digitalWrite(af, HIGH);// GIVING ALL HIGH BOTH MOTORS WILL BE STOPPED
  digitalWrite(ar, HIGH);
  digitalWrite(bf, HIGH);
  digitalWrite(br, HIGH);
  }
  //case-6 SHARP RIGHT TURN
  if((digitalRead(s1)==HIGH) && (digitalRead(s2)==LOW) && (digitalRead(s3)==LOW))
  {
  digitalWrite(ena, HIGH);
  digitalWrite(enb, HIGH);

  digitalWrite(af, HIGH);
  digitalWrite(ar, LOW);
  digitalWrite(bf, LOW);// THIS WILL REVERSE MOTOR B
  digitalWrite(br, HIGH);
  }
  //case-7 SHARP LEFT TURN 
  if((digitalRead(s1)==LOW) && (digitalRead(s2)==LOW) && (digitalRead(s3)==HIGH))
  {
  digitalWrite(ena, HIGH);
  digitalWrite(enb, HIGH);

  digitalWrite(af, LOW);// THIS WILL REVERSE MOTOR A
  digitalWrite(ar, HIGH);
  digitalWrite(bf, HIGH);
  digitalWrite(br, LOW);
  }
  //case-8 ANTINODE DETECT 
  if((digitalRead(s1)==LOW) && (digitalRead(s2)==HIGH) && (digitalRead(s3)==LOW))
  {
  digitalWrite(ena, HIGH);
  digitalWrite(enb, HIGH);

  digitalWrite(af, HIGH);//IGNORING ANTINODE CONDITION AND MOVING FORWARD
  digitalWrite(ar, LOW);
  digitalWrite(bf, HIGH);
  digitalWrite(br, LOW);
  }
}
_________________________________________________________________________________

  • You can use more sensors in If Statement like;
 if((digitalRead(s1)==HIGH) && (digitalRead(s2)==LOW) && (digitalRead(s3)==HIGH) && (digitalRead(s4)==HIGH) && (digitalRead(s5)==HIGH) && (digitalRead(s6)==HIGH))
{........}

  • You can add speed variation in :
  analogWrite(ena, 200);//0-255
  analogWrite(enb, 200);//0-255

This is end of line follower project. For more tech and projects follow me.


LINE FOLLOWER Part 3 : CONNECTING ALL TOGETHER AND CONCEPT

LINE FOLLOWER Part 3 : CONNECTING ALL TOGETHER AND CONCEPT

Coming to the part 3 all you need is- 
  1. Arduino UNO(you can use others also,but I prefer UNO for beginners)
  2. Sensor Array(min 3 to max 5 numbers of ir sensors)
  3. Motor Driver(L298 or any other)
  4. Line follower Chassis
  5. Two DC motors
  6. Jumper Wires(male-female,female-female)
  7. Wheels and an Castro Wheel(Multi directional wheel)

Connect all the Modules According to the diagram given below:-



Points Worth Noting:-
  • All the modules should have a common ground. Every time you add any module to your project you should add ground of module to the ground pin of Arduino.
  • Connect the enable of motor Driver to any of PWM pins(3,5,6,9,10,11). It will help you to control the speed of Motor.
  • If you don't want speed Control then add enable pin to 5V of motor Driver or Vin, it will then take max speed in default.
  • Put all your sensor input to the analog pins. Its more convenient and gracious. You can use these pins as analog(A0....A5) or as digital pins(14-19). Digital input is better for line follower.
Concept Behind the Line follower Algorithm:
I will discuss the concept or the idea that can you use easily. Suppose you have 3 sensors so there are total of 8 possibilities of sensor values.
You can use the formula-2^(No. of sensors)"
If you are using 5 sensors then you have 2^5=32 possibilities.
Lets go in detail....
See the figure

  • The sensor give digital HIGH while on white surface and digital LOW on black surface or VICE VERSA if you have sensor of inverting mode(op-amp).
  • Here we are considering following black line on white surface.
  • Going case by case.
CASE
ACTION REQUIRED
LEFT MOTOR
RIGHT MOTOR
1
FORWARD
FORWARD
FORWARD
2
SMOOTH TURN RIGHT  
FORWARD
STOP
3
SMOOTH TURN LEFT  
STOP
FORWARD
4
STOP OR START
STOP OR FORWARD
STOP OR FORWARD
5
STOP OR START
STOP OR FORWARD
STOP OR FORWARD
6
SHARP RIGHT TURN
FORWARD
REVERSE
7
SHARP LEFT TURN
REVERSE
FORWARD
8
ANTINODE DETECT
STOP OR FORWARD
STOP OR FORWARD


  • For simplicity here speed control is not disscussed. You can do that using Enable pin and analogWrite(pin number, 0-255) in arduino codes.
  • Codes to the line follower bot is in next blog.