I am working on a project of my own that involves servos on a raspberry pi. I have them spinning when I execute the code but I'd prefer to have the python script kill itself after 10 seconds, rather then having to hit CTRL + C all the time. Is there a way to do this with this particular code?
我正在做一个我自己的项目,这个项目涉及到对树莓派的伺服系统。我在执行代码时让它们旋转,但我更希望python脚本在10秒后自动终止,而不是一直按CTRL + C。是否有一种方法可以使用这个特定的代码来实现这一点?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
while True:
GPIO.output(7,1)
time.sleep(0.0015)
GPIO.output(7,0)
time.sleep(0.01)
except KeyboardInterrupt:
print"Stopping Auto-Feeder"
GPIO.cleanup()
1 个解决方案
#1
3
Try something like the following:
试试以下的方法:
import RPi.GPIO as GPIO
import time
stop_time = time.time() + 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
while time.time() < stop_time:
GPIO.output(7,1)
time.sleep(0.0015)
GPIO.output(7,0)
time.sleep(0.01)
except KeyboardInterrupt:
pass
print"Stopping Auto-Feeder"
GPIO.cleanup()
#1
3
Try something like the following:
试试以下的方法:
import RPi.GPIO as GPIO
import time
stop_time = time.time() + 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
while time.time() < stop_time:
GPIO.output(7,1)
time.sleep(0.0015)
GPIO.output(7,0)
time.sleep(0.01)
except KeyboardInterrupt:
pass
print"Stopping Auto-Feeder"
GPIO.cleanup()