The first thing I wanted to play with is to work with the channel parameter that was passed in the interrupt sample code. It does turn out that it's passing the number of the pin along, which means that a single callback function can be used to identify multiple pins. This allowed me to simplify the last example.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def my_callback(channel):
print "falling edge detected on", channel
GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback, bouncetime=300)
try:
GPIO.wait_for_edge(24, GPIO.RISING)
except KeyboardInterrupt:
GPIO.cleanup()
GPIO.cleanup()
As I was skimming the documentation, I noticed that there was also a GPIO.BOTH to detect both rising and falling voltage. So of course I was going to try it. The issue I quickly discovered is that there was no parameter being passed to indicate whether it was a rising edge or a falling edge. However, a quick internet search came up with another tutorial for dealing with GPIO.BOTH.I decided that things were getting a bit confusing with having a mixture of pull-up and pull-down resistors, so I decided I'd just start from scratch and build things in a way that makes sense to me. In my mind, pressing a button down should correspond to a falling edge. This meant I needed to have the pins set as a pull-up, and that the buttons should connect to ground when pressed. I also made it so that connecting the loose wires would lead to the end of the program.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# Two buttons connected to ground when pressed
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Loose wires connected to ground when touched
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def button_change(channel):
if GPIO.input(channel) == 1:
print "Button on pin", channel, "was released"
else:
print "Button on pin", channel, "was pressed"
GPIO.add_event_detect(23, GPIO.BOTH, callback=button_change)
GPIO.add_event_detect(24, GPIO.BOTH, callback=button_change)
try:
GPIO.wait_for_edge(22, GPIO.FALLING)
except KeyboardInterrupt:
GPIO.cleanup()
GPIO.cleanup()
By swapping GPIO.BOTH for either GPIO.RISING or GPIO.FALLING I was able to confirm that I had everything set up the way I wanted. However, in the process I discovered more artifacts about the physical nature of the project. Sometimes, even if the event was set to trigger on GPIO.FALLING I would get some "button released" feedback and sometimes I would get multiple presses registered.The multiple presses was a result of the bounce effect, and was resolved by adding in a bounce time delay. I set it to 200ms because I like the idea of two tenths of a second more than I like the idea of three tenths of a second. (That's probably an even divisibility thing. I would probably need to get some more sophisticated meters to determine the actual bounce times.) But I'm not entirely sure how to resolve the other issue. It might require a physical solution, like shoving a capacitor across the pins of the button like in the tutorial. But since I don't have any capacitors, I wasn't able to try that out.
I've reached a point where I think I've somewhat maxed out what I can do with these buttons in isolation. I can read button presses and turn LEDs on and off. So I think it's time for me to engage in slightly larger projects. The Dummies book has some instructions for making a Simon-like game (memory game), which seems like an interesting project. But I'm going to try to do it without using the book. I think it would be better for me to go through the practice of thinking through the logic and learning to code in Python on my own and not just copy code that already exists. And maybe I'll learn some PyGame (a graphics engine for Python) in the process.
I'll have to spend some time thinking about how exactly I would want to put that project together. I'm going to be traveling again soon, so I'll probably start with the programming portion and work my way back to the buttons and LEDs later.
No comments:
Post a Comment