Advertisement
gihanchanaka

Untitled

Aug 8th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. /*ALAbot
  2.  
  3. Edit history
  4. 2016-08-06-11:26 gihanchanaka :
  5. Added sonar return time methods
  6. Added sonar micro second to centemeter conversion method
  7. @Dushan, complete the above funtions
  8.  
  9. 2016-08-06-12-20 dushan:
  10. completed sonar distance
  11.  
  12. 2016-08-06-12:25 gihanchanaka:
  13. Added whichSideShouldTheRobotGo()
  14.  
  15. 2016-08-06-15:04 gihanchanaka:
  16. completed robotTurnRight(),robotTurnLeft().robotGoForward() functions
  17.  
  18. 2016-08-06-15:09 gihanchanaka:
  19. debugging
  20.  
  21. 2016-08-08-11:56 gihanchanaka:
  22. removed the turnRight,turnLeft,goForward functions
  23. added LED direction indication functions and defined pin numbers
  24. changes the whichDirectionToGo untion to indicate using LEDs instead of the mechanical movement
  25. */
  26.  
  27. //The following constants are used to define the pins used for sonar
  28. const int sonarRightPingPin=7;
  29. const int sonarForwardPingPin=5;
  30. const int sonarForwardTriggerPin=6;
  31. const int sonarLeftTriggerPin=-1;
  32. const int sonarRightTriggerPin=4;
  33.  
  34.  
  35. //Direction indication pin numbers
  36. const int groundForDirectionLEDs=12;
  37. const int rightDirectionLED=8;
  38. const int forwardDirectionLED=-1;
  39. const int leftDirectionLED=-1;
  40.  
  41.  
  42. //This is used to maintain the gap and also decide when to turn
  43. const int gapBetweenRobotAndWall=5;
  44.  
  45. //This data stores the time needed for the motors to turn the robot by 90 degrees
  46. const int microSecondsForATurn=0;
  47. const int percentageSpeedInTurning=0;
  48.  
  49. int sonarReturnTimeMicroSecondsForward(){
  50.  
  51. // establish variables for duration of the ping,
  52. // and the distance result in inches and centimeters:
  53. long duration, forwardcm;
  54. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  55. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  56. pinMode(sonarForwardTriggerPin, OUTPUT);
  57. digitalWrite(sonarForwardTriggerPin, LOW);
  58. delayMicroseconds(2);
  59. digitalWrite(sonarForwardTriggerPin, HIGH);
  60. delayMicroseconds(5);
  61. digitalWrite(sonarForwardTriggerPin, LOW);
  62.  
  63. pinMode(sonarCommonPingPin, INPUT);
  64. duration = pulseIn(sonarCommonPingPin, HIGH);
  65. //Serial.println(duration);
  66. // convert the time into a distance
  67. forwardcm = sonarMilliSecondsToCentiMeters(duration);
  68. return forwardcm ;
  69.  
  70. }
  71.  
  72. int sonarReturnTimeMicroSecondsLeft(){
  73.  
  74. // establish variables for duration of the ping,
  75. // and the distance result in inches and centimeters:
  76. long duration, leftcm;
  77. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  78. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  79. pinMode(sonarLeftTriggerPin, OUTPUT);
  80. digitalWrite(sonarLeftTriggerPin, LOW);
  81. delayMicroseconds(2);
  82. digitalWrite(sonarLeftTriggerPin, HIGH);
  83. delayMicroseconds(5);
  84. digitalWrite(sonarLeftTriggerPin, LOW);
  85.  
  86. pinMode(sonarCommonPingPin, INPUT);
  87. duration = pulseIn(sonarCommonPingPin, HIGH);
  88. //Serial.println(duration);
  89. // convert the time into a distance
  90. leftcm = sonarMilliSecondsToCentiMeters(duration);
  91.  
  92. return leftcm ;
  93.  
  94. }
  95.  
  96. int sonarReturnTimeMicroSecondsRight(){
  97.  
  98. // establish variables for duration of the ping,
  99. // and the distance result in inches and centimeters:
  100. long duration, rightcm;
  101. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  102. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  103. pinMode(sonarRightTriggerPin, OUTPUT);
  104. digitalWrite(sonarRightTriggerPin, LOW);
  105. delayMicroseconds(2);
  106. digitalWrite(sonarRightTriggerPin, HIGH);
  107. delayMicroseconds(5);
  108. digitalWrite(sonarRightTriggerPin, LOW);
  109.  
  110. pinMode(sonarCommonPingPin, INPUT);
  111. duration = pulseIn(sonarCommonPingPin, HIGH);
  112. //Serial.println(duration);
  113. // convert the time into a distance
  114. rightcm = sonarMilliSecondsToCentiMeters(duration);
  115. return rightcm ;
  116.  
  117.  
  118.  
  119.  
  120. }
  121.  
  122. int sonarMilliSecondsToCentiMeters(int microSeconds){
  123.  
  124. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  125. // The ping travels out and back, so to find the distance of the
  126. // object we take half of the distance travelled.
  127. return microSeconds / 29 / 2;
  128.  
  129. }
  130.  
  131.  
  132. int whichSideShouldTheRobotGo(){
  133. int distanceRight=sonarReturnTimeMicroSecondsRight();
  134. int distanceForward=sonarReturnTimeMicroSecondsForward();
  135. int distanceLeft=sonarReturnTimeMicroSecondsLeft();
  136. Serial.println(distanceRight);
  137. Serial.println(distanceForward);
  138. Serial.println(distanceLeft);
  139.  
  140. if(distanceRight>gapBetweenRobotAndWall){
  141. turnRightLED();
  142. }
  143. else{
  144. if(distanceForward>gapBetweenRobotAndWall){
  145. goForwardLED();
  146. }
  147. else{
  148. turnLeftLED();
  149. }
  150. }
  151.  
  152. }
  153.  
  154.  
  155.  
  156. //function to set the pins for direction LED indicators
  157. void setDirectionIndicationLED(){
  158. pinMode(groundForDirectionLEDs,OUTPUT);
  159. pinMode(rightDirectionLED,OUTPUT);
  160. pinMode(forwardDirectionLED,OUTPUT);
  161. pinMode(leftDirectionLED,OUTPUT);
  162. allDirectionLEDPinsToLow();
  163. }
  164. void allDirectionLEDPinsToLow(){
  165. //digitalWrite(groundForDirectionLED,LOW);
  166. digitalWrite(rightDirectionLED,LOW);
  167. digitalWrite(forwardDirectionLED,LOW);
  168. digitalWrite(leftDirectionLED,LOW);
  169. }
  170. //functions to switch o LEDs to indicate directions
  171. void turnRightLED(){
  172. allDirectionLEDPinsToLow();
  173. digitalWrite(rightDirectionLED,HIGH);
  174. }
  175. void goForwardLED(){
  176. allDirectionLEDPinsToLow();
  177. digitalWrite(forwardDirectionLED,HIGH);
  178. }
  179. void turnLeftLED(){
  180. allDirectionLEDPinsToLow();
  181. digitalWrite(leftDirectionLED,HIGH);
  182. }
  183.  
  184.  
  185. void loop(){
  186. whichSideShouldTheRobotGo();
  187. delay(1);
  188. }
  189.  
  190. void setup(){
  191. Serial.begin(9600);
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement