0
votes

I am trying to get a script to work where a button is monitored and if operated once then turn on something. Then if operated again turn it off. Like a toggle button. I have it working with the following code however this is not good as it always waits for the button edge to trigger. So any other code in the while loop will not be carried out. How can I check in the function if a button has been operated and if not then carry on with the rest of the code.

import time
import RPi.GPIO as 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 a 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 what is returned from function
    print (waterButton)

# need to do other stuff here

GPIO.cleanup()

Any help will be much appreciated

I have tried callback as shown below

  import time
  import RPi.GPIO as 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 what is returned from function
      print (waterButton)

  # need to do other stuff here

  GPIO.cleanup()

but I am getting an error as shown below

File "test.py", line 19, in GPIO.add_event_detect(21, GPIO.RISING, callback=my_callback) RuntimeError: Conflicting edge detection already enabled for this GPIO channel

1
Configure GPIO events to trigger callbacks, rather than waiting for events: raspi.tv/2013/… - larsks
I will have to research this I tried call back but did not set it up correctly. - user2669997

1 Answers

0
votes

found out how to do it with call back function as shown below. this script works turning on and off the LED when the button is pressed. the only problem I have now is the switch seems to be bouncing even with the debounce time added. I have heard that a small capacitor across the switch may stop this. i will give it a go this week

 import time
 import RPi.GPIO as 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 to do other stuff here

 GPIO.cleanup()