I am writing a Python function to turn my servo motor left and right every time either one of the 2 images (left and right) is clicked.
This is my python script:
#!/usr/bin/python
def turnCamera (**data):
import pigpio
import time
servos=4
key = data['direction']
m=1500
while (m >= 500 and m <= 2500):
if (key =="left"):
m=m+100
elif (key =="right"):
m=m-100
pigpio.start()
pigpio.set_servo_pulsewidth(servos, m)
servostatus= "Servo {} {} micro pulses".format(servos[0], key, m)
print servostatus
time.sleep(1)
pigpio.stop()
return servostatus
I know this code will not work because everytime the function is called, the value of m
(servo position) will get reset to 1500, which is unwanted.
It works when m=1500, without the if and else statement. But when I run this code (with Flask server), it gave me this error: http://pastebin.com/eGgVQnML
What have I done wrong?
This is my jQuery script:
$('#left_button').click(function(){
$.post("{{ url_for('turn_servo_ajax') }}", {direction:"left"}).done(function (reply) {
$('#camerapos').empty().append(reply);
alert("left button clicked");});
});