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.


No comments:

Post a Comment