Advertisement
safwan092

Untitled

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