Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 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_SPEED 250 //The compiler will replace any mention of MAX_SPEED with the value 230 at compile time,it also sets speed of DC motors
  8.  
  9.  
  10.  
  11. AF_DCMotor motor1(1, MOTOR12_1KHZ); // creates motor one selects motor pin 1, sets the frequency of the motor to one kilo Hertz
  12. AF_DCMotor motor2(3, MOTOR12_1KHZ); //creates motor two and selects motor pin 3, sets the frequency of the motor to one kilo Hertz
  13.  
  14.  
  15.  
  16. 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".
  17. //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.
  18.  
  19. const byte medianFilterWindowSize = 5;// this code selects a window size for the sensor
  20. 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
  21.  
  22.  
  23. Servo myservo; // creates a servo motor object named as "myservo"
  24.  
  25. 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.
  26. int distance = 100;// give // it gives the variable "distance" a value "100".
  27. int speedSet = 0;// it gives the variable speedSet the value "0"
  28.  
  29.  
  30. void setup() { // starts void setup to be executed only once
  31.  
  32. Serial.begin(9600); // we all know what this does
  33. myservo.attach(9); // pin 9 for the servo motor
  34. myservo.write(90); // set the angle of the servo to 115 degrees
  35. delay(5000);// delay for two seconds
  36. distance = readPing();//distance is equal to the ping that was read from the sensor, evaluate whatever value or expression is on the right side of the equal sign, and store it in the variable to the left of the equal sign.
  37. delay(100);// delay by 100 milliseconds
  38. distance = readPing(); // repeat
  39. delay(100);//repeat
  40. distance = readPing();// repeat
  41. delay(100);//repeat
  42. distance = readPing();// repeat
  43. delay(100);// repeat. it is repeated in order to take a more accurate reading from the sensor
  44. Serial.print(distance); // overall serial print to see what the reading is on the serial monitor
  45. }
  46.  
  47. void loop() {// starts the loop function
  48. unsigned int distance = sensor.getDist();// "unsigned int" is nearly the same as "int" unsigned means that it can store only positive values non-negative to be more precise this is done to save memory.
  49. //it is used for things like distance that will never be negative. this code was recommended to me by a friend
  50. int distanceR = 0;// gives the variable distanceR the value 0
  51. int distanceL = 0;// gives the variable distanceL the value 0
  52. Serial.println(distance);// prints the distance onto the serial monitor for 40 milliseconds
  53. delay(40);
  54.  
  55. if(distance<=650)// if the distance is less than 500 then execute the following commands (this is where the coding will start to get very complex as i will need to add moving forward and backward here depending on the situation).
  56. {
  57. moveStop(); // moveStop is a declared function. more info towards the end
  58. delay(100);// delay for 100 milliseconds
  59. moveBackward();// Move backwards for 600 milliseconds.
  60. delay(500);
  61. moveStop();// stop the motors
  62. delay(200);// delay for 200 milliseconds.
  63. distanceR = lookRight();// make distanceR value equal to the lookRight value.
  64. delay(200);// delay by 200 milliseconds
  65. distanceL = lookLeft();// make distanceL value equal to the lookLeft value.
  66. delay(200);// delay by 200 milliseconds
  67.  
  68. if(distanceR>=distanceL)//if the distanceR is bigger than distance left execute the following.
  69. {
  70. turnRight();// turn right
  71. moveStop();// stop
  72. }else // Corresponds to the second if structure code. Meaning if it is wrong then execute the following.
  73. {
  74. turnLeft(); // Turn left.
  75. moveStop(); // stop.
  76. }
  77. }else // corresponds to the first "if" structure command. meaning if it is false then execute the following
  78. {
  79. moveForward();// move forward
  80. }
  81. distance = readPing();// makes distance equal to the readPing, evaluate whatever value or expression is on the right side of the equal sign, and store it in the variable to the left of the equal sign.
  82. }
  83.  
  84. int lookRight()//replace the variable with the number prduced after the next 7 lines of code are executed.
  85. {
  86. myservo.write(0); // set the angle/direction of the servo to 50.
  87. delay(300);//wait for half a second
  88. int distance = readPing();// make distance equal to readPing
  89. delay(400);//delay for 100 milliseconds
  90. myservo.write(90);
  91. delay(300);// set the servo angle back to its initial position 115
  92. Serial.print("right:");
  93. Serial.print(distance);
  94. delay(10);
  95. return distance;// return the number where it was mentioned
  96.  
  97. }
  98.  
  99. int lookLeft()// variable lookLeft is stored as the following
  100. {
  101. myservo.write(180); // set the servo angle to 170 degrees
  102. delay(300);// delay for 500 milliseconds
  103. int distance = readPing();// make distance equal to the readping
  104. delay(400);//delay for 100 milliseconds
  105. myservo.write(90);
  106. delay(300);// return to original position(115 degrees)
  107. Serial.print("left:");
  108. Serial.print(distance);
  109. delay(10);
  110. return distance;// return distance to where it was mentioned
  111.  
  112. }
  113.  
  114. int readPing() { // it will execute the code below if readPing is mentioned.
  115. delay(70);// delay by 70 milliseconds
  116. unsigned int kms = sensor.getDist();// set the variable on the left to the value of the variable on the right
  117. if((kms >= 0 && kms <=200) || kms>1350){
  118. kms = 250;
  119. }
  120. return kms;//Terminate the function and return a value from a function to the calling function, if desired.
  121.  
  122.  
  123. }
  124. void moveStop() {//is a function declaration. It indicates that the function is expected to return no information to the function from which it was called. so when movestop is mentioned it will only execute the following lines.
  125. motor1.run(RELEASE); //releases the motor ready to be used/stop rotation
  126. motor2.run(RELEASE);//releases the motor ready to be used/stop rotation
  127.  
  128. }
  129.  
  130. void moveForward() {// this is a function decleration
  131.  
  132. if(!goesForward)// the "!" is a boolean operator.which means logical not results in a true if the operand is false and vice versa. and in this case the operator was set to false at the begginning.
  133. {// This code simply tells us that if "goesForward" is not a "true" operand( meaning if it is false) then execute the following commands.
  134. goesForward=true; // set goesForward to true so that this code is only executed once
  135. motor1.run(FORWARD); // make the motor run forward
  136. motor2.run(FORWARD); // make motor two run forward
  137. for (speedSet = 200; speedSet < MAX_SPEED; speedSet +=2) // set the speed to 150 if the speed set is below max speed then increase the speed by two.
  138. {
  139. motor1.setSpeed(speedSet);// set the speed of the motor one to the speed set in the previous code
  140. motor2.setSpeed(speedSet);// set the speed of the motor two to speedset and add the offset speed to it in order to take sharp turns.
  141. delay(5);// repeat with a delay of 5 miliseconds
  142. }
  143. }
  144. }
  145.  
  146. void moveBackward() {// function decleration
  147. goesForward=false; // set goes forrward to false again for the motors to be able to move forward again.
  148. motor1.run(BACKWARD); // make motor 1 move backwards
  149. motor2.run(BACKWARD); // make motor two move backwards
  150. for (speedSet = 200; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up untill the desireded speed is reached the "for" structure creates a loop
  151. {
  152. motor1.setSpeed(speedSet);// set the speed of motor one to the speedset
  153. motor2.setSpeed(speedSet);// set the speed of motor two to speedset+ max speed offset
  154. delay(5);// delay by 5 milliseconds and restart the for function untill the speedset reaches the max speed or untill the distance is less than 500.
  155. }
  156.  
  157. }
  158. void turnRight() {// function decleration
  159. motor1.run(FORWARD);// run motor one forward
  160. motor2.run(BACKWARD); // run motor 2 backward
  161. delay(400);// delay by half a second. this also means execute the previous codes for half a second
  162. motor1.run(FORWARD); // run motor one forward
  163. motor2.run(FORWARD); // rotate motor 2 forward
  164. }
  165.  
  166. void turnLeft() {// function decleration
  167. motor1.run(BACKWARD);// run motor one backwards
  168. motor2.run(FORWARD); // run motor 2 forwards
  169. delay(400);// execute the previous command for 500 milliseconds
  170. motor1.run(FORWARD); // run motor one forward
  171. motor2.run(FORWARD);//run motor two forward
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement