raspberry pi - Python GPIO function input button toggle on falling edge -


i trying script work button monitored , if operated once turn on something. if operated again turn off. toggle button. have working following code not waits button edge trigger. other code in while loop not carried out. how can check in function if button has been operated , if not carry on rest of code.

import time import rpi.gpio gpio  gpio.setmode(gpio.bcm) # using broadcomm pin numbers  gpio.setup(21, gpio.in, pull_up_down=gpio.pud_up)  prevwater = 0  def buttoncontrol ():     global prevwater      # take reading     gpio.wait_for_edge(21, gpio.falling)     prevwater = not prevwater     return prevwater  while 1:     waterbutton = buttoncontrol ()     if (waterbutton == true):         print ("turn water on")     if (waterbutton == false):         print ("turn water off")     # check returned function     print (waterbutton)  # need other stuff here  gpio.cleanup() 

any appreciated

i have tried callback shown below

  import time   import rpi.gpio gpio    gpio.setmode(gpio.bcm) # using broadcomm pin numbers    gpio.setup(21, gpio.in, pull_up_down=gpio.pud_up)    prevwater = 0    def my_callback(channel):       global prevwater        print("button pressed")       prevwater = not prevwater       return prevwater    while 1:       gpio.add_event_detect(21, gpio.rising, callback=my_callback)       waterbutton = prevwater       if (waterbutton == true):           print ("turn water on")       if (waterbutton == false):           print ("turn water off")       # check returned function       print (waterbutton)    # need other stuff here    gpio.cleanup() 

but getting error shown below

file "test.py", line 19, in gpio.add_event_detect(21, gpio.rising, callback=my_callback) runtimeerror: conflicting edge detection enabled gpio channel

found out how call function shown below. script works turning on , off led when button pressed. problem have switch seems bouncing debounce time added. have heard small capacitor across switch may stop this. give go week

 import time  import rpi.gpio gpio    gpio.setmode(gpio.bcm) # using broadcomm pin numbers   gpio.setup(21, gpio.in, pull_up_down=gpio.pud_up)  gpio.setup(17, gpio.out)   prevwater = 0   def my_callback(channel):      global prevwater      prevwater = not prevwater      return prevwater   gpio.add_event_detect(21, gpio.rising, callback=my_callback, bouncetime=200 )   while 1:      waterbutton = prevwater      if (waterbutton == true):          gpio.output(17,gpio.low)      if (waterbutton == false):          gpio.output(17,gpio.high)   # need other stuff here   gpio.cleanup() 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -