PWM output values

Trying to put this question in it most basic form, can you send a float number to a PWM output? I'll basically have it connected to a driver to turn on LEDs, and I have a programme that adds something like 0.3 to a variable which I aim to send to the PWM output. Will...

A. The PWM output not do anything
B. The PWM output only step by 1 every time the decimal gets to 1 or above, and then 2 and so forth
C. Actually give out 1.3, for example.
D. Something else?

I haven't got the lights set up yet so trying to work it out theoretically. Will I need to 'round' the numbers up or down and then send to the PWM pin? Or will one of the above happen?

Cheers. Steve.

PWM is 0-255

How about multiplying by 10 to get your value

001 ---> .1
030 ---> 3.0
185 --->18.5
...
255 --->25.5

So if you had 13.5
13.5 X 10 =135

analogWrite(3,135)

analogWrite takes an int as the second argument. So if you give it a float it will be truncated to the next lower integer.

Thanks for your replies. Larry the problem I have is that I want it to go through the decimels to slow the fade down. I have a start fade time, a duration of fade, and start and finish brightness value. So what I have done is different calculations to eventually give me a value to fade each second for the duration of the fade time. So if it is only increasing by say 0.33 per second, it'll prolong each full increment by 3 seconds each time.

Delta so have understood you correctly that if I send it a float, it will only output the rounded down closest value? For example sending it 100.35 will send 100 out of the pin, and 57.77 will send 57 and only flick over to 58 once the float value is above it?

PWM:

You understood me correctly.

What might make more sense would be to calculate the amount of time needed between two integer values and use that time between PWM changes instead of trying to stick with a time interval that doesn't work.

I agree and I did think of that delta, but I have four channels of lighting and each would be at different values so it would mean that they all adjust independently and not together. That's why I have decided to go with what I have.

That should be fine though. I'll continue with the build and if need be, I'll write a rounding up/down script in.

Thanks. :slight_smile:

last pwm output I coded I added a millis timer then used the timer to add one or minus one to the pwm output. Add a simple larger/smaller argument to cap the output number (max-min). add a little code to control the interval of the timer or use a preset like 3000 for 3 seconds