User avatar
Sabine
Posts: 10
Joined: Thu Jan 02, 2014 12:14 pm

Button, first press ON / second press OFF

Sun Feb 02, 2014 12:48 pm

Hi guys,

I have, I think, a simple question. But I just can't wrap my head around it.

As an introduction:
I have a PS3 controller connected to the RPI from which I can read the input.
This I can read like:

Code: Select all

if(state['square'] == True):
similar like:

Code: Select all

 if(GPIO.input(24) == True):
I know how to set up the pins and input/output of the GPIO

So this is the question:
How can I do the following?
If you press the square button a LED-pin should go HIGH
if you press it again it should go LOW

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Button, first press ON / second press OFF

Sun Feb 02, 2014 1:44 pm

You need to use a flag and then perform an action depending on the status of this flag.

Early on in your code, you need to initialise the flag:

Code: Select all

is_on = False
Then, when testing for the button:

Code: Select all

if(state['square'] == True):
  if is_on:
    #code to switch off
    is_on = False
  else:
    #code to switch on
    is_on = True
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Sabine
Posts: 10
Joined: Thu Jan 02, 2014 12:14 pm

Re: Button, first press ON / second press OFF

Sun Feb 02, 2014 3:34 pm

Thanks, I think I figured it out for the most part.
This is what I did with the info you gave me.
Is there a cleaner way to do this (and maybe with multiple buttons)

setup:

Code: Select all

laserState = False
laserStateOld = False

Code: Select all

            if(laserStateOld == False):
                if(laserState == False):
                    if(state[buttonLaser] == True):
                        GPIO.output(pinLaser, GPIO.HIGH)
                        laserState = True
                if(laserState == True):
                    if(state[buttonLaser] == False):
                        laserStateOld = True

            if(laserStateOld == True):
                if(laserState == True):
                    if(state[buttonLaser] == True):
                        GPIO.output(pinLaser, GPIO.LOW)
                        laserState = False
                if(laserState == False):
                    if(state[buttonLaser] == False):
                        laserStateOld = False

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Button, first press ON / second press OFF

Sun Feb 02, 2014 7:27 pm

If that works, then fine, but I'm not quite sure why you need the two flags unless it's something to do with bouncing on the GPIO pins (I don't use the GPIO pins in any scripts like this).

If it were me, I'd just do it like this (assuming setting high to turn it on):

Code: Select all

laser_is_on = False

if(state['square'] == True):
  if laser_is_on:
    GPIO.output(pinLaser, GPIO.LOW)
    laser_is_on = False
  else:
    GPIO.output(pinLaser, GPIO.HIGH)
    laser_is_on = True
This should work as it starts off setting the flag to False so, when the button is pressed for the first time, the script sees that the flag is False so it sets the pin to high and then sets the flag to True. Then, when the button is pressed the next time, as the flag is True, the script sets the pin to Low.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”