Facebook
From Expert TTA, 1 Year ago, written in C++.
This paste is a reply to Re: Re: Line Follower from Expert TTA - view diff
Embed
Download Paste or View Raw
Hits: 298
  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 A1
  10. #define ir2 A2
  11. #define ir3 A3
  12. #define ir4 A4
  13. #define ir5 A5
  14. //*************************************************//
  15.  
  16. int speed = 150;
  17.  
  18. void setup()
  19. {
  20.   pinMode(m1, OUTPUT);
  21.   pinMode(m2, OUTPUT);
  22.   pinMode(m3, OUTPUT);
  23.   pinMode(m4, OUTPUT);
  24.   pinMode(e1, OUTPUT);
  25.   pinMode(e2, OUTPUT);
  26.  
  27.   pinMode(ir1, INPUT);
  28.   pinMode(ir2, INPUT);
  29.   pinMode(ir3, INPUT);
  30.   pinMode(ir4, INPUT);
  31.   pinMode(ir5, INPUT);
  32.  
  33.   Serial.begin(9600);
  34.   Serial.println("Start");
  35.   delay(1000);
  36. }
  37.  
  38. void loop()
  39. {
  40.  
  41.   // CarTest();
  42.  
  43.   // //Reading Sensor Values
  44.   int s1 = digitalRead(ir1); // Left Most Sensor
  45.   int s2 = digitalRead(ir2); // Left Sensor
  46.   int s3 = digitalRead(ir3); // Middle Sensor
  47.   int s4 = digitalRead(ir4); // Right Sensor
  48.   int s5 = digitalRead(ir5); // Right Most Sensor
  49.  
  50.   Serial.print(s1);
  51.   Serial.print(" ");
  52.   Serial.print(s2);
  53.   Serial.print("");
  54.   Serial.print(s3);
  55.   Serial.print(" ");
  56.   Serial.print(s4);
  57.   Serial.print(" ");
  58.   Serial.print(s5);
  59.   Serial.print(" ");
  60.  
  61.   // delay(500);
  62.  
  63.   // print Froward , Right , Left in the serial monitor
  64.   if (s1 == 0 && s2 == 0 && s3 == 1 && s4 == 0 && s5 == 0)
  65.   {
  66.     Serial.println("Forward");
  67.   }
  68.   else if (s1 == 1 && s2 == 1 && s3 == 0 && s4 == 0 && s5 == 0)
  69.   {
  70.     Serial.println("Right");
  71.   }
  72.   else if (s1 == 1 && s2 == 0 && s3 == 0 && s4 == 0 && s5 == 0)
  73.   {
  74.     Serial.println("Right");
  75.   }
  76.   else if (s1 == 0 && s2 == 1 && s3 == 0 && s4 == 0 && s5 == 0)
  77.   {
  78.     Serial.println("Right");
  79.   }
  80.   else if (s1 == 0 && s2 == 1 && s3 == 1 && s4 == 0 && s5 == 0)
  81.   {
  82.     Serial.println("Right");
  83.   }
  84.   else if (s1 == 0 && s2 == 0 && s3 == 0 && s4 == 1 && s5 == 1)
  85.   {
  86.     Serial.println("Left");
  87.   }
  88.   else if (s1 == 0 && s2 == 0 && s3 == 0 && s4 == 0 && s5 == 1)
  89.   {
  90.     Serial.println("Left");
  91.   }
  92.   else if (s1 == 0 && s2 == 0 && s3 == 0 && s4 == 1 && s5 == 0)
  93.   {
  94.     Serial.println("Left");
  95.   }
  96.   else if (s1 == 0 && s2 == 0 && s3 == 1 && s4 == 1 && s5 == 0)
  97.   {
  98.     Serial.println("Left");
  99.   }
  100.   else if (s1 == 0 && s2 == 0 && s3 == 0 && s4 == 0 && s5 == 0)
  101.   {
  102.     Serial.println("Stop");
  103.   }
  104.   else
  105.   {
  106.     Serial.println("eroor");
  107.   }
  108.  
  109.   delay(500);
  110.  
  111.   // //if only middle sensor detects black line
  112.   // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  113.   // {
  114.   //   //going forward with full speed
  115.   //   Forward(speed);
  116.   // }
  117.  
  118.   // //if only left sensor detects black line
  119.   // if((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  120.   // {
  121.   //   //going right with full speed
  122.   //   Right(speed);
  123.   // }
  124.  
  125.   // //if only left most sensor detects black line
  126.   // if((s1 == 0) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  127.   // {
  128.   //   //going right with full speed
  129.   //   Right(speed);
  130.   // }
  131.  
  132.   // //if only right sensor detects black line
  133.   // if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
  134.   // {
  135.   //   //going left with full speed
  136.   //   Left(speed);
  137.   // }
  138.  
  139.   // //if only right most sensor detects black line
  140.   // if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))
  141.   // {
  142.   //   //going left with full speed
  143.   //   Left(speed);
  144.   // }
  145.  
  146.   // //if middle and right sensor detects black line
  147.   // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1))
  148.   // {
  149.   //   //going left with full speed
  150.   //   Left(speed);
  151.   // }
  152.  
  153.   // //if middle and left sensor detects black line
  154.   // if((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  155.   // {
  156.   //   //going right with full speed
  157.   //   Right(speed);
  158.   // }
  159.  
  160.   // //if middle, left and left most sensor detects black line
  161.   // if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  162.   // {
  163.   //   //going right with full speed
  164.   //   Right(speed);
  165.   // }
  166.  
  167.   // //if middle, right and right most sensor detects black line
  168.   // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  169.   // {
  170.   //   //going left with full speed
  171.   //   Left(speed);
  172.   // }
  173.  
  174.   // //if all sensors are on a black line
  175.   // if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  176.   // {
  177.   //   //stop
  178.   //   Stop();
  179.   // }
  180.  
  181.   // delay(100);
  182. }
  183.  
  184. void Forward(int speed)
  185. {
  186.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  187.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  188.  
  189.   digitalWrite(m1, HIGH);
  190.   digitalWrite(m2, LOW);
  191.   digitalWrite(m3, HIGH);
  192.   digitalWrite(m4, LOW);
  193. }
  194.  
  195. void Backward(int speed)
  196. {
  197.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  198.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  199.  
  200.   digitalWrite(m1, LOW);
  201.   digitalWrite(m2, HIGH);
  202.   digitalWrite(m3, LOW);
  203.   digitalWrite(m4, HIGH);
  204. }
  205.  
  206. void Right(int speed)
  207. {
  208.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  209.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  210.  
  211.   digitalWrite(m1, HIGH);
  212.   digitalWrite(m2, LOW);
  213.   digitalWrite(m3, LOW);
  214.   digitalWrite(m4, LOW);
  215. }
  216.  
  217. void Left(int speed)
  218. {
  219.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  220.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  221.  
  222.   digitalWrite(m1, LOW);
  223.   digitalWrite(m2, LOW);
  224.   digitalWrite(m3, HIGH);
  225.   digitalWrite(m4, LOW);
  226. }
  227.  
  228. void Stop()
  229. {
  230.   digitalWrite(m1, LOW);
  231.   digitalWrite(m2, LOW);
  232.   digitalWrite(m3, LOW);
  233.   digitalWrite(m4, LOW);
  234. }
  235.  
  236. void CarTest()
  237. {
  238.   Forward(150);
  239.   delay(1000);
  240.  
  241.   Backward(150);
  242.   delay(1000);
  243.  
  244.   Right(150);
  245.   delay(1000);
  246.  
  247.   Left(150);
  248.   delay(1000);
  249.  
  250.   Stop();
  251.   delay(1000);
  252. }

Replies to Re: Re: Re: Line Follower rss

Title Name Language When
Re: Re: Re: Re: Line Follower Expert TTA cpp 1 Year ago.