# Resolution of sin() function

Hi,
I want to control some LED by a sinus signal and was trying around a bit.

Checking the sin() function over the serial plotter I was wondering why the signal has just 5 or 6 steps and the resolution was that poor.

Code as followin:

``````void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.println(sin(millis()/150));
}
``````

I don't understand how you would determine that from this code.

Why are you using millis as the input to the sin function? If you're going to do that then at least print millis so you know what numbers you fed to it. It would make a LOT more sense just to write a for loop to feed it numbers.

Remember to print more than 2 decimal places of the result.

norpen:
I want to control some LED by a sinus signal

Why? That only makes our (eyes) non-linear perception of brightness worst.

What do you mean by " signal has just 5 or 6 steps"?

``````void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.println(sin(millis()/150));
}
``````

Let's see.... First read millis(), which is a monotonically increasing integer value (well, more often than not) - 1, 2, 3, 4, 5, 6....

Divide that by 150, producing another integer value

Pass that integer value to sin(), which takes a single argument, which is an angle expressed in radians - 2*PI radians == 360 degrees.

So...

For millis() anywhere from 0 to 149, this reduces to sin(0), which == 0
For millis() anywhere from 150 to 299, this reduces to sin(1), which == ~0.84
For millis() anywhere from 300 to 449, this reduces to sin(2), which == ~0.91
For millis() anywhere from 300 to 449, this reduces to sin(3), which == ~0.14
For millis() anywhere from 300 to 449, this reduces to sin(4), which == ~-0.76
....

Look familiar?

It really helps a lot to read the documentation for a function, and look at some examples, so you understand HOW it works, before using it.

Regards,
Ray L.

thanks a lot for your help

If you do the division in floating point you get a much smoother curve:

``````  Serial.println(sin(millis() / 150.0));
``````

If you do the division in floating point you get a much smoother curve:

True, but the curve depends in a nonobvious way on the Baud (sampling) rate. This program

``````void setup() {
Serial.begin(115200);
for (int i = 1; i < 300; i++) {
Serial.println(sin(millis() / 150.0));
}
}
void loop() {}
``````

Produces this curve: