如果硬币已插入,请尝试存储一个变量:
#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)