Facebook
From Expert TTA, 1 Month ago, written in C++.
This paste is a reply to Re: Re: Re: Re: Line Follower from Expert TTA - view diff
Embed
Download Paste or View Raw
Hits: 218
  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 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  65.   {
  66.     Serial.println("Forward");
  67.   }
  68.   if((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 1))
  69.   {
  70.     Serial.println("Forward");
  71.   }
  72.  
  73.   else if((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  74.   {
  75.     Serial.println("Right");
  76.   }
  77.   else if((s1 == 0) && (s2 == 1 && (s3 == 1) && (s4 == 1) && (s5 == 1)))
  78.   {
  79.     Serial.println("Right");
  80.   }
  81.   else if((s1 == 0) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  82.   {
  83.     Serial.println("Right");
  84.   }
  85.   else if ((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  86.   {
  87.     Serial.println("Right");
  88.   }
  89.  
  90.   else if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
  91.   {
  92.     Serial.println("Left");
  93.   }
  94.   else if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))
  95.   {
  96.     Serial.println("Left");
  97.   }
  98.   else if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 0))
  99.   {
  100.     Serial.println("Left");
  101.   }
  102.   else if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1))
  103.   {
  104.     Serial.println("Left");
  105.   }
  106.  
  107.   else if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
  108.   {
  109.     Serial.println("On All White");
  110.   }
  111.   else
  112.   {
  113.     Serial.println("On All Black");
  114.   }
  115.  
  116.   delay(500);
  117.  
  118.   // //if only middle sensor detects black line
  119.   // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  120.   // {
  121.   //   //going forward with full speed
  122.   //   Forward(speed);
  123.   // }
  124.  
  125.   // //if only left sensor detects black line
  126.   // if((s1 == 1) && (s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  127.   // {
  128.   //   //going right with full speed
  129.   //   Right(speed);
  130.   // }
  131.  
  132.   // //if only left most sensor detects black line
  133.   // if((s1 == 0) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 1))
  134.   // {
  135.   //   //going right with full speed
  136.   //   Right(speed);
  137.   // }
  138.  
  139.   // //if only right sensor detects black line
  140.   // if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 1))
  141.   // {
  142.   //   //going left with full speed
  143.   //   Left(speed);
  144.   // }
  145.  
  146.   // //if only right most sensor detects black line
  147.   // if((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))
  148.   // {
  149.   //   //going left with full speed
  150.   //   Left(speed);
  151.   // }
  152.  
  153.   // //if middle and right sensor detects black line
  154.   // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 1))
  155.   // {
  156.   //   //going left with full speed
  157.   //   Left(speed);
  158.   // }
  159.  
  160.   // //if middle and left sensor detects black line
  161.   // if((s1 == 1) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  162.   // {
  163.   //   //going right with full speed
  164.   //   Right(speed);
  165.   // }
  166.  
  167.   // //if middle, left and left most sensor detects black line
  168.   // if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))
  169.   // {
  170.   //   //going right with full speed
  171.   //   Right(speed);
  172.   // }
  173.  
  174.   // //if middle, right and right most sensor detects black line
  175.   // if((s1 == 1) && (s2 == 1) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  176.   // {
  177.   //   //going left with full speed
  178.   //   Left(speed);
  179.   // }
  180.  
  181.   // //if all sensors are on a black line
  182.   // if((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0))
  183.   // {
  184.   //   //stop
  185.   //   Stop();
  186.   // }
  187.  
  188.   // delay(100);
  189. }
  190.  
  191. void Forward(int speed)
  192. {
  193.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  194.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  195.  
  196.   digitalWrite(m1, HIGH);
  197.   digitalWrite(m2, LOW);
  198.   digitalWrite(m3, HIGH);
  199.   digitalWrite(m4, LOW);
  200. }
  201.  
  202. void Backward(int speed)
  203. {
  204.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  205.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  206.  
  207.   digitalWrite(m1, LOW);
  208.   digitalWrite(m2, HIGH);
  209.   digitalWrite(m3, LOW);
  210.   digitalWrite(m4, HIGH);
  211. }
  212.  
  213. void Right(int speed)
  214. {
  215.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  216.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  217.  
  218.   digitalWrite(m1, HIGH);
  219.   digitalWrite(m2, LOW);
  220.   digitalWrite(m3, LOW);
  221.   digitalWrite(m4, LOW);
  222. }
  223.  
  224. void Left(int speed)
  225. {
  226.   analogWrite(e1, speed); // you can adjust the speed of the motors from 0-255
  227.   analogWrite(e2, speed); // you can adjust the speed of the motors from 0-255
  228.  
  229.   digitalWrite(m1, LOW);
  230.   digitalWrite(m2, LOW);
  231.   digitalWrite(m3, HIGH);
  232.   digitalWrite(m4, LOW);
  233. }
  234.  
  235. void Stop()
  236. {
  237.   digitalWrite(m1, LOW);
  238.   digitalWrite(m2, LOW);
  239.   digitalWrite(m3, LOW);
  240.   digitalWrite(m4, LOW);
  241. }
  242.  
  243. void CarTest()
  244. {
  245.   Forward(150);
  246.   delay(1000);
  247.  
  248.   Backward(150);
  249.   delay(1000);
  250.  
  251.   Right(150);
  252.   delay(1000);
  253.  
  254.   Left(150);
  255.   delay(1000);
  256.  
  257.   Stop();
  258.   delay(1000);
  259. }