Admit it, if you have been following my recent articles, you knew this was coming ;)
Let's pull these things together!
We know these servos accept a maximum and minimum pulse width (signal), so all we do is increment and decrement while checking for those values.
I was asked why I am using the buttons instead of the joysticks. The answer is, it's simply easier to show the code right now if we keep it simple. Keep following these articles and all will become clear ;)
from evdev import InputDevice
import pigpio
pi = pigpio.pi()
tilt = 17
pan = 27
x = 1250
y = 1250
# want to display ALL info?
debug = False
if debug:
for device in evdev.list_devices():
print(evdev.InputDevice(device))
input("Press Enter to continue ...")
# create object to read input - specify YOUR device
controller = InputDevice('/dev/input/event0')
# what are the controller details?
if debug:
print(controller)
# read controller info in an infinite loop
for event in controller.read_loop():
# here I am looking for the start button
# so I can quit
if event.type == 1:
exit()
# set as true above if you want to look up
# different button values
if debug:
print("\n\nEvent Type:{}".format(event.type))
print("Event Code:{}".format(event.code))
print("Event Value:{}".format(event.value))
# look for pad moves
if event.type == 3:
# up and down is 17
if event.code == 17:
if event.value == 1:
print("\n\n\nPad Up\n")
if y > 600: y -= 100
if event.value == -1:
print("\n\n\nPad Down\n")
if y < 2400:y += 100
pi.set_servo_pulsewidth(tilt, y)
# left and right is 16
if event.code == 16:
if event.value == -1:
print("\n\n\nPad Left\n")
if x > 600: x -= 100
if event.value == 1:
print("\n\n\nPad Right\n")
if x < 2400: x += 100
pi.set_servo_pulsewidth(pan, x)