Pages: [1]   Go Down
Author Topic: running servos on a sine wave  (Read 562 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33362
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 986
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

And you could calculate that delay with something like this:

Code:
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 smiley-grin)

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can anyone help me please?

Have you looked at the servo "sweep" example code in the IDE code examples?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: