import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
def LEDflash(channel):
# DeactivateButtons()
inputchannel = channel
for i in range(1,5):
if inputchannel == button[i]:
GPIO.output(LED[i], GPIO.HIGH)
time.sleep(0.5)
GPIO.output(LED[i], GPIO.LOW)
# ActivateButtons()
def ActivateButtons():
for i in range (1,5):
GPIO.add_event_detect(button[i], GPIO.FALLING, callback=LEDflash, bouncetime=200)
def DeactivateButtons():
for i in range (1,5):
GPIO.remove_event_detect(button[i])
# Label buttons and pins
button = [4, 12, 16, 20, 21]
LED = [22, 5, 6, 13, 19]
# Set GPIO pins
for i in range (0,5):
GPIO.setup(button[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(LED[i], GPIO.OUT)
ActivateButtons()
GPIO.output(LED[0], GPIO.HIGH)
GPIO.wait_for_edge(button[0], GPIO.FALLING)
GPIO.output(LED[0], GPIO.LOW)
GPIO.cleanup()
This code flashes the LEDs on for a half second when the button is pressed. You will notice that there are two commented commands in the LEDflash function. The program behaves erratically when that code is included. And really, it seems to be trying to call the ActivateButton() command that is causing problems because if I just run the DeactivateButtons() command, it seems to run just fine. For the first two buttons, there are no issues. But when I try to press the other two buttons, the program behaves strangely.
If I press button 3, it will maybe repeat the loop a couple times and then exit the program abruptly. If I press button 4, it runs the loop exactly once and then exist abruptly. It does not proceed to the line that turns off the primary LED. I have no reasonable explanation for this behavior. I even tried switching the physical buttons to see if there was some sort of sticking that was happening. I toyed around with this for about an hour with no successful resolution.
So I went over to the Raspberry Pi forums and signed up. Maybe I'll get an answer from the community.
No comments:
Post a Comment