我非常陌生,目前正在编写Photobooth。 我想停用Buttonpress直到投掷硬币并希望在照片保存时停用它。 我只是不知道如何编码ButtonPress的动作,直到发生了什么。
我知道我的代码非常混乱,但整体效果很好。 我在Raspberry pi上编程并在GPIO 18上安装了Coin Acceptor。 我得到10个脉冲,因为它是一个硬币接收器,只能使用1欧元硬币。
这是我正在使用的代码。
如果有人可以照亮我的黑暗,那真是太棒了! 提前谢谢!答案 0 :(得分:1)
如果已插入硬币,请尝试存储变量:
#Default is not inserted (start of script)
coin_inserted = 0
然后当插入一枚硬币并获得十个脉冲时:
#Change the var to 1
coin_inserted = 1
在您检测按钮按下的代码位上:
def button_press_func(coin_inserted):
if coin_inserted = 1:
#Take Picture
take_picture_function(coin_inserted)
else:
#Error
print("You have not inserted a coin")
以及拍摄照片的代码,完成照片并保存照片后,将变量设置为0
#Change the var to 0
coin_inserted = 0
此外:
在评论中,用户强调他们并不知道如何检测重合符。忽略上述代码并执行以下操作。
#Set up GPIO18 as input, this goes at the top of code
GPIO.setup(18, GPIO.in, pull_up_down=GPIO.PUD_DOWN)
然后,在您的代码结束时,永远循环直到插入硬币。
#Will loop until ctrl+c
while True:
if GPIO.input(18):
take_picture_function()
sleep(0.1)