Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1.  
  2. #include <AFMotor.h> // includes adafruit motor shield library
  3. #include <SharpDistSensor.h> // includes sharp distance sensor library
  4. #include <Servo.h> // includes the servo motor library
  5.  
  6.  
  7. #define MAX_DISTANCE 240 //The compiler will replace any mention of MAX_DISTANCE with the value 240 at compile time.
  8. #define MAX_SPEED 230 //The compiler will replace any mention of MAX_SPEED with the value 230 at compile time,it also sets speed of DC motors
  9. #define MAX_SPEED_OFFSET 20// The compiler will replace any mention of MAXX_SPEED_OFFSET with the value 20 atcompile time.
  10.  
  11.  
  12.  
  13. AF_DCMotor motor1(1, MOTOR12_1KHZ); // creates motor one selects motor pin 1, sets the frequency of the motor to one kilo Hertz
  14. AF_DCMotor motor2(3, MOTOR12_1KHZ); //creates motor two and selects motor pin 3, sets the frequency of the motor to one kilo Hertz
  15.  
  16.  
  17.  
  18. const byte sensorPin = A0; // The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only".
  19. //This means that the variable can be used just as any other variable of its type, but its value cannot be changed. A byte stores an 8-bit unsigned number, from 0 to 255. in this case the code selects analog pin A0 for the sensor.
  20.  
  21. const byte medianFilterWindowSize = 5;// this code selects a window size for the sensor
  22. SharpDistSensor sensor(sensorPin, medianFilterWindowSize);// creates a sensor object and sets the sensorpin and the window size which were given a value in the previous lines of codes
  23.  
  24.  
  25. Servo myservo; // creates a servo motor object named as "myservo"
  26.  
  27. boolean goesForward=false;// boolean is a non-standard type alias for bool defined by Arduino. it si the same as int and it sets goesForward to fasle=0.
  28. int distance = 100;// give // it gives the variable "distance" a value "100".
  29. int speedSet = 0;// it gives the variable speedSet the value "0"
  30.  
  31.  
  32. void setup() { // starts void setup to be executed only once
  33.  
  34. Serial.begin(9600); // we all know what this does
  35. myservo.attach(9); // pin 9 for the servo motor
  36. myservo.write(115); // set the angle of the servo to 115 degrees
  37. delay(2000);// delay for two seconds
  38. distance = readPing();//distance is equal to the ping that was read from the sensor
  39. delay(100);// delay by 100 milliseconds
  40. distance = readPing(); // repeat
  41. delay(100);//repeat
  42. distance = readPing();// repeat
  43. delay(100);//repeat
  44. distance = readPing();// repeat
  45. delay(100);// repeat. it is repeated in order to take a more accurate reading from the sensor
  46. Serial.print(distance); // overall serial print to see what the reading is on the serial monitor
  47. }
  48.  
  49. void loop() {// starts the loop function
  50. unsigned int distance = sensor.getDist();
  51. int distanceR = 0;
  52. int distanceL = 0;
  53. Serial.println(distance);
  54. delay(40);
  55.  
  56. if(distance<=500)
  57. {
  58. moveStop();
  59. delay(100);
  60. moveBackward();
  61. delay(600);
  62. moveStop();
  63. delay(200);
  64. distanceR = lookRight();
  65. delay(200);
  66. distanceL = lookLeft();
  67. delay(200);
  68.  
  69. if(distanceR>=distanceL)
  70. {
  71. turnRight();
  72. moveStop();
  73. }else
  74. {
  75. turnLeft();
  76. moveStop();
  77. }
  78. }else
  79. {
  80. moveForward();
  81. }
  82. distance = readPing();
  83. }
  84.  
  85. int lookRight()
  86. {
  87. myservo.write(50);
  88. delay(500);
  89. int distance = readPing();
  90. delay(100);
  91. myservo.write(115);
  92. Serial.print(distance + "right");// serial print for right
  93. return distance;
  94.  
  95. }
  96.  
  97. int lookLeft()
  98. {
  99. myservo.write(170);
  100. delay(500);
  101. int distance = readPing();
  102. delay(100);
  103. myservo.write(115);
  104. Serial.print(distance + "left"); // serial print for left
  105. return distance;
  106. delay(100);
  107.  
  108. }
  109.  
  110. int readPing() {
  111. delay(70);
  112. unsigned int cm = sensor.getDist();
  113. if(cm==0)
  114. {
  115. cm = 250;
  116. }
  117. return cm;
  118. }
  119.  
  120. void moveStop() {
  121. motor1.run(RELEASE);
  122. motor2.run(RELEASE);
  123. }
  124.  
  125. void moveForward() {
  126.  
  127. if(!goesForward)
  128. {
  129. goesForward=true;
  130. motor1.run(FORWARD);
  131. motor2.run(FORWARD);
  132. for (speedSet = 150; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  133. {
  134. motor1.setSpeed(speedSet);
  135. motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
  136. delay(5);
  137. }
  138. }
  139. }
  140.  
  141. void moveBackward() {
  142. goesForward=false;
  143. motor1.run(BACKWARD);
  144. motor2.run(BACKWARD);
  145. for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  146. {
  147. motor1.setSpeed(speedSet);
  148. motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
  149. delay(5);
  150. }
  151. }
  152.  
  153. void turnRight() {
  154. motor1.run(FORWARD);
  155. motor2.run(BACKWARD);
  156. delay(500);
  157. motor1.run(FORWARD);
  158. motor2.run(FORWARD);
  159. }
  160.  
  161. void turnLeft() {
  162. motor1.run(BACKWARD);
  163. motor2.run(FORWARD);
  164. delay(500);
  165. motor1.run(FORWARD);
  166. motor2.run(FORWARD);
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement