Go Down

Topic: Advice on changing pwm duty cycle over time using RTC (Read 891 times) previous topic - next topic


Been lurking a long time, and have learned a lot from these forums, but now I'm in need of a little assistance. I just can't seem to wrap my head around a way to approach this issue.

So, I'm trying my hand at building a controller for an LED aquarium light. It's a 4 channel setup that uses 4 pwm outputs to drive 4 LED drivers. Each channel can be set independantly from each other, and need to be turned on and off at certain points in the day. I'm using a Sparkfun DS1307 RTC breakout to deal with time. I can turn everything on and off easily enough, but I want to be able to fade from one state to another once a certain time point has been hit. Part of what is making my brain hurt is that each channel can be set to almost any value, and they all need to fade down and hit zero at the same time, and fade up and hit their set values at the same time. To clarify, think of it this way:

On time pwm values (example only):
CH1 - 150
CH2 - 225
CH3 - 75
CH4 - 25

All those channels need to start dimming down to zero (or maybe a minimum value that is non-zero) at a certain time, and hit zero at the same time. Opposite is true when it's time for them to come on.

Anyone have any good ideas on how to accomplish this? I can understand code and how to structure things well enough once I'm shown, but I haven't seen a good enough example so far to try this on my own.

Thanks in advance.


First figure out how long you want them to take to go from the "ON" value to "OFF". Then work out how many steps you want to do this in. After that it's just a problem of changing the vaules by the right amount every X seconds.



The way I'd deal with that is to define a 'percentage on' variable which is at 100% when the outputs are at their full nominal brightness and 0 when they are fully off.

Let's suppose everything is initially off and percentageOn is at 0. At the time you want to turn them on, note the start time. Periodically after that measure the elapsed time since that start time and increment percentageOn accordingly. For example you might increment it by 10 (%) per second so that it reaches 100 (%) after ten seconds. Each time you update percentageOn, calculate the PWM value for each output by multiplying that output's 'fully on' duty cycle by percentageOn / 100. Take care to do the multiplication before the division otherwise your intermediate results are going to get rounded down.

To turn them off, just carry out the same process in reverse - decrement percentageOn at regular intervals until it reaches zero.

The natural way to structure this would be to hold your 'fully on' brightness and pin numbers in an array, and define a function to set all the outputs to a specified percentageOn, then use a finite state machine to record whether the outputs are on, off, turning on or turning off and i the 'turning' states use the techniques demonstrated in Blink Without Delay to increment or decrement the percentageOn value at regular intervals. The whole thing would probably take about twenty lines of code.

Go Up