Advertisement
Guest User

line follower 4 ir

a guest
Mar 23rd, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. /*Mert Arduino and Raspberry Pi - Line Following Robot*/
  2.  
  3. //Define Pins
  4.  
  5. int ENA = 3; //Enable Pin of the Right Motor (must be PWM)
  6. int RM1 = 1; //Control Pin
  7. int RM2 = 2;
  8.  
  9. int ENB = 6; //Enable Pin of the Left Motor (must be PWM)
  10. int LM1 = 4;
  11. int LM2 = 5;
  12.  
  13. //Speed of the Motors
  14. #define ENASpeed 150
  15. #define ENBSpeed 150
  16.  
  17. // we dont want to work with sensor 1 and 6 , it will make more complex the if-else statement
  18. int Sensor2 = 0;
  19. int Sensor3 = 0;
  20. int Sensor4 = 0;
  21. int Sensor5 = 0;
  22.  
  23. void setup() {
  24.  
  25. pinMode(ENA, OUTPUT);
  26. pinMode(RM1, OUTPUT);
  27. pinMode(RM2, OUTPUT);
  28.  
  29. pinMode(ENB, OUTPUT);
  30. pinMode(LM1, OUTPUT);
  31. pinMode(LM2, OUTPUT);
  32.  
  33. pinMode(Sensor2, INPUT);
  34. pinMode(Sensor3, INPUT);
  35. pinMode(Sensor4, INPUT);
  36. pinMode(Sensor5, INPUT);
  37.  
  38. }
  39.  
  40. void loop(){
  41.  
  42. //Use analogWrite to run motor at adjusted speed
  43. analogWrite(ENA, ENASpeed);
  44. analogWrite(ENB, ENBSpeed);
  45.  
  46. //Read the Sensor if HIGH (BLACK Line) or LOW (WHITE Line)
  47. Sensor2 = digitalRead(8);
  48. Sensor3 = digitalRead(9);
  49. Sensor4 = digitalRead(10);
  50. Sensor5 = digitalRead(11);
  51.  
  52. //Set conditions for FORWARD, LEFT and RIGHT
  53.  
  54. if(Sensor2 == HIGH && Sensor3 == HIGH && Sensor4 == LOW && Sensor5 == LOW){
  55.  
  56. //Turn LEFT
  57. //motor LM Backward
  58. digitalWrite(LM1, LOW);
  59. digitalWrite(LM2, HIGH);
  60.  
  61. //motor RM Forward
  62. digitalWrite(RM1, HIGH);
  63. digitalWrite(RM2, LOW);
  64. }
  65.  
  66. else if (Sensor2 == LOW && Sensor3 == LOW && Sensor4 == HIGH && Sensor5 == HIGH){
  67.  
  68. //Turn RIGHT
  69. //motor LM Forward
  70. digitalWrite(LM1, HIGH);
  71. digitalWrite(LM2, LOW);
  72.  
  73. //motor RM Backward
  74. digitalWrite(RM1, LOW);
  75. digitalWrite(RM2, HIGH);
  76. }
  77.  
  78. else{
  79.  
  80. //if(Sensor2 == LOW && Sensor3 == HIGH && Sensor4 == HIGH && Sensor5 == LOW
  81.  
  82. //FORWARD
  83. digitalWrite(LM1, HIGH);
  84. digitalWrite(LM2, LOW);
  85. digitalWrite(RM1, HIGH);
  86. digitalWrite(RM2, LOW);
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement