SkeeBallPi
Posts: 1
Joined: Thu Feb 06, 2020 1:31 am

Multiple GPIO inputs

Thu Feb 06, 2020 1:45 am

Hi, all. Newbie here. My experience is with VB and SQL, not Python. This is my first Python anything, so as my 7 year old says "go easy on me."

I've built a skee ball machine, and it works great! Now I want score kept without me doing it, and a Pi seemslike a good way. I've set up 7 momentary switches, and verified that a ball does, in fact, cause the contacts to close. Good start. Now what I have to do, is get scores attributed to each button and present the total to the LCD I'm using as a scoreboard. I have zero idea how to get that done. I've found several examples of using a button to fire up a LED, but can't find anything on the continuous loop needed to be able to assign values, and keep those values in a variable.

I know the pseudo codebelow won't work, but I'm offering it up just as a "I'm not just asking for a solution without at least thinking about it just a little bit," and a bit of context. I know "wait for press" won't work, I threw it in there to maybe start to test basics. And I don't know how to make certain that momentary contact doesn't occur while the program isn't at that point in the loop. I'm sure there is something easy and baked in.

So I don't know much, other than how to build a skee ball machine that can't keep score :D

Any guidance would be appreciated. I'm sure something is out there that I could learn to modify.

Thank you,

Stephen.



########################## bad pseudo code below:
from gpiozero import Button
from time import sleep



button100 = Button(2)
button101= button(3)
button50=button(4)
button40=button(17)
button30=button(27)
button20=button(22)
button10=button(10)
buttonReset=button(9)

score=0

#### Begin loop. No idea how to do this.

button100.wait_for_press()
score=score+100 #increments score based on value of the target hit
sleep(.5) #prevents "bounce" in the sensor from recording multiple scores

button101.wait_for_press()
score=score+100
sleep(.5)

button50.wait_for_press()
score=score+50
sleep(.5)

button40.wait_for_press()
score=score+40
sleep(.5)

button30.wait_for_press()
score=score+30
sleep(.5)

button20.wait_for_press()
score=score+20
sleep(.5)

button10.wait_for_press()
score=score+10
sleep(.5)

buttonReset.wait_for_press()
score=0

Print(score)

### Loop return

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Multiple GPIO inputs

Thu Feb 06, 2020 11:24 am

Hi.

I am not a gpiozero user so not 100% sure how to do it using gpiozero,

but you can do the same thing using RPi.GPIO , you need to use event detect so as not to block the operation of the program and allow you to print the score while detecting the buttons.

example code

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)

score = 0

def button1(channel):
    global score
    score=score+50 
    
def button2(channel):
    global score
    score=score+100     
    
    
GPIO.add_event_detect(7, GPIO.FALLING, callback=button1, bouncetime=200)
GPIO.add_event_detect(8, GPIO.FALLING, callback=button1, bouncetime=200)

while True:
    print ("Score :", score)
    time.sleep(2)
    
untested code expect errors

So first thing I would avoid using gpio 2,3,4 as these have other uses which and cause problems with gpio input.

So we have 2 switches connected between gpio 7 and 8 and a ground pin, set-up as inputs with pull ups on them.

Then we have 2 functions which is what the closing of a switch will call.

Then we have the 2 lines that detect the inputs falling when a switch is closed, these are what calls the functions.

Finally there is a while true loop that prints the score every 2 seconds.

for more details on using the above functions see : https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/

Hopes this helps.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Multiple GPIO inputs

Thu Feb 06, 2020 11:32 am

Use plain language pseudo code to explain what you want to do. There are lots of errors in the gpiozero usage which detract from what you are trying to achieve.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Multiple GPIO inputs

Thu Feb 06, 2020 11:37 am

To continue with gpiozero, which does a lot of the implementation for you, have a look at when_pressed instead of wait_for_press.

https://gpiozero.readthedocs.io/en/stab ... tml#button

PiGraham
Posts: 3939
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Multiple GPIO inputs

Thu Feb 06, 2020 12:59 pm

It looks fairly simple.If you have reliable switch operation when the ball drops thorough the hole on a scoring zone you can use when_pressed for each switch to add some score increment to a global total.
Setup the events then loop checking for the score variable to change and update the display when it does.

You may need to specify a bounce_time to avoid double counting.

gpiozero.Button ring1(….)
ring1..when_pressed = yourScoreFunction

Return to “Beginners”