Python PhotoBooth no Buttonpress until Coin inserted

224 Views Asked by At

I'm VERY new to Python and am currently programming a Photobooth. I want to deactivate the Buttonpress until a coin is tossed and want it to deactivate when the photo is Saved. I just don't get how i could code it that the action ButtonPress works until something happens.

I know my code is very chaotic but it works fine overall. I program it on a Raspberry pi and have the Coin Acceptor on GPIO 18. I get 10 Pulses because it is a coin acceptor which only works with 1€ coins.

Code in github

this is the code I'm using.

Would be awesome if somebody could lighten my darkness! Thanks in advance!

1

There are 1 best solutions below

10
On BEST ANSWER

Try having a variable to store if the coin has been inserted:

#Default is not inserted (start of script)
coin_inserted = 0

then when a coin is inserted and you get the ten pulses:

#Change the var to 1
coin_inserted = 1

on your bit of code where you detect your button press:

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")

and on your code that takes a photo, when it's done and the photo is saved, set the variable back to 0

#Change the var to 0
coin_inserted = 0

Additionally:

In comments, user highlighted that they didn't know how to detect the coincounter. Disregard the above code and do the following.

#Set up GPIO18 as input, this goes at the top of code
GPIO.setup(18, GPIO.in, pull_up_down=GPIO.PUD_DOWN)

then, at the end of your your code, loop forever until a coin is inserted.

#Will loop until ctrl+c
while True:
    if GPIO.input(18):
        take_picture_function()
        sleep(0.1)