PDA

View Full Version : Generating Timed Fades in PBP - ideas ?



bcd
- 9th August 2008, 09:38
Hi All,

Just want to bounce some ideas of everyone for a project I am working on at the moment.

I have an application that controls 6 channels of dimming over RS232, but what I am struggling with is the ability to fade a channel to a know value over a certain time.

As an example lets say I want to fade from level 0 to level 255 over 10 seconds. This means every second the value has to change by 25.5.

What I was thinking of doing was using DT Interrupts to generate an interrupt every 0.1 second where we could do some maths to increment the value as required. Using the values from the example above I would have to increment by 2.55 values every interrupt - 2.55 * 10 (25.5 per second) -> over 10s gives us 255.

One option I though of was to multiply the value by 10 so 255 becomes 2550 (defined as word rather than byte) which would allow me to have 1 decimal place precision. I could then check if the lowest value > 5 and if so increment. I might need to set a bit to mark if we did a 'roundup' step so the count doesn't get too far out too quickly

So steps would be: (over 100 time periods - 2.55 steps per period (becomes 25.5 steps as we x10)
step1 - increment by 25 - 254 steps remaining - output is 0025 (rounds to 3)
step2 - increment by 25 - 253 steps remaining - output is 0050 (rounds to 5)
step3 - increment by 25 - 252 steps remaining - output is 0075 (rounds to 8)
step4 - increment by 25 - 251 steps remaining - output is 0100 (rounds to 10)
...-...
step 253 - increments by 25 - 1 step remaining - output is 2525 (rounds to 253)
step 254 - increments by 25 - 0 step remaining - output is 2550 (rounds to 255)

This gives a running average of 2.5 steps per time
There is a bit of maths that I think I might need to do some Long string manipulation to make things work, but does it sound like I am close with this idea??

We Know the Start Value, Finish Value and required Fade Time.
these give me number of steps to do each interrupt. I just need to work out how to do decrement or increment based on the remaining number of steps and the if it is less than 1 we miss a step, or if a whole number we inc / dec by that much, but a value that has a value to the right of the decimal place then we hold off unless it is greater than 0.5.

Once we reach the end of the fade time we might need to do some bumping to the final level to make up for any rounding errors.

I seems to work OK for values I try, but I might need to try with some weird values to see if it still holds up - 33 - 101 over 5 seconds ??
(68 steps over 5 seconds or 50 interrupts = change by 1.36 per interrupt !! aaarg !)

Any thoughts anyone ?

bill.

skimask
- 9th August 2008, 21:52
I seems to work OK for values I try, but I might need to try with some weird values to see if it still holds up - 33 - 101 over 5 seconds ??
(68 steps over 5 seconds or 50 interrupts = change by 1.36 per interrupt !! aaarg !)

( Start value - End value ) / ( time * 10 )
( 33 - 101 ) = use the ABS function to get the real difference between the 2 = 68
( 5 * 10 ) = 50
68 / 50 = 1.36
True, not doable in PBP...but...a bit of scaling will take care of that...

68 * 100 = 6800 / 50 = 136

Keep track of your steps/interrupts...
1st step = 6800 / 50 * step (1 in this case) = 136 / 100 = 1.36, rounds down to 1, add to start = 34
2nd step = 6800 / 50 * 2 = 272 / 100 = 2.72, rounds down to 2, add to start = 35
3rd step = 6800 / 50 * 3 = 408 / 100 = 4.08, rounds down to 4, add to start = 36
...blah blah blah...
49th step = 6800 / 50 * 49 = 6664 / 100 = 66.64, rounds down to 66, add to the start value = 99.
50th step = 6800 / 50 * 50 = 6800 / 100 = 68, add to start value = 101

bcd
- 10th August 2008, 00:18
cool thanks for that - today I am going to try a bit of coding to see how it runs.

bill