I have a problem in controlling the servo motors. I want to do something like this, if i press a button on my usb gamepad the servo will rotate clockwise and if I press another button that same servo will rotate anticlockwise. What happens is that if I press a button ti goes from 0 to 180 degrees and 180 to 0 degrees. but i cant control them independently. Could anyone plz post a code how to control servos independently.
Here is a part of the whole code to show actually which one I am using
def handleJoyEvent(e):
# Assign actions for Button DOWN events
if e.type == pygame.JOYBUTTONDOWN:
# Button 1 (trigger)
if (e.dict['button'] == 0):
print "Trriger Down"
# Set pin 13 LED to HIGH for digital on/off demo
servo.move(99, 180)
# Button 2
if (e.dict['button'] == 1):
print "Button 2 Down"
servo.move(5, 180) #This is used to select servo no. 5 and rotate it to 180 degrees
# Button 4
if (e.dict['button'] == 3):
servo.move(5, 0) # #This is used to select servo no. 5 and rotate it back to 0 degrees
print "Button 4 Down"
Here button == 1 will rotate the servo clockwise and button == 3 will rotate that servo anticlockwise . Also there is another file servo.py where there is move(servo, angle) function. here servo means number which sends data to switch statement below.
Here is my arduino code
case 5:
if(pos == 180)
{
//What code should be written to rotate it clockwise as long as the button is pressed
}
if(pos == 0)
{
////What code should be written to rotate it anti-clockwise as long as the button is pressed
}
servo5.write(fpos);
delay(1000);
break;
I have used the switch statement because its easier to communicate with usb gamepad through serial.read()