Page 1 of 1

Help on buttons

Posted: Sat Apr 29, 2017 8:06 pm
by pips
I need a code which the program asks the user to press either button 1,2 or 3. If button 1 is pressed, function1 will run. If button 2 is pressed, function2 will run and if button 3 is pressed, function3 will run. Any help please? Thank you :)

Re: Help on buttons

Posted: Sat Apr 29, 2017 11:48 pm
by FlexibleSigmoid
Which programming languages are you comfortable creating with?

Re: Help on buttons

Posted: Sun Apr 30, 2017 6:33 am
by B.Goode
If you are prepared to code in Python the gpiozero library has built-in support for exactly what you describe.

https://gpiozero.readthedocs.io/en/stable/

Re: Help on buttons

Posted: Sun Apr 30, 2017 10:29 am
by pcmanbob
Hi.

if you just want to check to see which of 3 buttons are pressed assuming they are connected to the GPIO
then you code could be something as simple as this:-

Code: Select all

def function1():
#this is function 1


def function2():
#this is function 2


def function3():
#this is function 3



while True:

	if gpio 16 == 1:
		function1()
		
	if gpio 20 == 1:
		function1()	
		
	if gpio 21 == 1:
		function1()	
		
	sleep(0.1)
please note this is not a working program it is only example code.

Re: Help on buttons

Posted: Tue May 02, 2017 10:36 pm
by pips
I use Python 3.0. Was wondering if I could do a Basketball Scoring System with 3 game modes, and when the user is asked which game mode he wants to play, he chooses the specific button..

Re: Help on buttons

Posted: Tue May 02, 2017 10:37 pm
by pips
FlexibleSigmoid wrote:Which programming languages are you comfortable creating with?
Python 3.0

Re: Help on buttons

Posted: Tue May 02, 2017 10:38 pm
by pips
B.Goode wrote:If you are prepared to code in Python the gpiozero library has built-in support for exactly what you describe.

https://gpiozero.readthedocs.io/en/stable/
Can you tell me which one please? Thanks again..

Re: Help on buttons

Posted: Wed May 03, 2017 9:21 am
by pcmanbob
detecting which button is presses and then calling the version of the game scoring you want is probably going to be the simplest part of your program.
assuming buttons wired normally low / high on press connected to GPIO 16, 20, 21
this program will do what you want.

Code: Select all

import RPi.GPIO as GPIO
import time

def game1():
    print("you pressed button 1 ")
    time.sleep(10)

def game2():
    print("you pressed button 2 ")
    time.sleep(10)


def game3():
    print("you pressed button 3 ")
    time.sleep(10)



while True:

   if GPIO.input(16) == 1:
      game1()
      
   if GPIO.input(20) == 1:
      game2()   
      
   if GPIO.input(21) == 1:
      game3()   
      
   sleep(0.1)

Re: Help on buttons

Posted: Wed May 03, 2017 9:29 am
by B.Goode
pips wrote:
B.Goode wrote:If you are prepared to code in Python the gpiozero library has built-in support for exactly what you describe.

https://gpiozero.readthedocs.io/en/stable/
Can you tell me which one please? Thanks again..
If you mean which version of gpiozero, then probably choose 'stable' unless you have some reason not to.

If you mean which Recipe, refer to the examples for Button, here: https://gpiozero.readthedocs.io/en/stab ... tml#button and read until you find the heading Run a function every time the button is pressed: