Mind the variable creations as they are handled. My questions are the following.

1) Why do we have 2 whiles here? Does the sleep at the end of the first part does really anything significant?

2) Whenever there is no press of the button, logically the press should be 0, but whenever I print print(press) after the press variable, I always get 1.

3) Following up on question 2, the part GPIO.output(lightPin, not GPIO.input(buttonPin)) uses opposite logic to check if there is a press turn on the light. Same concern here as question 2.

    while True:
        while True:
            press = GPIO.input(buttonPin)
            if (press == 1):
            GPIO.output(lightPin, not GPIO.input(buttonPin))
            print("Led is on.")
            GPIO.output(lightPin, 0)
    GPIO.output(lightPin, False)
  • Is your button wired with a pull-up or a pull-down resistor? If it's pull-up, that would explain (2) and (3). – anonymoose Nov 5 '17 at 12:05
  • Are you sure the indentation is right? If GPIO.output(lightPin, 0) was unindented one level, this would make much more sense. – anonymoose Nov 5 '17 at 12:09
  • If you found this code somewhere, did it have a description of how it works next to it? – anonymoose Nov 5 '17 at 12:13

Not quite sure what you are trying to achieve so here is a script that uses pull with explanations, maybe it'll help.

#!/usr/bin/env python2.7

from time import sleep
import subprocess
import RPi.GPIO as GPIO

CHANNEL = 17 # GPIO channel 17 is on pin 11 of connector P1
# it will work on any GPIO channel

GPIO.setup(CHANNEL, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# setup the channel as input with a 50K Ohm pull up. A push button will ground the pin,
# creating a falling edge.

def system_action(CHANNEL):
    print('Button press = negative edge detected on channel %s'%CHANNEL)
    button_press_timer = 0
    while True:
            if (GPIO.input(CHANNEL) == False) : # while button is still pressed down
                button_press_timer += 1 # keep counting until button is released
            else: # button is released, figure out for how long
                if (button_press_timer > 7) : # pressed for > 7 seconds
                    print "long press > 7 : ", button_press_timer
                    # do what you need to do before halting
                    subprocess.call(['shutdown -h now "System halted by GPIO action" &'], shell=True)
                elif (button_press_timer > 2) : # press for > 2 < 7 seconds
                    print "short press > 2 < 7 : ", button_press_timer
                    # do what you need to do before a reboot
                    subprocess.call(['sudo reboot &'], shell=True)
                button_press_timer = 0

GPIO.add_event_detect(CHANNEL, GPIO.FALLING, callback=system_action, bouncetime=200)
# setup the thread, detect a falling edge on channel 17 and debounce it with 200mSec

# assume this is the main code...
    while True:
        # do whatever
        # while "waiting" for falling edge on port 17
        sleep (2)

except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

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.