Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4792

Python • Help with detecting events with Buttons

$
0
0
Newbie at Python and Raspberry Pi usage here; I had a question regarding event detection and buttons.

I have servos which I want to execute a specific coordinated motions with different button presses. I've been testing out sample code just to learn more about buttons (see below), but had a few specific questions I was hoping someone could answer for me:

If I have multiple event detections set up does python prevent other events from being detected? I.E If I pressed a button and the servo's are executing a specific motion for me, what happens when another button to execute a different set of motions with the same servo's is pressed? Does it stop in the middle of executing the first set of instructions at the point when the 2nd button was pressed, jump into the code for the 2nd button, and then return back once it's finished?

Code:

import RPi.GPIO as GPIOimport time BUTTON = 21#Define button press functions in order to activate certain motionsdef button_change(button):    print("Button pressed.")GPIO.setmode(GPIO.BCM)GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.add_event_detect(BUTTON, GPIO.BOTH, callback=button_change, bouncetime=20)while TRUE:    time.sleep(1)print("Press CTRL-C to exit.")

Statistics: Posted by nhanderr — Sun May 19, 2024 7:56 pm



Viewing all articles
Browse latest Browse all 4792

Trending Articles