I hooked up a servo onto my RaspberryPi 3, and i want to control it. I am currently using pygame library. It is installed and is a latest version. This is my code :
# Import libraries
import RPi.GPIO as GPIO
import time
import pygame
from pygame.locals import *
pygame.init()
# Set GPIO numbering mode
GPIO.setmode(GPIO.BOARD)
# Set pin 11 as an output, and define as servo1 as PWM pin
GPIO.setup(11,GPIO.OUT)
servo = GPIO.PWM(11,50) # pin 11 for servo1, pulse 50Hz
# Start PWM running, with value of 0 (pulse off)
servo.start(0)
angle = 90.0
servo.ChangeDutyCycle(2+(angle/18))
time.sleep(0.5)
servo.ChangeDutyCycle(0)
changed = False
while 1:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
if angle > 10:
angle = angle - 5
changed = True
elif event.key == pygame.K_d:
if angle < 170:
angle = angle + 5
changed = True
elif event.key == pygame.K_q:
break
if changed:
servo.ChangeDutyCycle(2+(angle/18))
time.sleep(0.5)
servo.ChangeDutyCycle(0)
time.sleep(1.5)
changed = False
#Clean things up at the end
servoH.stop()
GPIO.cleanup()
pygame.quit()
But it does not detect an event. I keep hitting the keybord keys and the servo just stands still. I debugged it, and it detects no keybord/mouse events what so ever.
Please help.