PWM as float instead integer

Hello

I'm finishing my code to control some leds with PWM...
It happens that my calculations are float... (Eg: 244.1, 244.2 ... 244.9)
Between 244.1 and 244.9 the light behaviour should have some minutes... With PWM as integer, pass through directly from 244 to 245 don't let me satisfied...

analogWrite... analogWrite(uint8_t pin, int val)

What do you suggest... beside that my values (calculations) are float...

Thanks on advance
Best regards
Pedro Ferrer

Between 244.1 and 244.9 the light behaviour should have some minutes

Are you saying you should be able to see the difference between a PWM value of 244.1 and 244.8?

No, in fact I'd be surprised if you could see a difference between 244 and 255.

You could try mapping the range you are interested in to a wider one, e.g. if you only ever get values of say 240 to 245, then make 240 PWM 0, and make 245 PWM 255.

Hello AWOL

In fact, I don't know if between 254-255 exist too much light difference.. but on my algorithm the gap between them is about some seconds...minutes...
Don't have funny transform float behaviours to integer behaviours...

Thanks on advance
Best regards
Pedro Ferrer

I think I sense some more fundamental misunderstanding - possibly on my side - why are you talking about "minutes"?

Hello

Msquare:
I think I sense some more fundamental misunderstanding - possibly on my side - why are you talking about "minutes"?

Because the calculation works like that... that's why... :slight_smile:

Best regards
Pedro Ferrer

Because the calculation works like that...

You may need to make some version of map() to work in your application. Create a function that you can pass your (float) to and get back an appropriate (int) for the PWM.

Or, modify your calculations to yield usable integers.

float value;
const float MinimumValue = 244.1;
const float MaximumValue = 244.9;
...

//  Map the range of analog values into the integer range 0-255
unsigned int pwm = ((value - MinimumValue) / (MaximumValue-MinimumValue)) * 255U;
analogWrite(LED_pin, pwm);

Good morning Johnwasser

const float MinimumValue = 244.1;
const float MaximumValue = 244.9;

Thanks by your help!

So, I think that my MinimumValue will be '0' and MaximumValue will be '255'.
float value will have a range between 0-255 (0, 0.1, 0.2, 0.3 ...)

Probably doing Int(244.9) have that same behaviour doing map?...

Please let me know
Thanks on advance
Pedro Ferrer

So, I think that my MinimumValue will be '0' and MaximumValue will be '255'.
float value will have a range between 0-255 (0, 0.1, 0.2, 0.3 ...)

Still don't see where the "minutes" come in.
Is it "minutes" as in "hours, minutes, seconds", or "minute" is in "very tiny" (my-newt)?

Are you saying then, that instead of 8 bit PWM (0..255), you need something more like 12 bit PWM (0..2559)?

Good morning

Minutes came from some delays that I have on purpose...

AWOL:

Are you saying then, that instead of 8 bit PWM (0..255), you need something more like 12 bit PWM?

Perhaps... I don't know! Is possible that!?

Thanks on advance
Best regards
Pedro Ferrer

Perhaps...

I don't really do "perhaps".

http://www.arduino.cc/playground/Learning/TLC5940