How to program a sine wave motion?

Hi,

I am having a hard time to write a code to tell two servo motors to follow a sinusoidal motion. Right now, I can tell my motors can run on a triangle command but that's not really what I want. Does anybody knows how to program this?

Thanks!

Something like:

void loop ()
{
  float theta = millis () * 0.001 ;
  servo.write (90 + 70 * sin (theta)) ;
}

Thanks Mark!

Do you how can I control both the frequency and amplitude of the sine wave? I understand the "70" in your code is the amplitude. But how about frequency? like I wanted to make it a 0.25 Hz. Sorry if it sounds silly, I am very new to programming.

Thanks!

You would change the 0.001 number in the above example.

In the example, you are changing the "angle" theta by 1/1000 of a radian per millisecond, which is one radian per second, which is about 6.28 seconds for the angle to change by 2*pi, a complete sine wave cycle.

gazebotaco:
Thanks Mark!

Do you how can I control both the frequency and amplitude of the sine wave? I understand the "70" in your code is the amplitude. But how about frequency? like I wanted to make it a 0.25 Hz. Sorry if it sounds silly, I am very new to programming.

Thanks!

Maths: y = A sin (w t)
Code: y = A * sin (w*millis ()/1000.0)
where w is angular frequency.

Keep learning! The simple method I gave is only a start, I'd never program something that inefficient on
a microcontroller (no hardware floating point, so I'd use some sort of table look up or CORDIC) - for now
you can ignore such issues.

In the above comment, w (the angular frequency in radians/second) is the frequency f (in Hz) multiplied by 2 pi.

w = 6.28*f; //approximately