Facebook
From Expert TTA, 2 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 267
  1. #define m1 5  //Right Motor MA1
  2. #define m2 4  //Right Motor MA2
  3. #define m3 2  //Left Motor MB1
  4. #define m4 3  //Left Motor MB2
  5. #define e1 11  //Right Motor Enable Pin EA
  6. #define e2 10 //Left Motor Enable Pin EB
  7.  
  8. //**********5 Channel IR Sensor Connection**********//
  9. #define ir1 A0
  10. #define ir2 A1
  11. #define ir3 A2
  12. #define ir4 A3
  13. #define ir5 A4
  14. //*************************************************//
  15.  
  16. int speed = 150;
  17.  
  18. void setup() {
  19.   pinMode(m1, OUTPUT);
  20.   pinMode(m2, OUTPUT);
  21.   pinMode(m3, OUTPUT);
  22.   pinMode(m4, OUTPUT);
  23.   pinMode(e1, OUTPUT);
  24.   pinMode(e2, OUTPUT);
  25.   pinMode(ir1, INPUT);
  26.   pinMode(ir2, INPUT);
  27.   pinMode(ir3, INPUT);
  28.   pinMode(ir4, INPUT);
  29.   pinMode(ir5, INPUT);
  30. }
  31.  
  32. void loop() {
  33.   //Reading Sensor Values
  34.   int s1 = digitalRead(ir1);  //Left Most Sensor
  35.   int s2 = digitalRead(ir2);  //Left Sensor
  36.   int s3 = digitalRead(ir3);  //Middle Sensor
  37.   int s4 = digitalRead(ir4);  //Right Sensor
  38.   int s5 = digitalRead(ir5);  //Right Most Sensor
  39.  
  40.   //if only middle sensor detects black line
  41.   if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  42.   {
  43.     //going forward with full speed
  44.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  45.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  46.     digitalWrite(m1, HIGH);
  47.     digitalWrite(m2, LOW);
  48.     digitalWrite(m3, HIGH);
  49.     digitalWrite(m4, LOW);
  50.   }
  51.  
  52.   //if only left sensor detects black line
  53.   if((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  54.   {
  55.     //going right with full speed
  56.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  57.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  58.     digitalWrite(m1, HIGH);
  59.     digitalWrite(m2, LOW);
  60.     digitalWrite(m3, LOW);
  61.     digitalWrite(m4, LOW);
  62.   }
  63.  
  64.   //if only left most sensor detects black line
  65.   if((s1 == 0) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  66.   {
  67.     //going right with full speed
  68.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  69.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  70.     digitalWrite(m1, HIGH);
  71.     digitalWrite(m2, LOW);
  72.     digitalWrite(m3, LOW);
  73.     digitalWrite(m4, HIGH);
  74.   }
  75.  
  76.   //if only right sensor detects black line
  77.   if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
  78.   {
  79.     //going left with full speed
  80.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  81.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  82.     digitalWrite(m1, LOW);
  83.     digitalWrite(m2, LOW);
  84.     digitalWrite(m3, HIGH);
  85.     digitalWrite(m4, LOW);
  86.   }
  87.  
  88.   //if only right most sensor detects black line
  89.   if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))
  90.   {
  91.     //going left with full speed
  92.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  93.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  94.     digitalWrite(m1, LOW);
  95.     digitalWrite(m2, HIGH);
  96.     digitalWrite(m3, HIGH);
  97.     digitalWrite(m4, LOW);
  98.   }
  99.  
  100.   //if middle and right sensor detects black line
  101.   if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1))
  102.   {
  103.     //going left with full speed
  104.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  105.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  106.     digitalWrite(m1, LOW);
  107.     digitalWrite(m2, LOW);
  108.     digitalWrite(m3, HIGH);
  109.     digitalWrite(m4, LOW);
  110.   }
  111.  
  112.   //if middle and left sensor detects black line
  113.   if((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  114.   {
  115.     //going right with full speed
  116.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  117.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  118.     digitalWrite(m1, HIGH);
  119.     digitalWrite(m2, LOW);
  120.     digitalWrite(m3, LOW);
  121.     digitalWrite(m4, LOW);
  122.   }
  123.  
  124.   //if middle, left and left most sensor detects black line
  125.   if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  126.   {
  127.     //going right with full speed
  128.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  129.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  130.     digitalWrite(m1, HIGH);
  131.     digitalWrite(m2, LOW);
  132.     digitalWrite(m3, LOW);
  133.     digitalWrite(m4, LOW);
  134.   }
  135.  
  136.   //if middle, right and right most sensor detects black line
  137.   if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  138.   {
  139.     //going left with full speed
  140.     analogWrite(e1, speed); //you can adjust the speed of the motors from 0-255
  141.     analogWrite(e2, speed); //you can adjust the speed of the motors from 0-255
  142.     digitalWrite(m1, LOW);
  143.     digitalWrite(m2, LOW);
  144.     digitalWrite(m3, HIGH);
  145.     digitalWrite(m4, LOW);
  146.   }
  147.  
  148.   //if all sensors are on a black line
  149.   if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  150.   {
  151.     //stop
  152.     digitalWrite(m1, LOW);
  153.     digitalWrite(m2, LOW);
  154.     digitalWrite(m3, LOW);
  155.     digitalWrite(m4, LOW);
  156.   }
  157. }

Replies to Line Follower rss

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