Advertisement
safwan092

Untitled

Nov 7th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 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. //moveBackward();
  45. //turnLeft();
  46. //turnRight();
  47. //moveForward();
  48. //}
  49.  
  50. int distanceRight = 0;
  51. int distanceLeft = 0;
  52. delay(50);
  53.  
  54. if (distance <= 20){
  55. moveStop();
  56. delay(300);
  57. moveBackward();
  58. delay(400);
  59. moveStop();
  60. delay(300);
  61. distanceRight = lookRight();
  62. delay(300);
  63. distanceLeft = lookLeft();
  64. delay(300);
  65.  
  66. if (distance >= distanceLeft){
  67. turnRight();
  68. moveStop();
  69. }
  70. else{
  71. turnLeft();
  72. moveStop();
  73. }
  74. }
  75. else{
  76. moveForward();
  77. }
  78. distance = readPing();
  79. }//end of LOOP
  80.  
  81. int lookRight(){
  82. servo_motor.write(10);
  83. delay(500);
  84. int distance = readPing();
  85. delay(100);
  86. servo_motor.write(90);
  87. return distance;
  88. }
  89.  
  90. int lookLeft(){
  91. servo_motor.write(170);
  92. delay(500);
  93. int distance = readPing();
  94. delay(100);
  95. servo_motor.write(90);
  96. return distance;
  97. delay(100);
  98. }
  99.  
  100. int readPing(){
  101. delay(70);
  102. int cm = sonar.ping_cm();
  103. if (cm==0){
  104. cm=250;
  105. }
  106. return cm;
  107. }
  108.  
  109. void moveStop(){
  110.  
  111. digitalWrite(RightMotorForward, LOW);
  112. digitalWrite(LeftMotorForward, LOW);
  113. digitalWrite(RightMotorBackward, LOW);
  114. digitalWrite(LeftMotorBackward, LOW);
  115. }
  116.  
  117. void moveForward(){
  118.  
  119. if(!goesForward){
  120.  
  121. goesForward=true;
  122.  
  123. digitalWrite(LeftMotorForward, HIGH);
  124. digitalWrite(RightMotorForward, HIGH);
  125.  
  126. digitalWrite(LeftMotorBackward, LOW);
  127. digitalWrite(RightMotorBackward, LOW);
  128. }
  129. }
  130.  
  131. void moveBackward(){
  132.  
  133. goesForward=false;
  134.  
  135. digitalWrite(LeftMotorBackward, HIGH);
  136. digitalWrite(RightMotorBackward, HIGH);
  137.  
  138. digitalWrite(LeftMotorForward, LOW);
  139. digitalWrite(RightMotorForward, LOW);
  140.  
  141. }
  142.  
  143. void turnRight(){
  144.  
  145. digitalWrite(LeftMotorForward, HIGH);
  146. digitalWrite(RightMotorBackward, HIGH);
  147.  
  148. digitalWrite(LeftMotorBackward, LOW);
  149. digitalWrite(RightMotorForward, LOW);
  150.  
  151. delay(500);
  152. /*
  153. digitalWrite(LeftMotorForward, HIGH);
  154. digitalWrite(RightMotorForward, HIGH);
  155.  
  156. digitalWrite(LeftMotorBackward, LOW);
  157. digitalWrite(RightMotorBackward, LOW);
  158. */
  159.  
  160.  
  161. }
  162.  
  163. void turnLeft(){
  164.  
  165. digitalWrite(LeftMotorBackward, HIGH);
  166. digitalWrite(RightMotorForward, HIGH);
  167.  
  168. digitalWrite(LeftMotorForward, LOW);
  169. digitalWrite(RightMotorBackward, LOW);
  170.  
  171. delay(500);
  172. /*
  173. digitalWrite(LeftMotorForward, HIGH);
  174. digitalWrite(RightMotorForward, HIGH);
  175.  
  176. digitalWrite(LeftMotorBackward, LOW);
  177. digitalWrite(RightMotorBackward, LOW);
  178. */
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement