I use Raspberry 3 PI b+ model which is installed with Raspbian OS.
and that is my code
Code: Select all
import RPi.GPIO as GPIO import time, sys, os from subprocess import Popen import subprocess as sp distance = 400 zone = 1 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO_TRIGGER = 18 GPIO_ECHO = 24 GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN) movie2 = ('/home/pi/Videos/sample1.mp4') movie1 = ('/home/pi/Videos/the tide2.mp4') def distance(): GPIO.output(GPIO_TRIGGER, True) time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) StartTime = time.time() StopTime = time.time() while GPIO.input(GPIO_ECHO) == 0: StartTime = time.time() while GPIO.input(GPIO_ECHO) == 1: StopTime = time.time() TimeElapsed = StopTime - StartTime distance = (TimeElapsed * 34300) / 2 return distance if __name__ == '__main__': try: while True: dist = distance() time.sleep(1) if (distance() <= 100): zone = 1 if (distance() >= 2000): zone = 1 elif (100 < distance() < 2000): zone = 2 if (zone == 1): Popen(['/usr/bin/omxplayer', '--display','0', movie1]) time.sleep(6) if (zone == 2): Popen(['/usr/bin/omxplayer', '--display','0', movie2]) time.sleep(6) except KeyboardInterrupt: GPIO.cleanup()
When the distance is changed, the video is not changed instantly and after 6 sec, the video is played because of time.sleep function.
But if I don't use time.sleep function in this code, this code doesn't work and even my PI is shutdown.
How can I fix this problem? If you have any suggestions, I would greatly appreciate it! Thanks!