0

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!

Domposter
  • 19
  • 6
  • Please remove the tag `coin-flipping`, it has nothing to do with your task. And don't post links or pictures of code, instead post [your code](https://stackoverflow.com/help/mcve) here on SO. – Mr. T Jan 02 '18 at 16:08

1 Answers1

1

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)
Harvey Fletcher
  • 1,167
  • 1
  • 9
  • 22
  • First of all: Thanks for the quick answer! You helped a lot! Could you tell me how i can add those detected Pulses on GPIO 18 and store them in "coin_inserted" if the pulse was coming? – Domposter Jan 02 '18 at 15:53
  • No problem, would love to hear back from you on if this worked or not – Harvey Fletcher Jan 02 '18 at 15:55
  • is it maybe something like if GPIO.input (18) = true? – Domposter Jan 02 '18 at 16:18
  • Oh, sorry, I thought you already had that set up in your code. Will amend answer. – Harvey Fletcher Jan 02 '18 at 16:21
  • @DominikLink i've added the code for this to my answer – Harvey Fletcher Jan 02 '18 at 16:36
  • fotobox.py:374: RuntimeWarning: No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead! GPIO.cleanup() Traceback (most recent call last): File "fotobox.py", line 267, in class QDialog_mod(QDialog): File "fotobox.py", line 370, in QDialog_mod if GPIO.input(18): RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) – Domposter Jan 02 '18 at 18:43
  • thanks for the ridiculous fast and awesome help! get this error now altough i have set the GPIO on line 273 if you look at GitHub :( sorry to annoy you!!! [link] (https://pastebin.com/U0ZdTY87) this is the code and how i added it (i guess i did that wrong aswell) would be so damn awesome if you could help me bring it to life! – Domposter Jan 02 '18 at 18:43
  • On line 330, you have "GPIO.setmode(GPIO.BCM)" if you move this up to line 14, do you still get the error? Also it seems like your "while True:" is on the wrong indentation level. It should be at level 0, but is at level 1. – Harvey Fletcher Jan 03 '18 at 13:40
  • fotobox.py:374: RuntimeWarning: No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead! GPIO.cleanup() Traceback (most recent call last): File "fotobox.py", line 268, in class QDialog_mod(QDialog): File "fotobox.py", line 370, in QDialog_mod if GPIO.input(18): RuntimeError: You must setup() the GPIO channel first Followed your steps (thanks again) this is what i get now. I think we are pretty close :D – Domposter Jan 03 '18 at 15:49
  • Remove the finally: GPIO.cleanup() from the try: block, in fact, just remove the try altogether. I've altered the answer above. Don't take out the while loop though. – Harvey Fletcher Jan 04 '18 at 17:21