我的程序是为一个raspberry pi项目编写的,在这个项目中,我为用户提供了当他们按下按钮时录制消息的选项。我目前的目标是让一个while循环通过按下按钮然后退出循环来提供录制的选项,如果没有按下它5秒,它就会退出循环。
下面是我的代码:
w = True
while w:
# if the button on the pi is pressed once this while loop begins,
if GPIO.input(17) == 0:
print ("Button Pressed")
sleep(2)
print ("Explaining recording instructions")
pygame.mixer.Sound.play(record_s)
sleep(8)
print ("Recording")
record_audio()
# Exit the loop once message is recorded by pressing the button once more
if GPIO.input(17) == 0:
w = False
# If the button is not pressed for 5 seconds,
elif sleep(5):
print ("No input")
# Exit the loop
w= False我尝试了几种不同的方法,做了大量的谷歌搜索,并查看了类似的问题,但都没有奏效。
发布于 2019-06-18 05:48:09
def run():
while True:
# if the button on the pi is pressed once this while loop begins,
if GPIO.input(17) == 0:
print ("Button Pressed")
print ("Explaining recording instructions")
pygame.mixer.Sound.play(record_s)
sleep(8)
print ("Recording")
record_audio()
# Exit the loop once message is recorded by pressing the button once more
if GPIO.input(17) == 0:
break
def main():
t = threading.Thread(target=run)
t.daemon = True
t.start()
time.sleep(5)https://stackoverflow.com/questions/56639171
复制相似问题