How to actually convert float to int?
quick question convert int to float
Int to float convertion[Solved]
How to change int to float??
LED_CYCLE_TIME is zero because you don't use floating point division ((1 / F_LEDS) / 2 is (1 / 5) / 2, but 1 / 5 == 0, so it's 0).
Just use doubles: #define LED_CYCLE_TIME (0.5 / F_LEDS)
And you also need some extra brackets around your defines, since currently OVERFLOWS_PER_CYCLE is defined as (unsigned int)((1 / F_LEDS) / 2 / 1 / F_CPU / 256.0), which does the division left-to-right instead of in the correct order.
So your corrected code:
#define OF_FREQUENCY (F_CPU / 256.0)
#define SECONDS_PER_OF (1 / OF_FREQUENCY)
#define F_LEDS 5
#define LED_CYCLE_TIME (0.5 / F_LEDS)
#define OVERFLOWS_PER_CYCLE (unsigned int)(LED_CYCLE_TIME / SECONDS_PER_OF)
The arithmetic can be considerably simpler and can be performed without the need for floating point.
Consider that LED_CYCLE_TIME is:
1 / (2 * F_LEDS)
and that you are dividing that by SECONDS_PER_OF. But that is the same as multiplying by the reciprocal of SECONDS_PER_OF, and you already calculated that as OF_FREQUENCY. So now you have:
1 / (2 * F_LEDS) * OF_FREQUENCY
which is the same as:
OF_FREQUENCY / (2 * F_LEDS)
So what you end up with in code is:
#define OF_FREQUENCY (F_CPU / 256)
#define F_LEDS 5
#define OVERFLOWS_PER_CYCLE (OF_FREQUENCY / (2 * F_LEDS))
Or better:
#define LED_PRESCALER 256
#define F_LEDS 5
#define OVERFLOWS_PER_CYCLE (F_CPU / (LED_PRESCALER * 2 * F_LEDS))
Which given F_CPU == 16000000 is 6250 and requires no floating point.