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

Replies to Re: Re: Line Follower rss

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