Go Down

### Topic: running servos on a sine wave (Read 1 time)previous topic - next topic

#### jjrabie

##### May 04, 2013, 05:05 am
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

#1
##### May 04, 2013, 09:24 am
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

#2
##### May 04, 2013, 02:10 pm
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 )

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

#3
##### May 04, 2013, 03:07 pm

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.

#### zoomkat

#4
##### May 04, 2013, 04:46 pm
Quote
can anyone help me please?

Have you looked at the servo "sweep" example code in the IDE code examples?
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up