servo.MoveToDegree int float or other


#1

I want move a servo by serial command. This code not work. The servo run. The serial read the command, but movetodegree not work. I’ve converted the value to int. It’s correct?
from drivers.servo
import servo import streams
s=streams.serial()
MyServo=servo.Servo(D11.PWM,400,2400,500,3000)
while True:
sleep(200)
print(“Servo ON”)
MyServo.attach()
sleep(3000)
print(“insert degree:”)
MyServo.moveToDegree(0)
sleep(300)
MyServoX.moveToDegree(180)
sleep(300)
line = s.readline()
int(line)
MyServo.moveToDegree(line)
print(MyServoX.getCurrentDegree()) 


#2

Hello masteruan,

Let me reformat your code:

from drivers.servo import servo 
import streams
s=streams.serial()
MyServo=servo.Servo(D11.PWM,400,2400,500,3000)
while True:
sleep(200)
print(“Servo ON”)
MyServo.attach()
sleep(3000)
print(“insert degree:”)
MyServo.moveToDegree(0)
sleep(300)
MyServo.moveToDegree(180)
sleep(300)
line = s.readline()
int(line)
MyServo.moveToDegree(line)
print(MyServo.getCurrentDegree()) 
<br><br>The error is at MyServo.MoveToDegree(line) because line is a string and a TypeError exception is raised.<br><br>You can fix it by replacing line with int(line), like this:<br><br><pre class=" CodeBlock">from drivers.servo import servo 
import streams<br>
s=streams.serial()<br>MyServo=servo.Servo(D11.PWM,400,2400,500,3000)<br>while True:<br>    sleep(200)<br>    print("Servo ON")<br>    MyServo.attach()<br>    sleep(3000)<br>    print("insert degree:")<br>    MyServo.moveToDegree(0)<br>    sleep(300)<br>    MyServo.moveToDegree(180)<br>    sleep(300)<br>    line = s.readline()<br>    MyServo.moveToDegree(int(line))<br>    print(MyServo.getCurrentDegree())

You can also try this one, that prints the raised exception, pointing you to the line with errors:


from drivers.servo import servo 
import streams
s=streams.serial()
MyServo=servo.Servo(D11.PWM,400,2400,500,3000)
while True:
try:
sleep(200)
print(“Servo ON”)
MyServo.attach()
sleep(3000)
print(“insert degree:”)
MyServo.moveToDegree(0)
sleep(300)
MyServo.moveToDegree(180)
sleep(300)
line = s.readline()
int(line)
MyServo.moveToDegree(line)
print(MyServo.getCurrentDegree()) 
except Exception as e:
print(e)
<p><br></p>

#3

Hi Giacomo,
I’ve used after 15 minute of the post the same code like this:

int(line)
<div>But the code doesn't work.</div><div>I've tried this:</div><pre class="CodeBlock">MyServo.moveToDegree(int(line))

This work!

This is my complete code:

from drivers.servo import servo
import streams
s = streams.serial()
MyServoX=servo.Servo(D11.PWM,400,2400,500,3000)
MyServoY=servo.Servo(D10.PWM,400,2400,500,3000)
sleep(200)
print("All servo ON")
MyServoX.attach()
MyServoY.attach()
sleep(2000)
MyServoX.moveToDegree(0)
sleep(300)
MyServoX.moveToDegree(180)
sleep(300)
MyServoY.moveToDegree(0)
sleep(300)
MyServoY.moveToDegree(180)
while True:
    print("insert X:")
    line = s.readline()
    if line == 0:
        MyServoX.detach()
        print("Servo X OFF")
    MyServoX.moveToDegree (int(line))
    print("You wrote:", line)
    print("The servoX is", MyServoX.getCurrentDegree())
    print("insert Y:")
    line = s.readline()
    if line == 0:
        MyServoY.detach()
        print("Servo Y OFF")
    MyServoY.moveToDegree (int(line))
    print("You wrote:", line)
    print("The servoY is", MyServoX.getCurrentDegree()) ```

This work! Now I print the base and the connections of two servo, and publish the tutorial on the Hackster.io and Instructables.

Tank's Giacomo