quietstorm Posted January 19, 2010 Share Posted January 19, 2010 Hi everyone, I'd like to simulate sunrise and sunset with my LED lighting system using an Arduino but I've had problems with my code. I use a RTC module for time also. I've used the code available on arduino.cc for PWM and it works well with my led array. The problem is that when the light is dimming, the time that displays on my LCD screen doesn't increment correctly, it just stops. I think the issue comes from the fact that I use delay() for dimming and that it affects the whole programm. Has anyone a code suggestion to have a smooth sunrise / sunset without affecting the rest of the programm ? Thanks in advance for your help ! Link to comment
nfh63 Posted January 19, 2010 Share Posted January 19, 2010 Here is one piece of code that relies to new Time library. I wrote this from memory as I'm not at home now. So check carefully if I have made mistakes. I made this quite simple, only white leds. #include <DS1307RTC.h>#include <Wire.h>#include <Time.h>// Next comes variables for white leds, you have to do the same for blues too.byte pwmWhite = 0;// Next idea from Dave_uk, thanks (http://www.nano-reef.com/forums/index.php?showtopic=206246)// Array of pwm values for 15 minute sectionsbyte whiteLeds[96] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-03 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00-06 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 06-09 20, 30, 40, 50, 60, 80, 100, 120, 140, 160, 180, 200, // 09-12 220, 240, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 12-15 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 15-18 240, 220, 200, 180, 160, 140, 120, 100, 80, 80, 80, 60, // 18-21 60, 40, 20, 20, 0, 0, 0, 0, 0, 0, 0, 0 // 21-24}; byte whiteMaxCurrent = 70; // Percentage of, 70 is 70%, so 1000mA puckbuck is driven at 700mA// Next the pin for writingbyte whitePin = 11;void setup() {// Setup stuff, LCD, pins, RTC etc.}void loop() { // Main loop getWhitePwm(); analogWrite(whitePin, pwmWhite);}void getWhitePwm() { int tempIndex = 0; // Index for the array containing dimming values for the leds, calculated in 15 mins sectors long tempTime = 0; // Used to store number of seconds since last midnight pwmWhite = 0; // Public variable. Reset the value (just to be sure) tempTime = elapsedSecsToday(now()); // From Time.h, seconds since last midnight, now() is current time // Check out what is the index of 15 min sections tempIndex = (tempTime / (SECS_PER_HOUR / 4)); // macro SECS_PER_HOUR from Time.h // Calculate the PWM value according the index and maximum power we have defined. pwmWhite = ( whiteLeds[tempIndex] * (whiteMaxCurrent) /100 ); } Hope this helps. Jouko Link to comment
jm82792 Posted January 19, 2010 Share Posted January 19, 2010 There is a way to connect one of those wireless atomic clock modules to arduino, then somehow(supposedly easily) you can choose what time zone and such. Using that would be a great idea, I'd use it but it won't work out here... Link to comment
quietstorm Posted January 21, 2010 Author Share Posted January 21, 2010 nfh63, thanx for the code ! I'm going to try to combine that to Dave_UK's code to see if it works. I'll tell you then. If anyone else has other suggestions, please share ! Link to comment
Rufessor Posted January 23, 2012 Share Posted January 23, 2012 There is a complete library .cpp file that is precoded to do exactly what you want. I myself just found this today and happened upon your post searching for more information so I don't know if its god but it looks about right.... http://www.swfltek.com/arduino/timelord.html Hope that helps. ITS easy for sure as all the work is done for you. Link to comment
Recommended Posts
Archived
This topic is now archived and is closed to further replies.