Aha! The slider acting as a brake is a good idea!
Looks like this is the
button.py you're using? https://github.com/simonmonk/squid/blob/master/button.py
Normally we call this method of GPIO access/monitoring "polling", although I'm not sure how you would describe a singular event in that case so "interrupt" is as good a word as any. (RPi.GPIOs interrupts are just really low level polling anyway!)
I can't yet see why it results in the behaviour you describe, but using RPi.GPIOs interrupts is simple enough that, perhaps, it would be better to try and refactor some of the code to use those.
You could do something like:
import RPi.GPIO as GPIO
BTN_LEFT = 8
BTN_RIGHT = 18
BTN_CENTRE = 9
GPIO.setup(BTN_LEFT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_RIGHT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(BTN_CENTRE, GPIO.IN, pull_up_down=GPIO.PUD_UP)
current_direction = "left"
current_direction = "right"
current_direction = "forwards"
GPIO.add_event_detect(BTN_LEFT, GPIO.FALLING, callback=go_left, bouncetime=200)
GPIO.add_event_detect(BTN_RIGHT, GPIO.FALLING, callback=go_right, bouncetime=200)
GPIO.add_event_detect(BTN_CENTRE, GPIO.FALLING, callback=go_forwards, bouncetime=200)
if current_direction == "left":
# Make robot go left
if current_direction == "right":
# Make robot go right
if current_direction == "forwards":
# Make robot go forwards
This style decouples the button handling from the code that governs the behaviour of the robot. It makes it difficult to do express like
if BUTTON_LEFT and BUTTON_RIGHT then do this though.
add_event_detect is RPi.GPIOs method of handling "interrupts", but I'm pretty sure it just implements a polling loop in C code since the Pi doesn't have hardware interrupts on all pins.