Guest User

Untitled

a guest
May 22nd, 2018
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. /***
  2. * Description: Just for ...
  3. * Email: ntamvl@gmail.com
  4. ***/
  5.  
  6. #include <AFMotor.h>
  7. #include <Servo.h>
  8.  
  9. #define TRIG_PIN A4
  10. #define ECHO_PIN A5
  11. #define SERVO_PIN 10
  12. #define MAX_SPEED 150
  13. #define MOTORS_CALIBRATION_OFFSET 3
  14. #define MIN_DISTANCE 25
  15. #define INIT_ANGLE 87
  16.  
  17. AF_DCMotor leftMotor(4, MOTOR12_8KHZ);
  18. AF_DCMotor rightMotor(3, MOTOR12_8KHZ);
  19. Servo _servo;
  20.  
  21. String motorSet = "";
  22. int speedSet = 0;
  23.  
  24. void setup()
  25. {
  26. Serial.begin(9600);
  27. pinMode(TRIG_PIN, OUTPUT);
  28. pinMode(ECHO_PIN, INPUT);
  29. _servo.attach(SERVO_PIN);
  30. _servo.write(INIT_ANGLE);
  31. delay(2000);
  32. }
  33.  
  34. void loop()
  35. {
  36. objectDistance();
  37. Serial.println(motorSet);
  38. doAutoCar();
  39. }
  40.  
  41. void doAutoCar()
  42. {
  43. int distance = 0;
  44. int distance_90 = 0;
  45. int maxLeftDistance = -1;
  46. int maxRightDistance = -1;
  47.  
  48. moveForward();
  49.  
  50. _servo.write(INIT_ANGLE);
  51. delay(200);
  52.  
  53. distance = objectDistance();
  54. if (distance <= MIN_DISTANCE)
  55. {
  56. moveStop();
  57. delay(500);
  58.  
  59. // begin detect
  60. for (int pos = 150; pos >= 30; pos -= 10)
  61. {
  62. _servo.write(pos);
  63. delay(90);
  64. distance = objectDistance();
  65.  
  66. if (pos == INIT_ANGLE)
  67. distance_90 = distance;
  68.  
  69. if (pos > INIT_ANGLE)
  70. {
  71. if (maxLeftDistance < distance)
  72. maxLeftDistance = distance;
  73. }
  74. else
  75. {
  76. if (maxRightDistance < distance)
  77. maxRightDistance = distance;
  78. }
  79. }
  80.  
  81. for (int pos = 10; pos <= 150; pos += 10)
  82. {
  83. _servo.write(pos);
  84. delay(90);
  85. distance = objectDistance();
  86.  
  87. if (pos > INIT_ANGLE)
  88. {
  89. Serial.println("On left");
  90. if (maxLeftDistance < distance)
  91. maxLeftDistance = distance;
  92. Serial.print("maxLeftDistance: ");
  93. Serial.print(maxLeftDistance);
  94. }
  95. else
  96. {
  97. Serial.println("On right");
  98. if (maxRightDistance < distance)
  99. maxRightDistance = distance;
  100. Serial.print("maxRightDistance: ");
  101. Serial.print(maxRightDistance);
  102. }
  103. }
  104.  
  105. if (maxLeftDistance < maxRightDistance)
  106. {
  107. moveBackward();
  108. delay(500);
  109.  
  110. Serial.println("Turn right");
  111. turnRight();
  112. delay(500);
  113. moveForward();
  114. }
  115.  
  116. if (maxLeftDistance > maxRightDistance)
  117. {
  118. moveBackward();
  119. delay(500);
  120.  
  121. Serial.println("Turn left");
  122. turnLeft();
  123. delay(500);
  124. moveForward();
  125. }
  126.  
  127. if (maxLeftDistance == maxRightDistance)
  128. {
  129. Serial.println("Backward");
  130. moveBackward();
  131. delay(500);
  132. }
  133.  
  134. delay(200);
  135. distance = objectDistance();
  136. if (distance <= MIN_DISTANCE)
  137. {
  138. moveBackward();
  139. delay(500);
  140.  
  141. Serial.println("Turn right");
  142. turnRight();
  143. delay(500);
  144. }
  145. // end detect
  146. }
  147. }
  148.  
  149. void moveStop()
  150. {
  151. leftMotor.run(RELEASE);
  152. rightMotor.run(RELEASE);
  153. }
  154.  
  155. void moveForward()
  156. {
  157. motorSet = "FORWARD";
  158. leftMotor.run(FORWARD);
  159. rightMotor.run(FORWARD);
  160. leftMotor.setSpeed(MAX_SPEED);
  161. rightMotor.setSpeed(MAX_SPEED);
  162. }
  163.  
  164. void moveBackward()
  165. {
  166. motorSet = "BACKWARD";
  167. leftMotor.run(BACKWARD);
  168. rightMotor.run(BACKWARD);
  169. leftMotor.setSpeed(MAX_SPEED);
  170. rightMotor.setSpeed(MAX_SPEED);
  171. }
  172.  
  173. void turnRight()
  174. {
  175. motorSet = "RIGHT";
  176. leftMotor.run(FORWARD);
  177. rightMotor.run(RELEASE);
  178. leftMotor.setSpeed(MAX_SPEED);
  179. rightMotor.setSpeed(MAX_SPEED);
  180. }
  181.  
  182. void turnLeft()
  183. {
  184. motorSet = "LEFT";
  185. leftMotor.run(RELEASE);
  186. rightMotor.run(FORWARD);
  187. leftMotor.setSpeed(MAX_SPEED);
  188. rightMotor.setSpeed(MAX_SPEED);
  189. }
  190.  
  191. void backwardRight()
  192. {
  193. motorSet = "BACKWARD_RIGHT";
  194. leftMotor.run(BACKWARD);
  195. rightMotor.run(RELEASE);
  196. leftMotor.setSpeed(MAX_SPEED);
  197. rightMotor.setSpeed(MAX_SPEED);
  198. }
  199.  
  200. void backwardLeft()
  201. {
  202. motorSet = "BACKWARD_LEFT";
  203. leftMotor.run(RELEASE);
  204. rightMotor.run(BACKWARD);
  205. leftMotor.setSpeed(MAX_SPEED);
  206. rightMotor.setSpeed(MAX_SPEED);
  207. }
  208.  
  209. int objectDistance()
  210. {
  211. unsigned long duration;
  212. int distance;
  213.  
  214. digitalWrite(TRIG_PIN, 0);
  215. delayMicroseconds(2);
  216.  
  217. digitalWrite(TRIG_PIN, 1);
  218. delayMicroseconds(5);
  219.  
  220. digitalWrite(TRIG_PIN, 0);
  221.  
  222. duration = pulseIn(ECHO_PIN, HIGH);
  223.  
  224. distance = int(duration / 2 / 29.412);
  225.  
  226. Serial.print(distance);
  227. Serial.println(" cm");
  228. return distance;
  229. }
Add Comment
Please, Sign In to add comment