Advertisement
Guest User

Untitled

a guest
May 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 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 = 7;
  6. const int LeftMotorBackward = 6;
  7. const int RightMotorForward = 4;
  8. const int RightMotorBackward = 5;
  9.  
  10. //sensor pins
  11. #define trig_pin A1 //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. boolean fire = false;
  22. //connect PUMP to Digital Pin 8
  23. #define pump 8
  24. //Connect IR sensore TO Digital Pin 9
  25. #define f_sensore 9
  26.  
  27. void setup(){
  28.  
  29. pinMode(RightMotorForward, OUTPUT);
  30. pinMode(LeftMotorForward, OUTPUT);
  31. pinMode(LeftMotorBackward, OUTPUT);
  32. pinMode(RightMotorBackward, OUTPUT);
  33. pinMode(pump , OUTPUT);
  34. pinMode(f_sensore,INPUT);
  35.  
  36. servo_motor.attach(10); //our servo pin
  37.  
  38. servo_motor.write(115);
  39. delay(2000);
  40. distance = readPing();
  41. delay(100);
  42. distance = readPing();
  43. delay(100);
  44. distance = readPing();
  45. delay(100);
  46. distance = readPing();
  47. delay(100);
  48. }
  49.  
  50. void loop(){
  51. int fire = digitalRead(f_sensore);
  52. if(fire==HIGH)
  53. {
  54. digitalWrite(pump,HIGH);
  55. fire = true;
  56. moveStop();
  57. }
  58. else{
  59. digitalWrite(pump,LOW);
  60. fire = false;
  61. }
  62. {
  63. int distanceRight = 0;
  64. int distanceLeft = 0;
  65. delay(50);
  66.  
  67.  
  68.  
  69. if (distance <= 20){
  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 (distanceRight >= distanceLeft){
  82. turnRight();
  83. moveStop();
  84. }
  85. else{
  86. turnLeft();
  87. moveStop();
  88. }
  89. }
  90. else{
  91. moveForward();
  92. }
  93. }
  94. distance = readPing();
  95. }
  96.  
  97. int lookRight(){
  98. servo_motor.write(50);
  99. delay(500);
  100. int distance = readPing();
  101. delay(100);
  102. servo_motor.write(115);
  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(115);
  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 turnRight(){
  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 turnLeft(){
  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