Go Down

Topic: running servos on a sine wave (Read 640 times) previous topic - next topic

jjrabie

hey there,

im trying to run a servo continuously between 0 and 180 degrees but on a sine wave....(i.e. quicker in the middle at 90 degrees, and slower at each end)

can anyone help me please?

Grumpy_Mike

The speed of a servo is not something you have control over, it is down to the servo electronics.
All you can do is to feed the servo with small steps with a delay in between.

guix

Hello :)

And you could calculate that delay with something like this:

Code: [Select]

delay_ms = 3 / sin( angle )


So with this example, when the angle is 90 the delay will be 3 ms until the next servo move (91 or 89) and with for example an angle of 179, the delay will be ~172 ms. I suggest you use microseconds for more accuracy.

(Be carefully with angles 0 or 180, it will divide by 0 and your Arduino may explode :D)

Oh and don't use delay() or delayMicroseconds() functions, instead look at the "Blink without delay" example to see how to do delays without those blocking functions.

PeterH


im trying to run a servo continuously between 0 and 180 degrees but on a sine wave....(i.e. quicker in the middle at 90 degrees, and slower at each end)


The most obvious approach would be to use millis() to read the current time, scale it to give the frequency you want, apply the % (modulus) operator to convert to the range 0 - 359, calculate the sin of that, use map() to convert the result to the range 0 - 179 or whatever range of angles you wanted your servo to cover, and write that to the servo. Although it sounds a lot, each step is simple and the whole thing is only one or two lines of code when you get down to it. Use the technique demonstrated in the 'Blink without delay' example sketch to execute this code at regular intervals.
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat

Quote
can anyone help me please?


Have you looked at the servo "sweep" example code in the IDE code examples?
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Go Up