Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import sys
  5. import time
  6. import RPi.GPIO as GPIO
  7.  
  8. GPIOTrigger = 23
  9. GPIOEcho = 24
  10.  
  11. def wait(sec):
  12. while sec > 0:
  13. sys.stdout.write(str(sec) + ' r')
  14. sec -= 1
  15. time.sleep(1)
  16.  
  17. def MeasureDistance():
  18. GPIO.output(GPIOTrigger, True)
  19. time.sleep(0.00001)
  20. GPIO.output(GPIOTrigger, False)
  21. StartTime = time.time()
  22. while GPIO.input(GPIOEcho) == 0:
  23. StartTime = time.time()
  24. while GPIO.input(GPIOEcho) == 1:
  25. StopTime = time.time()
  26. TimeElapsed = StopTime - StartTime
  27. Distance = (TimeElapsed * 34300) / 2
  28. return Distance
  29. def main():
  30. STATUS = 0
  31. try:
  32. while True:
  33. Distance = MeasureDistance()
  34. if Distance > 10.0:
  35. time.sleep(0.01)
  36. else:
  37. if STATUS != 1:
  38. print("Passing by (%.1f cm)" % Distance)
  39. import subprocess
  40. subprocess.call(["/root/rcswitch-pi/send", "10000", "1", "1"])
  41. wait(5)
  42. STATUS = 1
  43. else:
  44. subprocess.call(["/root/rcswitch-pi/send", "10000", "1", "0"])
  45. wait(5)
  46. STATUS = 0
  47.  
  48. except KeyboardInterrupt:
  49. print("Stopped by user")
  50. GPIO.cleanup()
  51. if __name__ == '__main__':
  52. # use GPIO pin numbering convention
  53. GPIO.setmode(GPIO.BCM)
  54. GPIO.setup(GPIOTrigger, GPIO.OUT)
  55. GPIO.setup(GPIOEcho, GPIO.IN)
  56. GPIO.output(GPIOTrigger, False)
  57. main()
  58.  
  59. def MeasureDistance():
  60. StartTime = time.time()
  61. GPIO.output(GPIOTrigger, True)
  62. while GPIO.input(GPIOEcho) == 0:
  63. pass
  64. # Found the raising front
  65. StopTime = time.time()
  66. GPIO.output(GPIOTrigger, False)
  67.  
  68. try:
  69. Distance = MeasureDistance()
  70. while True:
  71. while Distance > 10.0:
  72. Distance = MeasureDistance()
  73. wait(5)
  74. switch_on()
  75. while Distance < 10.0:
  76. Distance = MeasureDistance()
  77. wait(5)
  78. switch_off()
  79. except KeyboardInterrupt:
  80.  
  81. while Distance > breaking_distance - tolerance:
  82. Distance = MeasureDistance()
  83. wait(5)
  84. switch_on()
  85. while Distance < breaking_distance + tolerance:
  86. Distance = MeasureDistance()
  87. wait(5)
  88. switch_off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement