Advertisement
ossipee

miniskybot

Aug 22nd, 2015
1,418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Mini-Skybot
  2.  
  3. 'ossipee
  4.  
  5.  
  6.  
  7. symbol motorR = 1 ; call pin for right motor
  8.  
  9. symbol motorL = 2 ; call pin for left motor
  10.  
  11. symbol motorRC = 145 ; stop motor
  12.  
  13. symbol motorLC = 147 ; stop motor
  14.  
  15. symbol trigger = 0 ; call pin that send pulse
  16.  
  17. symbol echo = 4 ; call pin that gets pulse back
  18.  
  19. symbol tm = w1 ;variable for microseconds read off of HCSR04
  20.  
  21. symbol inches = w2; variable for converting microseconds to distance, about 15 us/in
  22.  
  23. symbol dangerL = 7 ;arbitrary safe distance to obstacles-make it lower than you'd think
  24.  
  25. symbol leftObstacle = w3; variable to store distance  read when looking left
  26.  
  27. symbol rightObstacle = w4; variable to store distance  read when looking right
  28.  
  29.  
  30.  
  31. servo motorR,motorRC ; set pins as servo outputs
  32.  
  33. servo motorL,motorLC
  34.  
  35.  
  36.  
  37. pause 3000 ; wait 3 seconds to start
  38.  
  39.  
  40.  
  41. main:
  42.  
  43. gosub myHCSR04
  44.  
  45. if inches > dangerL then ; safe go forward
  46.  
  47. gosub nodanger ; go forward
  48.  
  49. else
  50.  
  51. gosub whichway ; look how to go
  52.  
  53. endif
  54.  
  55. goto main
  56.  
  57.  
  58.  
  59. myHCSR04:
  60.  
  61. pulsout C.0,1; sends 20us pulse out to ping
  62.  
  63. pulsin C.4,1,tm ; measures range in 10us steps
  64.  
  65. inches = tm/15
  66.  
  67. pause 100
  68.  
  69. return
  70.  
  71.  
  72.  
  73. nodanger:
  74.  
  75. servopos motorR,225
  76.  
  77. servopos motorL,75
  78.  
  79. return
  80.  
  81.  
  82.  
  83.  
  84.  
  85. whichway:
  86.  
  87. gosub totalhalt ; stop everything for crash
  88.  
  89. gosub turnleft ; turn left
  90.  
  91. pause 780; for 3/4 second or 90 degrees left depending on your servos
  92.  
  93. gosub totalhalt; stop
  94.  
  95. gosub myHCSR04;  to take reading
  96.  
  97. leftObstacle = inches;
  98.  
  99. gosub turnright ; turn right
  100.  
  101. pause 1500 ; for 1.5 seconds or 180 degrees right depending on your servos
  102.  
  103. gosub totalhalt; stop
  104.  
  105. gosub myHCSR04; to take reading
  106.  
  107. rightObstacle = inches
  108.  
  109. if leftObstacle > rightObstacle then; if the left obstacle is more distant than the right
  110.  
  111. gosub turnleft; go back around to the left
  112.  
  113. pause 1560; for 1.5 seconds or 180 degrees
  114.  
  115. else
  116.  
  117. return; no need to turn if the right obstacle is more distant
  118.  
  119. endif
  120.  
  121. return
  122.  
  123.  
  124.  
  125. turnleft:
  126.  
  127. servopos motorR, 75
  128.  
  129. servopos motorL, 75
  130.  
  131. return
  132.  
  133.  
  134.  
  135. turnright:
  136.  
  137. servopos motorR, 225
  138.  
  139. servopos motorL, 225
  140.  
  141. return
  142.  
  143.  
  144.  
  145. totalhalt:
  146.  
  147. servopos motorR, 145
  148.  
  149. servopos motorL, 147
  150.  
  151. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement