Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. /*
  2. driveTrain.c - Follows an object a certain distance away from it using 2 ultrasonic sensors
  3.  
  4. */
  5.  
  6.  
  7. //Pins used to control direction and speed of the motor. Speed pin should be a pwm pin.
  8. #define MotorDirectionLeft 8
  9. #define MotorSpeedLeft 9
  10. #define MotorDirectionRight 12
  11. #define MotorSpeedRight 11
  12.  
  13. #define leftTrigPin 4 // TODO: Double check these are correct ports // TRIG pin
  14. #define leftEchoPin 5 // ECHO pin,
  15. #define rightTrigPin 6
  16. #define rightEchoPin 7
  17.  
  18. int SpeedVal = 0;
  19.  
  20. void setup() {
  21. //Declaration for the pins used, both should be outputs.
  22. Serial.begin(9600);
  23. pinMode(MotorDirectionLeft, OUTPUT);
  24. pinMode(MotorSpeedLeft, OUTPUT);
  25. pinMode(MotorDirectionRight, OUTPUT);
  26. pinMode(MotorSpeedRight, OUTPUT);
  27. pinMode(leftTrigPin, OUTPUT);
  28. pinMode(rightTrigPin, OUTPUT);
  29. pinMode(leftEchoPin,INPUT);
  30. pinMode(rightEchoPin,INPUT);
  31. analogWrite(MotorSpeedLeft, 0);
  32. analogWrite(MotorSpeedRight,0);
  33.  
  34. }
  35.  
  36. void loop() {
  37. long duration1, inches1, cm1;
  38. long duration2, inches2, cm2;
  39. // Get the ultrasonic sensors to gather distances
  40. // Left Sensor
  41. digitalWrite(leftTrigPin, LOW);
  42. delayMicroseconds(2);
  43. digitalWrite(leftTrigPin,HIGH);
  44. delayMicroseconds(5);
  45. digitalWrite(leftTrigPin,LOW);
  46. duration1 = pulseIn(leftEchoPin,HIGH); // gets the time value for snesor 1
  47.  
  48. // convert sensor 1 time to inches/cm
  49. //inches1 = microsecondsToInches(duration1);
  50. cm1 = microsecondsToCentimeters(duration1);
  51.  
  52. // Right Sensor
  53. digitalWrite(rightTrigPin, LOW);
  54. delayMicroseconds(2);
  55. digitalWrite(rightTrigPin,HIGH);
  56. delayMicroseconds(5);
  57. digitalWrite(rightTrigPin,LOW);
  58. duration2 = pulseIn(rightEchoPin,HIGH); // gets the time value for sensor 2
  59.  
  60. // convert sensor 2 time to inches/cm
  61. //inches2 = microsecondsToInches(duration2);
  62. cm2 = microsecondsToCentimeters(duration2);
  63.  
  64. // Serial Monitor
  65.  
  66. //Serial.print(inches1);
  67. // Serial.print(" in, ");
  68. Serial.print("Left: ");
  69. Serial.print(cm1);
  70.  
  71. Serial.println();
  72.  
  73.  
  74. //Serial.print(inches2);
  75. //Serial.print("in2, ");
  76. Serial.print("Right: ");
  77. Serial.print(cm2);
  78.  
  79. Serial.println();
  80.  
  81.  
  82. delay(100);
  83.  
  84. // Drive Train
  85. // Motor Right: High means move forward
  86. // Motor Left: Low Means move forward
  87.  
  88. /*if(cm1 > 1000 || cm2 > 1000) // if further than 160 inches, brake
  89. {
  90. brake();
  91. Serial.print("Brake(misread)");
  92. Serial.println();
  93. return;
  94.  
  95. }
  96. */
  97.  
  98. if (cm1<80 && cm2 < 80){
  99. brake();
  100.  
  101. Serial.print("BreakClose");
  102. Serial.println();
  103. }
  104.  
  105. else if((cm1<175 && cm2 < 175) && (cm1 > 80 && cm2 > 80)) // if less than 68 inches away, go forward (slowly)
  106. {
  107. goForward();
  108. }
  109.  
  110. else if((cm1 > 175 or cm <20) && cm2 < 175) // if something in front of the RIGHT sensor, turn RIGHT
  111. {
  112. goRight();
  113. }
  114.  
  115. else if(cm1 < 175 && (cm2 > 175 or cm < 20)) // if something in front of the LEFT sensor, turn left
  116. {
  117. goLeft();
  118. }
  119.  
  120. else
  121. {
  122. brake();
  123. Serial.print("BrakeFar");
  124. Serial.println();
  125. }
  126. }
  127.  
  128.  
  129.  
  130. long microsecondsToInches(long microseconds)// convert ms t inches
  131. {
  132. return microseconds/74/2 ;
  133. }
  134.  
  135. long microsecondsToCentimeters(long microseconds)
  136. {
  137. return microseconds/ 29/2;
  138. }
  139.  
  140. void brake()
  141. {
  142. digitalWrite(MotorDirectionLeft, LOW);
  143. digitalWrite(MotorDirectionRight, HIGH);
  144. analogWrite(MotorSpeedLeft, 0);
  145. analogWrite(MotorSpeedRight, 0);
  146. delay(500);
  147. }
  148.  
  149. void goLeft()
  150. {
  151. digitalWrite(MotorDirectionLeft, HIGH);
  152. digitalWrite(MotorDirectionRight, HIGH);
  153.  
  154. //analogWrite(MotorSpeedLeft, 150);
  155. analogWrite(MotorSpeedLeft, 60);
  156. analogWrite(MotorSpeedRight, 150); // set right motor on to turn left
  157.  
  158. Serial.print("Go Left");
  159. Serial.println();
  160. delay(500);
  161. }
  162.  
  163. void goRight()
  164. {
  165. digitalWrite(MotorDirectionLeft, LOW);
  166. digitalWrite(MotorDirectionRight, LOW);
  167.  
  168. analogWrite(MotorSpeedLeft, 150);
  169. analogWrite(MotorSpeedRight, 60);
  170. //analogWrite(MotorSpeedRight, 150); // set left motor on to turn right
  171.  
  172. Serial.print("Go Right");
  173. Serial.println();
  174. delay(500);
  175. }
  176.  
  177. void goForward()
  178. {
  179. digitalWrite(MotorDirectionLeft, LOW);
  180. digitalWrite(MotorDirectionRight, HIGH);
  181. analogWrite(MotorSpeedLeft, 60);
  182. analogWrite(MotorSpeedRight, 60);
  183.  
  184. Serial.print("Go Forward");
  185. Serial.println();
  186. delay(500);
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement