Advertisement
safwan092

Untitled

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