Facebook
From Expert TTA, 2 Months ago, written in C++.
This paste is a reply to Line Follower from Expert TTA - go back
Embed
Viewing differences between Line Follower and Re: Line Follower
#define m1 5  //Right Motor MA1
#define m2 4  //Right Motor MA2
#define m3 2  //Left Motor MB1
#define m4 3  //Left Motor MB2
#define e1 11  //Right Motor Enable Pin EA
#define e2 10 //Left Motor Enable Pin EB

//**********5 Channel IR Sensor Connection**********//
#define ir1 A0
A1
#define ir2 A1
A2
#define ir3 A2
A3
#define ir4 A3
A4
#define ir5 A4
A5
//*************************************************//

int speed = 150;

void setup() {
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(m3, OUTPUT);
  pinMode(m4, OUTPUT);
  pinMode(e1, OUTPUT);
  pinMode(e2, OUTPUT);
  
  pinMode(ir1, INPUT);
  pinMode(ir2, INPUT);
  pinMode(ir3, INPUT);
  pinMode(ir4, INPUT);
  pinMode(ir5, INPUT);
INPUT);

}

void loop() {
  
{

  CarTest();

  // 
//Reading Sensor Values
  // int s1 = digitalRead(ir1);  //Left Most Sensor
  // int s2 = digitalRead(ir2);  //Left Sensor
  // int s3 = digitalRead(ir3);  //Middle Sensor
  // int s4 = digitalRead(ir4);  //Right Sensor
  // int s5 = digitalRead(ir5);  //Right Most Sensor

  // //if only middle sensor detects black line
  // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  {
    
// {
  //   
//going forward with full speed 
    analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    digitalWrite(m1, HIGH);
    digitalWrite(m2, LOW);
    digitalWrite(m3, HIGH);
    digitalWrite(m4, LOW);
  
speed 
  //   Forward(speed);
  // 
}
  
  // //if only left sensor detects black line
  // if((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  {
    
// {
  //   
//going right with full speed 
    analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    digitalWrite(m1, HIGH);
    digitalWrite(m2, LOW);
    digitalWrite(m3, LOW);
    digitalWrite(m4, LOW);
  
speed 
  //   Right(speed);
  // 
}
  
  // //if only left most sensor detects black line
  // if((s1 == 0) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  {
    
// {
  //   
//going right with full speed 
    
speed 
  //   Right(speed);
  // }

  // //if only right sensor detects black line
  // if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
  // {
  //   //going left with full speed 
  //   Left(speed);
  // }

  // //if only right most sensor detects black line
  // if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))
  // {
  //   //going left with full speed 
  //   Left(speed);
  // }

  // //if middle and right sensor detects black line
  // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1))
  // {
  //   //going left with full speed 
  //   Left(speed);
  // }

  // //if middle and left sensor detects black line
  // if((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  // {
  //   //going right with full speed
  //   Right(speed);
  // }

  // //if middle, left and left most sensor detects black line
  // if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  // {
  //   //going right with full speed
  //   Right(speed);
  // }

  // //if middle, right and right most sensor detects black line
  // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  // {
  //   //going left with full speed
  //   Left(speed);
  // }

  // //if all sensors are on a black line
  // if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  // {
  //   //stop
  //   Stop();
  // }

  // delay(100);
}

void Forward(int speed){
  
analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
  
digitalWrite(m1, HIGH);
    
HIGH);
  
digitalWrite(m2, LOW);
    
LOW);
  
digitalWrite(m3, LOW);
    
HIGH);
  
digitalWrite(m4, HIGH);
  }

  //if only right sensor detects black line
  if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
  {
    //going left with full speed 
    
LOW);
}

void Backward(int speed){
  
analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
  
digitalWrite(m1, LOW);
    
LOW);
  
digitalWrite(m2, LOW);
    
HIGH);
  
digitalWrite(m3, HIGH);
    
LOW);
  
digitalWrite(m4, LOW);
  }

  //if only right most sensor detects black line
  if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))
  {
    //going left with full speed 
    
HIGH);
}

void Right(int speed){
  
analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
  
digitalWrite(m1, LOW);
    
HIGH);
  
digitalWrite(m2, HIGH);
    
LOW);
  
digitalWrite(m3, HIGH);
    
LOW);
  
digitalWrite(m4, LOW);
  }

  //if middle and right sensor detects black line
  if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1))
  {
    //going left with full speed 
    
LOW);
}

void Left(int speed){
  
analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    
0-255
  
  
digitalWrite(m1, LOW);
    
LOW);
  
digitalWrite(m2, LOW);
    
LOW);
  
digitalWrite(m3, HIGH);
    
HIGH);
  
digitalWrite(m4, LOW);
  }

  //if middle and left sensor detects black line
  if((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  {
    //going right with full speed 
    analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    
LOW);
}

void Stop(){
  
digitalWrite(m1, HIGH);
    
LOW);
  
digitalWrite(m2, LOW);
    
LOW);
  
digitalWrite(m3, LOW);
    
LOW);
  
digitalWrite(m4, LOW);
  }

  //if middle, left and left most sensor detects black line
  if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  {
    //going right with full speed 
    analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    digitalWrite(m1, HIGH);
    digitalWrite(m2, LOW);
    digitalWrite(m3, LOW);
    digitalWrite(m4, LOW);
  }

  //if middle, right and right most sensor detects black line
  if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  {
    //going left with full speed 
    analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
    analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
    digitalWrite(m1, LOW);
    digitalWrite(m2, LOW);
    digitalWrite(m3, HIGH);
    digitalWrite(m4, LOW);
  }

  //if all sensors are on a black line
  if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  {
    //stop
    digitalWrite(m1, LOW);
    digitalWrite(m2, LOW);
    digitalWrite(m3, LOW);
    digitalWrite(m4, LOW);
  }
LOW);
}


void CarTest(){
  Forward(150);
  delay(1000);

  Backward(150);
  delay(1000);
  
  Right(150);
  delay(1000);
  
  Left(150);
  delay(1000);
  
  Stop();
  delay(1000);
}

Replies to Re: Line Follower rss

Title Name Language When
Re: Re: Line Follower Expert TTA cpp 2 Months ago.