Maths Guru ? or of course, a simpler approach in the first place !

For ‘the challenge’ , I set out to control multiple ‘devices’ via Shift Registers.
I wanted to see if I could control each device with PWM throught the SRs.
And yes, I could :slight_smile:

However, my approach does have limitations.
I have an array that holds a ‘power’ value each device.
Then …

loop() {
if(power_value_of_the_bit < counter){turn bit off}
counter++
if (counter>limit){counter=0}
}

…and indeed, this works well so far with power levels 0-50, 2SRs & 8 ‘devices’.

Problem is, for a power level of 25 out of 50, I am actually sending 25 ONs, followed by 25 OFFs.
That delay (25 continuous OFFs), is now limiting how expandable the system will go.
e.g. if I want 100 power levels, or control other devices that require/benefit from more constant signals.
The ideal situation would to alternate, ON then OFF, 25 times.

I thought I had this sorted, until I did the maths.
(I’ll use power values up to 100 now to make reading easier)
Power 80, would mean turning the bit off on iteration every 5. [100/(100-80)=5]
And that is very doable by looking at when the iteration counter is exaclty divisible by 5.
But then when we look at a power of 60, I need to turn bits off every 2.5 iterations !

Well I sat with Excel for hours, trying to work out the maths of how to add in the adjustments … then smoke started coming out of my ears and setting the smoke alarms off!
I MUST be missing something obvious here ?
Or am I asking too much of this type of setup ?

I’d be grateful of any maths insight, or strategy hints, that might help me get a little futher, or indeed - - tell me ‘no’ !

Many Thanks
:slight_smile:

Notes:

Happy to post actual code if required, but atm I am seeing this as more of an ‘academic’ question than literal.
‘Devices’ : yes atm they are LEDs, but it is the technique of mastering Shift Registers/PWM that is the real focus here. (LEDs are just easy to use for testing.)

simonB2013:
Problem is, for a power level of 25 out of 50, I am actually sending 25 ONs, followed by 25 OFFs.
That delay (25 continuous OFFs), is now limiting how expandable the system will go.
e.g. if I want 100 power levels, or control other devices that require/benefit from more constant signals.
The ideal situation would to alternate, ON then OFF, 25 times.

This is the same kind of problem as drawing a straight line from (0,0) to (50,25) across a grid. The solution to that problem is the Bresenham algorithm; every time the line would increment up the Y axis you would turn the signal on, and every time you don’t increment you would turn the signal off.

When you figure it out the code will be just ~10 lines. I can provide an example but I’m certain you won’t understand it without first attempting to work it out yourself.

Chagrin,
I cannot thank you enough.
Pointing me in the right direction is the most perfect answer, and so much better than just handing me the answer on a plate.
At least I will finish this with some new learning and some self respect now :wink:

Funny, now you've opened this door to me, this reminds me of 'ray casting' which is something long lost in the midsts of my ever depleating memory.
I shall dig into this with excitement !!

Again, thank you :slight_smile:

Very interesting, thanks for the Wikipedia post Chagrin. Years ago I was wondering how a pen plotter stepped in two directions to "spread out" the slope.... Now I know.

Thanks to chagrin, i can point you to my musings on the topic.

http://forum.arduino.cc/index.php?topic=167357.0