Asked — Edited

Servo Rotation

Does anyone have any code they could share that would turn a servo from side to side. I need it to move the servo from position 50 to position 80 and back again not exceeding those positions but I cant seem to figure out the logic to make it happen. Any examples?


Upgrade to ARC Pro

Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!



servo(D0,50) #set servo to position 50
servo(D0,80) #move servo to position 80
servo(D0,50) #move servo back to original position 50


The important thing in tjat script is the sleep steps. servo positions are absolute, so if you tell it to go to a position it will. However, servos don't give feedback back to ARC to say when they have reached the position. Therefore, as soon as ARC gives the command, it assumes it is complete and moves on to the next step. Without the sleep steps, it will head back to position 50 before ever reaching position 80.

You can enhance this is with the addition of a servospeed command if you want to slow it down (but will need to use a longer sleep).

If you want it to repeat you can use a label and goto, or if you want it to repeat until some condition is met you could use repeat until or repeat while commands.



Ok, what I mean is I want it to slew back and forth not exceeding the position. I can move the servo. I'm trying to set the code up to check the position of the servo, the slew to the side it is further away slowly and then back again. If I use those commands, it very fast and abrubt. something more smooth. I was trying to use a counter and add 1 to the counter to move it that way. But I couldn't get the logic down.


So then use servospeed(D0,2) command in conjunction with sleep commands to slow things down... Picking the right servo speed and the right sleep commands should get you what you want...


I am not at a place I can check the code. So are you suggesting something like this:

Servo(d1,50) sleep (600) Servo(d1,60) sleep (600) servo(d1,70) sleep (600) servo(d1,80)


More like this... This will execute much smoother and slower (sweeping back and forth like radar)

servospeed(D1,2) #may need adjusting slows the servo down
sleep(2000) #may need adjusting
sleep(2000) #may need adjusting


Ok, will give it a try tonight. Movement like Radar is what I am looking for not the jerky movement I've experienced. Thanks


You may need to adjust the sleep commands to maybe sleep(1000) and if it is too slow speed it up using servospeed(D1,1) instead ofservospeed(D1,2)... FYI servospeed(D1,3), servospeed(D1,4) etc.. would be even slower...


Something like this may give you some help too. I use these two scripts attached to joystick buttons to open and close a claw as long as I am pressing the button. You could adapt the function to your needs but not exceed the min or max values.

RepeatWhile(GetServo(D23) < 90)
  ServoUp(D23, 2)

RepeatWhile(GetServo(D23) > 34)
  ServoDown(D23, 4)



I would like to thank everybody. The example code and explanation was just what I needed. What I've noticed is that by running the code this way I have to way for the slewing to complete. Is there a way to do two things at once? Meaning I would like it to go up/down and side/to side, like tracing a W. Any thoughts or suggestions?


Do you mean two servos like a pan/tilt?

If you use something like my last example where you are moving a little at a time in a loop, you would just issue two commands, one to each servo before looping. The commands will execute so fast that they will be simultaneous for all intents and purposes.


United Kingdom

Another way you could do it using the example in post #7, is to add another servo command before the sleep() (anything before the sleep will run at the same time).

#Assuming you have servos connected to ports D1 and D2. 

#initialize servo speeds

#start your repeating servo commands
Servo(D2, #add your MAX servo position here)
Servo(D2, #add your MIN servo position here)

Obviously, change where you want your max positions to go, and as already mentioned, servo speeds, and length of sleeps where you need to.