pips
Posts: 5
Joined: Mon Apr 03, 2017 5:39 pm

Help on buttons

Sat Apr 29, 2017 8:06 pm

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

FlexibleSigmoid
Posts: 61
Joined: Sun Jul 03, 2016 1:33 am

Re: Help on buttons

Sat Apr 29, 2017 11:48 pm

Which programming languages are you comfortable creating with?

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

Re: Help on buttons

Sun Apr 30, 2017 6:33 am

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/

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

Re: Help on buttons

Sun Apr 30, 2017 10:29 am

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.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pips
Posts: 5
Joined: Mon Apr 03, 2017 5:39 pm

Re: Help on buttons

Tue May 02, 2017 10:36 pm

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..

pips
Posts: 5
Joined: Mon Apr 03, 2017 5:39 pm

Re: Help on buttons

Tue May 02, 2017 10:37 pm

FlexibleSigmoid wrote:Which programming languages are you comfortable creating with?
Python 3.0

pips
Posts: 5
Joined: Mon Apr 03, 2017 5:39 pm

Re: Help on buttons

Tue May 02, 2017 10:38 pm

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..

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

Re: Help on buttons

Wed May 03, 2017 9:21 am

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)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Help on buttons

Wed May 03, 2017 9:29 am

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:

Return to “General discussion”