I'm getting started with a project on a Raspberry Pi (never done anything with RPi or python before, so it's all new to me, but I've done a little Arduino stuff). I'm trying to create a very simply python script to run a bit of code whenever a digital input signal switches from low to high or high to low.
I'm treating the input as a button, and using the
gpiozero library. I'm trying to use the callback method to run my code whenever the transitions take place.
The problem is that the callback only happens once. I expect each callback to run every time I make or break contact on the switch, so I should see "pressed" and "released" printed N times if I toggle the switch N times. But instead I only see a single line printed, as though the callbacks are firing once and then just stopping. The script does not exit (because of the "pause" call at the end), so that's not the problem.
I've based my work on the examples in the gpiozero Recipes. There is an example (See 3rd codeblock under the "Button" heading) that is almost exactly what I need, and the description says "Run a function every time the button is pressed", but my code only runs the function once.
Perhaps I'm making some newbie RPi or Python mistake, but I'm stuck. Please help me out. Thanks.
Here is my code:
from gpiozero import Button import signal button = Button(3) def pressed(): print("pressed") def released(): print("released") button.when_pressed = pressed button.when_released = released signal.pause()