Hi Guys,
I'm in the process of working out some logic for a project which uses PWM to control some power LEDs. My current version which was adapted from a friends basic code works OK when programmed, but has a few bugs, so I'm starting with a clean sheet and aim for something more flexible.
I'm using a DS1307 and have the hours and minutes converted from BDC to give two variables TimeH and TimeM. I then want to set five user programmable timing points where the programmable brightness of the LEDs can be set, and the code either ramps up or down to these values as it passes the points. For example, assuming the first point is set to 14:00, and the second is 14:30. The value for the first point is 0 and 128 for the second. So when the time equals point 1 the LEDs will fade up to a 128 (or 50:50) duty cycle by the time it reaches the second set point time. If the third point is set to 18:00 say, and a value of 254, then the LEDs would continue to increase from 128 to 254 over the course of three and a half hours. The next point could be say 20:00 and a value of 64 so the LEDs dim from 254 to 64 over two hours, and finally the last point at 21:00 means the LEDs dim from 64 to 0 over the last hour.
I was proposing to do an if point1 hour = TimeH and point1 min = TimeM then case=1 type statement, and then in the case section do some form of calculation to give the seconds between the point 1 time and point 2 time and then divide this by the value for the second point to give the duration between increments for the PWM. Whilst this is OK for the first section where the PWM is increasing, it raises an issue for subsequent sections as the duty cycle could decrease or increase (say if we wanted to go to full brightness between point 1 and point 2 and then slowly fade down to point 3 with a rapid fade down between 4 and 5). This is problem 1
The second problem is that I need some form of time check so in the event of a power cut, the unit can work out where in the cycle it should be when the power returns, and fade the LEDs up to the corresponding value if power is restored at a time when the LEDs should be on. For example if the power came back on at 14:15 the code would work out its midway between point 1 and 2 and then ramp up the LEDs to a value of 128 over 15 minutes, so it then matches the programmed value at the programmed time. If the power cut was in the middle of the night then it would know that let value would be zero and thus the lights would stay off.
My existing project simply allows you to set an on time, fade in duration in hours and minutes to reach the set brightness, then an off time and fade out duration, but has no power cut option and sometimes if changes to the values are made from the push buttons it falls over and sometimes starts turning then LEDs back on after the off time has passed, or the brightness value gets corrupted and the LEDs don't reach the desired setting. Flash the chip again with the presets and it works fine !
The PIC is an 18F4520, and will use the hardware PWM pins for the LEDs.
Any suggestions or assistance would be welcome.
Bookmarks