gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

Edge problem

Wed Aug 19, 2015 4:13 pm

Hello!
I watched a YouTube video where I got my button working, the code is:

Code: Select all

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#variables
atHome = 0
#Output the LED
GPIO.setup(21,GPIO.OUT)
#Input the button
GPIO.setup(17,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def LED(pin):
        #If I click the button
        if(pin==17):
                #I want to turn on the led
                output = 21
        #if the led is high put it low
        if GPIO.input(output):
                GPIO.output(output,GPIO.LOW)
                atHome = 0
                print atHome
        else:
                #if low put it high
                GPIO.output(output,GPIO.HIGH)
                atHome = 1
                print atHome
#Event for listening
GPIO.add_event_detect(17,GPIO.RISING,callback=LED,bouncetime=200)

value = input("Press Any Key To Continue")
GPIO.cleanup()

This code was just for testing, but my real problem starts with my project at main.py

So I have setup everything and at the hearth of the program there is a try, while loop. Now I don't know where to put the event detector.
Here is the code:

Code: Select all

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#variables
atHome = 0
#Output the LED
GPIO.setup(21,GPIO.OUT)
#Input the button
GPIO.setup(17,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def LED(pin):
        #If I click the button
        if(pin==17):
                #I want to turn on the led
                output = 21
        #if the led is high put it low
        if GPIO.input(output):
                GPIO.output(output,GPIO.LOW)
                atHome = 0
                print atHome
        else:
                #if low put it high
                GPIO.output(output,GPIO.HIGH)
                atHome = 1
                print atHome
try:
        while True:
                GPIO.add_event_detect(17,GPIO.RISING,callback=LED,bouncetime=200)
                # some other if statements here 50+ lines
except KeyboardInterrupt:
        pass
print "Offline"
GPIO.cleanup()

Thx! :ugeek: :ugeek: :roll: :lol:

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Edge problem

Wed Aug 19, 2015 4:54 pm

You would call the event detection routine only once, so it could look something like (untested):

Code: Select all

import RPi.GPIO as GPIO

def LED(pin):
        #If I click the button
        if(pin==17):
                #I want to turn on the led
                output = 21
        #if the led is high put it low
        if GPIO.input(output):
                GPIO.output(output,GPIO.LOW)
                atHome = 0
                print atHome
        else:
                #if low put it high
                GPIO.output(output,GPIO.HIGH)
                atHome = 1
                print atHome


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#variables
atHome = 0
#Output the LED
GPIO.setup(21,GPIO.OUT)
#Input the button
GPIO.setup(17,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.add_event_detect(17,GPIO.RISING,callback=LED,bouncetime=200)


try:
        while True:
                sleep(1) # or more, it doesn't matter for the GPIO event detection

except KeyboardInterrupt:
        pass
print "Offline"
GPIO.cleanup()

gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

Re: Edge problem

Wed Aug 19, 2015 6:05 pm

Yes, I tried that kind of code, but it's buggy, it turns self off, but I got the solution for it.

It's totally different then this :) But thx :)

If anybody wants the code, comment here
:lol: :lol: 8-) 8-)

Return to “Python”