Advertisement
safwan092

Untitled

Mar 29th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <Servo.h> //Servo motor library. This is standard library
  2. #include <NewPing.h> //Ultrasonic sensor function library. You must install this library
  3.  
  4. //our L298N control pins
  5. const int LeftMotorForward = 5;
  6. const int LeftMotorBackward = 4;
  7. const int RightMotorForward = 3;
  8. const int RightMotorBackward = 2;
  9.  
  10. //sensor pins
  11. #define trig_pin A3 //analog input 1
  12. #define echo_pin A2 //analog input 2
  13.  
  14. #define maximum_distance 200
  15. boolean goesForward = false;
  16. int distance = 100;
  17.  
  18. NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
  19. Servo servo_motor; //our servo name
  20.  
  21.  
  22. void setup(){
  23.  
  24. pinMode(RightMotorForward, OUTPUT);
  25. pinMode(LeftMotorForward, OUTPUT);
  26. pinMode(LeftMotorBackward, OUTPUT);
  27. pinMode(RightMotorBackward, OUTPUT);
  28.  
  29. servo_motor.attach(11); //our servo pin
  30.  
  31. servo_motor.write(90);
  32. delay(2000);
  33. distance = readPing();
  34. delay(100);
  35. distance = readPing();
  36. delay(100);
  37. distance = readPing();
  38. delay(100);
  39. distance = readPing();
  40. delay(100);
  41. }
  42.  
  43. void loop(){
  44.  
  45. int distanceRight = 0;
  46. int distanceLeft = 0;
  47. delay(50);
  48.  
  49. if (distance <= 20){
  50. moveStop();
  51. delay(300);
  52. moveBackward();
  53. delay(400);
  54. moveStop();
  55. delay(300);
  56. distanceRight = lookRight();
  57. delay(300);
  58. distanceLeft = lookLeft();
  59. delay(300);
  60.  
  61. if (distance >= distanceLeft){
  62. turnRight();
  63. moveStop();
  64. }
  65. else{
  66. turnLeft();
  67. moveStop();
  68. }
  69. }
  70. else{
  71. moveForward();
  72. }
  73. distance = readPing();
  74. }
  75.  
  76. int lookRight(){
  77. servo_motor.write(10);
  78. delay(500);
  79. int distance = readPing();
  80. delay(100);
  81. servo_motor.write(90);
  82. return distance;
  83. }
  84.  
  85. int lookLeft(){
  86. servo_motor.write(170);
  87. delay(500);
  88. int distance = readPing();
  89. delay(100);
  90. servo_motor.write(90);
  91. return distance;
  92. delay(100);
  93. }
  94.  
  95. int readPing(){
  96. delay(70);
  97. int cm = sonar.ping_cm();
  98. if (cm==0){
  99. cm=250;
  100. }
  101. return cm;
  102. }
  103.  
  104. void moveStop(){
  105.  
  106. digitalWrite(RightMotorForward, LOW);
  107. digitalWrite(LeftMotorForward, LOW);
  108. digitalWrite(RightMotorBackward, LOW);
  109. digitalWrite(LeftMotorBackward, LOW);
  110. }
  111.  
  112. void moveForward(){
  113.  
  114. if(!goesForward){
  115.  
  116. goesForward=true;
  117.  
  118. digitalWrite(LeftMotorForward, HIGH);
  119. digitalWrite(RightMotorForward, HIGH);
  120.  
  121. digitalWrite(LeftMotorBackward, LOW);
  122. digitalWrite(RightMotorBackward, LOW);
  123. }
  124. }
  125.  
  126. void moveBackward(){
  127.  
  128. goesForward=false;
  129.  
  130. digitalWrite(LeftMotorBackward, HIGH);
  131. digitalWrite(RightMotorBackward, HIGH);
  132.  
  133. digitalWrite(LeftMotorForward, LOW);
  134. digitalWrite(RightMotorForward, LOW);
  135.  
  136. }
  137.  
  138. void turnRight(){
  139.  
  140. digitalWrite(LeftMotorForward, HIGH);
  141. digitalWrite(RightMotorBackward, HIGH);
  142.  
  143. digitalWrite(LeftMotorBackward, LOW);
  144. digitalWrite(RightMotorForward, LOW);
  145.  
  146. delay(500);
  147.  
  148. digitalWrite(LeftMotorForward, HIGH);
  149. digitalWrite(RightMotorForward, HIGH);
  150.  
  151. digitalWrite(LeftMotorBackward, LOW);
  152. digitalWrite(RightMotorBackward, LOW);
  153.  
  154.  
  155.  
  156. }
  157.  
  158. void turnLeft(){
  159.  
  160. digitalWrite(LeftMotorBackward, HIGH);
  161. digitalWrite(RightMotorForward, HIGH);
  162.  
  163. digitalWrite(LeftMotorForward, LOW);
  164. digitalWrite(RightMotorBackward, LOW);
  165.  
  166. delay(500);
  167.  
  168. digitalWrite(LeftMotorForward, HIGH);
  169. digitalWrite(RightMotorForward, HIGH);
  170.  
  171. digitalWrite(LeftMotorBackward, LOW);
  172. digitalWrite(RightMotorBackward, LOW);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement