Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

GPIO press-on press-off help!

Fri Jan 31, 2014 2:42 pm

Hi guys,

so I have the following code that turns an LED on when a button is pressed. No problem there. What I need to be able to do is turn the LED off when the button is pressed again.

I've been on this for a couple of days and cant seem to fathom it out.

I'm in the process of building a camera and this issue is the basis for starting/stopping the motion detection script (which will use the LED as an indicator).

Code: Select all

import RPi.GPIO as GPIO

# Define a function to keep script running
def loop():
    raw_input()


# Define a function to run when an interrupt is called
def LEDON(pin):
	GPIO.output(25, True)
		         
GPIO.setmode(GPIO.BCM) #Set pin numbering to GPIO numbering
GPIO.setup(24, GPIO.IN) # Set up GPIO 24 as an input (button)
GPIO.setup(25, GPIO.OUT) # Set GPIO 25 as an output (LED)
GPIO.output(25, False)

GPIO.add_event_detect(24, GPIO.RISING, callback=LEDON, bouncetime=200)

loop() # Run the loop function to keep script running
I know there's more than one way to skin a cat, so if the above code is not a good basis (it was adapted from elsewhere) then please do share etc.

This is the schematic for the little circuit:
button.jpg
button.jpg (35.89 KiB) Viewed 1056 times

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: GPIO press-on press-off help!

Fri Jan 31, 2014 3:38 pm

Use a flag. EG. If the button is pressed and your flag is False (myFlag = False) start recording and set the Flag to True. If the button is pressed and the flag is True do the opposite.
You'll probably need to check for the button being pressed AND released to stop the code flicking between the 2 conditions while the button is being pressed.

Dave.
Apple say... Monkey do !!

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: GPIO press-on press-off help!

Fri Jan 31, 2014 4:01 pm

Hi,

... and a hardware advice: add a resistor between GPIO24 and existing resistor-button junction. This additional resistor will protect your GPIO pin in case you'd accidently set it to output, with value set to high, and then the button would be pressed.

This resistor value should be few hundred ohms.


Best wishes, Ivan zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Re: GPIO press-on press-off help!

Fri Jan 31, 2014 5:42 pm

Hi guys,

Cheers for the info - that resistor hadn't occurred to me to be honest! I'll add that in asap.

With regard to the flags, I can definitely see the logic behind it but code wise I'm lost!

Would anyone be willing to write me a (very short) example that I can then build upon?

Cheers

Mark

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

Re: GPIO press-on press-off help!

Fri Jan 31, 2014 6:03 pm

Code: Select all

#!/usr/bin/env python

import RPi.GPIO as GPIO

led_state = False

# Define a function to keep script running
def loop():
    raw_input()


# Define a function to run when an interrupt is called
def LED_TOGGLE(pin):
    global led_state
    led_state = not led_state
    GPIO.output(25, led_state)
                   
GPIO.setmode(GPIO.BCM) #Set pin numbering to GPIO numbering
GPIO.setup(24, GPIO.IN) # Set up GPIO 24 as an input (button)
GPIO.setup(25, GPIO.OUT) # Set GPIO 25 as an output (LED)
GPIO.output(25, led_state)

GPIO.add_event_detect(24, GPIO.RISING, callback=LED_TOGGLE, bouncetime=200)

loop() # Run the loop function to keep script running

Viper1953
Posts: 19
Joined: Tue Dec 31, 2013 10:09 am

Re: GPIO press-on press-off help!

Fri Jan 31, 2014 6:45 pm

Joan, that worked a treat! thankyou! I can see how it makes sense now!

To further expand a little - how can I start a script with the first button press (and still keep the LED on), and then interrupt the script with the second button push (and turn the LED off)?

Return to “Python”