Jump to content


Photo

Simulate sunrise / sunset using Arduino


  • Please log in to reply
4 replies to this topic

#1
quietstorm

quietstorm

    Nano Reefer

  • Members
  • 59 posts
  • Joined 26 Oct 2009

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 !

#2
nfh63

nfh63

    Nano Reefer

  • Members
  • 3 posts
  • Joined 13 Oct 2009
  • Imatra, Finland.
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 sections

byte 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 writing

byte 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

#3
jm82792

jm82792

    Nano Reefer

  • Members
  • 2,564 posts
  • Joined 10 Mar 2008
  • Hamakua Cost,Hawaii (Big Island)
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...

Posted Image

Well you'll always have opportunities to pay bills,
but your window for a vortech GB comes around only occasionally


My Feedback
http://www.nano-reef...amp;hl=feedback


My 29 Gallon Reef


And........
My Accelerated College Education Website
Get that college credit cheap and quick!
I've earned 59 credits for under $3,000 with college testing.

#4
quietstorm

quietstorm

    Nano Reefer

  • Members
  • 59 posts
  • Joined 26 Oct 2009
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 !

#5
Rufessor

Rufessor

    Nano Reefer

  • Members
  • 107 posts
  • Joined 12 Jan 2011
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.c...o/timelord.html

Hope that helps. ITS easy for sure as all the work is done for you.