Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PhotoBooth无按键,直到硬币插入_Python_Raspberry Pi_Photobooth - Fatal编程技术网

Python PhotoBooth无按键,直到硬币插入

Python PhotoBooth无按键,直到硬币插入,python,raspberry-pi,photobooth,Python,Raspberry Pi,Photobooth,我对Python非常陌生,目前正在编写一个Photobooth。 我想停用按钮,直到硬币被抛起,并希望它在照片保存时停用。 我只是不明白我怎么能把它编码成动作按钮在发生什么事之前都能工作 我知道我的代码非常混乱,但总体来说运行良好。 我在树莓圆周率上编程,并在GPIO 18上设置硬币接收器。 我得到10个脉冲,因为它是一个硬币接收器,只能处理1欧元的硬币 这是我正在使用的代码 如果有人能点亮我的黑暗,那就太棒了! 提前谢谢 如果硬币已插入,请尝试存储一个变量: #Default is not

我对Python非常陌生,目前正在编写一个Photobooth。 我想停用按钮,直到硬币被抛起,并希望它在照片保存时停用。 我只是不明白我怎么能把它编码成动作按钮在发生什么事之前都能工作

我知道我的代码非常混乱,但总体来说运行良好。 我在树莓圆周率上编程,并在GPIO 18上设置硬币接收器。 我得到10个脉冲,因为它是一个硬币接收器,只能处理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)

首先:谢谢你的快速回答!你帮了大忙!你能告诉我如何在GPIO 18上添加检测到的脉冲,并在脉冲到来时将其存储在“coin_inserted”(硬币插入)中吗?没问题,希望收到你的回复,这是否有效?这可能类似于如果GPIO.input(18)=true?哦,对不起,我以为你的代码中已经设置好了。将修改答案。@Dominikling我已将此代码添加到我的答案中。请删除“抛硬币”标签,它与您的任务无关。不要发布代码的链接或图片,而是在这里发布。