0

This is the first project I am doing with my RaspberryPi B+ and the first time I'm coding in Python so I thought I'd run it every one to see if I got this right.

OK I've got 3 player buttons with corresponding LED and Buzzers for each. Along with a 4th button I control.

I want to make sure this program only allows whoever button is pressed first to go off only and ignore the other play button if they press after. I would like to also rest the program if I press my button.

import time
import RPi.GPIO a GPIO

GPIO.setmode(GPIO.BOARD)
#button 1
GPIO.setup(4, GPIO.IN,pull_up_down=GPIO.PUD_UP)
#button 2
GPIO.setup(17, GPIO.IN,pull_up_down=GPIO.PUD_UP)
#button 3
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)
#button 4
GPIO.setup(27, GPIO.IN,pull_up_down=GPIO.PUD_UP)
#LED 1
GPIO.setup(23, GPIO.OUT)
#LED 2
GPIO.setup(24, GPIO.OUT)
#LED 3
GPIO.setup(25, GPIO.OUT)
#BUZZER 1
GPIO.setup(13, GPIO.OUT)
#BUZZER 2
GPIO.setup(16, GPIO.OUT)
#BUZZER 3
GPIO.setup(26, GPIO.OUT)

Button1 = GPIO.input(4)
Button2 = GPIO.input(17)
Button3 = GPIO.input(18)
ResetButton = GPIO.input(27)

def play
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(16, GPIO.LOW)
GPIO.output(26, GPIO.LOW)

FirstPress = 0

while (FirstPress == 0):
if Button1 == False:
    print("button1")
    FirstPress = 1

if Button2 == False:
    print("button2")
    FirstPress = 2

if Button3 == False:
    print("button3")
    FirstPress = 3

if FirstPress == 1:
GPIO.output(23,GPIO.HIGH)
GPIO.output(13,GPIO.HIGH)
sleep(.2)
GPIO.output(13,GPIO.LOW)



if FirstPress == 2:
GPIO.output(24,GPIO.HIGH)
GPIO.output(16,GPIO.HIGH)
sleep(.2)
GPIO.output(16,GPIO.LOW)
sleep(.2)
GPIO.output(16,GPIO.HIGH)
sleep(.2)
GPIO.output(16,GPIO.LOW)    

if FirstPress == 3:
GPIO.output(25,GPIO.HIGH)
GPIO.output(26,GPIO.HIGH)
sleep(.2)
GPIO.output(26,GPIO.LOW)
sleep(.2)
GPIO.output(26,GPIO.HIGH)
sleep(.2)
GPIO.output(26,GPIO.LOW)
sleep(.2)
GPIO.output(26,GPIO.HIGH)
sleep(.2)
GPIO.output(26,GPIO.LOW)

if ResetButton == False
play()  
3

To keep your developpement fluid, you may want to write your code for only one button, test it, then duplicating it for the other one when you are sure it's working.

About your code, it seems you have some syntax errors :
def play miss its :, and then code just below is not indentated.
The if FirstPress == sections also lacks of indentation.

For a better code maintenance, it's also a good idea to define variable name for storing pins number, i.e. led1= 23; or leds=[23,24,25]. This help to avoid errors, and allow to easily modify the code when you change the physical connections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.