Jump to content
quietstorm

Simulate sunrise / sunset using Arduino

Recommended Posts

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 !

Share this post


Link to post
nfh63   

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

Share this post


Link to post
jm82792   

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...

Share this post


Link to post

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 !

Share this post


Link to post
Rufessor   

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.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recommended Discussions

  • Sign Up or Sign In to hide this.

×