Advertisement
Koelion

Untitled

May 6th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3.  
  4. const int trigPin = A4;
  5. const int echoPin = A5;
  6.  
  7. unsigned long previousMillis;
  8. unsigned long interval = 500;
  9.  
  10. int MoveForward = 0;
  11. int MoveBackward = 1;
  12. int MoveLeft = 3;
  13. int MoveRight = 4;
  14. int distanceTraveled = 0;
  15. bool lastState;
  16. Servo myservo;
  17.  
  18. void setup() {
  19. Serial.begin(9600);
  20. pinMode(A3, INPUT);
  21. pinMode(A4, INPUT);
  22. pinMode(4, INPUT);
  23. pinMode(5, INPUT);
  24. pinMode(7, OUTPUT);
  25. pinMode(8, OUTPUT);
  26. pinMode(12, OUTPUT);
  27. pinMode(13, OUTPUT);
  28. pinMode(10, OUTPUT);
  29. pinMode(11, OUTPUT);
  30. previousMillis = millis();
  31. myservo.attach(A1);
  32. pinMode(A0, INPUT);
  33. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  34. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  35. }
  36.  
  37. bool targetReatched = false;
  38. void loop() {
  39. String str = Serial.readString();
  40. ParseInput(str);
  41. }
  42.  
  43. float EchoDistance()
  44. {
  45. digitalWrite(trigPin, LOW);
  46. delayMicroseconds(2);
  47. // Sets the trigPin on HIGH state for 10 micro seconds
  48. digitalWrite(trigPin, HIGH);
  49. delayMicroseconds(10);
  50. digitalWrite(trigPin, LOW);
  51. // Reads the echoPin, returns the sound wave travel time in microseconds
  52. float duration = pulseIn(echoPin, HIGH);
  53. // Calculating the distance
  54. float distance = duration*0.034 / 2;
  55. return distance;
  56. }
  57.  
  58. void ParseInput(String str)
  59. {
  60. if (str.length() <= 0)
  61. return;
  62.  
  63. char func = str[0];
  64. int arg = str.substring(2).toInt();
  65.  
  66. if (str[0] == '~')
  67. {
  68. Stop();
  69. }
  70. else if (str[0] == 'm')
  71. {
  72. GoStraightDistance(arg);
  73. }
  74. else if (str[0] == 'r')
  75. {
  76. TurnDistance(arg);
  77. }
  78. else if (str[0] == 'b')
  79. {
  80. Serial.println(EchoDistance());
  81. }
  82. else if (str[0] == 'p')
  83. {
  84. RotateServo(arg);
  85. }
  86. else if (str[0] == 'c')
  87. {
  88. Serial.println(ReadDistanceFromInfraRed());
  89. }
  90. else if (str[0] == 'w')
  91. {
  92. RotateServo(arg);
  93. Serial.println(ReadDistanceFromInfraRed());
  94. }
  95. else if (str[0] == 's')
  96. {
  97. LoopRotateAndCheckDistance();
  98. }
  99. }
  100.  
  101. float ReadDistanceFromInfraRed()
  102. {
  103. float v = analogRead(A0);
  104. float u = (v / 1023.0f) * 3.30f;
  105. if (u < 0.50f)
  106. {
  107. u = 0.50f;
  108. }
  109.  
  110. return (70.0f / u) - 6.50f;
  111. }
  112.  
  113. int currentRot = 0;
  114. void LoopRotateAndCheckDistance()
  115. {
  116. if(currentRot <= 90)
  117. {
  118. for (currentRot = 0; currentRot <= 180; currentRot += 1) {
  119. myservo.write(currentRot);
  120. PrintRotDistance(currentRot, ReadDistanceFromInfraRed());
  121. delay(15);
  122. }
  123. }
  124. else
  125. {
  126. for (currentRot = 180; currentRot >= 0; currentRot -= 1) {
  127. myservo.write(currentRot);
  128. PrintRotDistance(currentRot, ReadDistanceFromInfraRed());
  129. delay(15);
  130. }
  131. }
  132.  
  133. currentRot = 90;
  134. myservo.write(currentRot);
  135. }
  136.  
  137. void PrintRotDistance(int rot, float distance)
  138. {
  139. Serial.print("rot: ");
  140. Serial.print(rot);
  141. Serial.print(", dist:");
  142. Serial.println(distance);
  143. }
  144.  
  145.  
  146. void RotateServo(int angle)
  147. {
  148. currentRot = angle;
  149. myservo.write(currentRot);
  150. }
  151.  
  152. void GoStraightDistance(int distanceToTravel)
  153. {
  154. distanceTraveled = 0;
  155. int dir = MoveForward;
  156. if (distanceToTravel < 0)
  157. {
  158. dir = MoveBackward;
  159. distanceToTravel = distanceToTravel * -1;
  160. Serial.print(distanceToTravel);
  161. }
  162.  
  163. while (distanceTraveled < distanceToTravel)
  164. {
  165. if (dir == MoveForward)
  166. {
  167. Front();
  168. }
  169. else if (dir == MoveBackward)
  170. {
  171. Back();
  172. }
  173. CalcDistanceTraveled();
  174. }
  175. Stop();
  176. }
  177.  
  178. int AngleToTics(int angle){
  179. return roundf(angle/6);
  180. }
  181.  
  182. void TurnDistance(int angle)
  183. {
  184. distanceTraveled = 0;
  185. int distanceToTravel = AngleToTics(angle);
  186. int dir = MoveLeft;
  187. if (distanceToTravel < 0)
  188. {
  189. dir = MoveRight;
  190. distanceToTravel = distanceToTravel * -1;
  191. }
  192. while (distanceTraveled < distanceToTravel)
  193. {
  194. if (dir == MoveLeft)
  195. {
  196. LeftFront();
  197. }
  198. else if (dir == MoveRight)
  199. {
  200. RightFront();
  201. }
  202. CalcDistanceTraveled();
  203. }
  204. Stop();
  205. }
  206.  
  207.  
  208. void CalcDistanceTraveled() {
  209. int state = digitalRead(A3);
  210. if (state != lastState)
  211. {
  212. distanceTraveled++;
  213. lastState = state;
  214. }
  215. }
  216.  
  217.  
  218. void RightFront()
  219. {
  220. digitalWrite(7, HIGH);
  221. digitalWrite(8, LOW);
  222. digitalWrite(10, HIGH);
  223.  
  224. digitalWrite(12, LOW);
  225. digitalWrite(13, HIGH);
  226. digitalWrite(11, HIGH);
  227. }
  228.  
  229. void LeftFront()
  230. {
  231. digitalWrite(7, LOW);
  232. digitalWrite(8, HIGH);
  233. digitalWrite(10, HIGH);
  234.  
  235. digitalWrite(12, HIGH);
  236. digitalWrite(13, LOW);
  237. digitalWrite(11, HIGH);
  238. }
  239.  
  240.  
  241. void Front()
  242. {
  243. digitalWrite(7, HIGH);
  244. digitalWrite(8, LOW);
  245. digitalWrite(10, HIGH);
  246.  
  247. digitalWrite(12, HIGH);
  248. digitalWrite(13, LOW);
  249. digitalWrite(11, HIGH);
  250. }
  251.  
  252. void Back()
  253. {
  254. digitalWrite(7, LOW);
  255. digitalWrite(8, HIGH);
  256. digitalWrite(10, HIGH);
  257.  
  258. digitalWrite(12, LOW);
  259. digitalWrite(13, HIGH);
  260. digitalWrite(11, HIGH);
  261. }
  262.  
  263.  
  264. void Stop()
  265. {
  266. digitalWrite(10, 0);
  267. digitalWrite(11, 0);
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement