Sunrise / sunset code based on location

Do you have a question on how to do something.
Ask in here.
TanksNStuff
Posts: 188
Joined: Fri Dec 30, 2011 6:57 am

Re: Sunrise / sunset code based on location

Post by TanksNStuff »

Curt, I can try to start a new thread with all the info you requested, but I don't have time to do it right now. I'm "at work" and I'd probably need to be at home where I can review my code and stuff first.

I'd be happy to do the testing though once we get it rolling.

Whenever I start the new thread, I'll be sure to PM you with a link to it.

PS, thanks for the input and the link Lee (although I think the code in that link is outdated and may not work with current libraries?). Sorry about the minor hijack to your thread, but I thought it was related since you mentioned it a few pages back.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

roberto, is that link outdated? Do you have newer code for the clouds? I didn't compare line-by-line, but it looked like the same code you had written up for jedimasterben...
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Sunrise / sunset code based on location

Post by rimai »

That's is it.
The other one uses the dimming expansion module
Roberto.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

Had another wierd problem where I needed to change int to long. This was for the rise hour and set hour offsets. I had to change my current offset (-9) to positive (15) since it was actually setting the rise/set to midnight of the day before due to going backwards. This was causing my comparison to now() not function properly. I was seeing wierd results with the offset until I changed it to use long instead. Here's the latest update. I'm considering adding an isDay / isNight function. Let me know if you think that would be helpful :)
Attachments
SunLocation.zip
(3.94 KiB) Downloaded 413 times
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Sunrise / sunset code based on location

Post by binder »

Sweet. Thanks for the link Lee.
TanksNStuff - No rush on this. It's simply whenever you get around to doing it.

I'll look over things more and see what all I can do with it and see about working an interface for everything.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

So I had an issue today when my Sunset hit midnight and that threw off calculations for ActinicOffset and messed with my whole lighting schedule... I never really accounted for Winter being the longest time of the year and I guess with DST it ended up with night starting a bit later than when I first calculated it...

Anyway, I was playing with the coordinates we had and found out if you inverse the latitude, you end up right nearby Honolulu, HI. and then I decided to test using my latitude but keeping GBR longitude, which took me right off the coast of Chile. Next was north of the equator again, and I end up just North of the Caribbean, near Turks and Caicos. I thought that was pretty cool to have all these coordinates handy and it's good to know that all these areas have an abundance of Coral habitats.

The default GPS is Great Barrier Reef - Coordinates which is -21.08931,-147.699722
If you move that North of the equator, you get somewhere near Honolulu, HI... at 21.08931,-147.699722
Using my NY latitude and GBR longitude, you get off the coast of Chile -21.08931, -73.528383
The inverse of that brings you just south of Turks and Caicos 21.08931, 73.528383

For now, I'm using the Chile times so keep my offset low and don't have to worry about that... using a -1 offset right now to get the time I want. Later, in the spring, I may switch to the Turks and Caicos time or somewhere nearby so that the day length cycle matches up with our Winter/Summer schedule.

All in all, still happy with the day length cycle at these latitudes as the deviations aren't that high between the seasons. We'll see how it progresses.
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

I am confused how did you end up with daylight past midnight?

EDIT: Oh an by the way, I am incorporating this into the climate library I am working on. It will include moon, clouds and sun
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

Because of the offset...it pushed the daytime hours past midnight.
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

What does the offset do? Is it suppose to handle timezones /daylight savings time?
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

Ok sunrise in boise Idaho is 8am which locally is 10am but i want my lights on at noon...thats what the offset is for.
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

And just checking are you using dev libraries 1.0.5 or greater? reason I ask is, there should be improved slope and parabol methods that handle crossing midnight.

So when I calculate the sun rise, which time zone is it calculating it for. When I first tried it. it had sunrise is 12:50 something... Is it calculating for GMT?
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

It's tricky. What timeline is your gps. Its screwed because it calculates the time based on gmt. Even though you probably aren't. So you need to offset it to your local time plus your preference. That why I'm currently set for Bahamas directly south from me and same latitude as hawaii which is the opposite lat from gbr. The benefit being in the northern hemisphere is that the seasons and length of day follow our schedule, otherwise you'll habe the longest day in winter and shortest in summer.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

thekameleon wrote:And just checking are you using dev libraries 1.0.5 or greater? reason I ask is, there should be improved slope and parabol methods that handle crossing midnight.

So when I calculate the sun rise, which time zone is it calculating it for. When I first tried it. it had sunrise is 12:50 something... Is it calculating for GMT?
At the time I wasn't... but I'm now past the current (using dev versions...) However my lights are standard on/off so the slope and parabola's weren't much use to me :)

I have the current class if UseMemory=true; updates memory variables for on/off times for the standard light routines.
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

Quick question then. if you are writing to the EEPROM daily... How many writes can it handle? I get why you do it and it makes total sense. If it's in the 10's of thousands it's not a big deal, but if it is only in the thousands that might be an issue in 3 to 4 years.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

I believe roberto said it was in hundred of thousands.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Sunrise / sunset code based on location

Post by rimai »

http://www.atmel.com/Images/2549S.pdf
– Write/Erase Cycles:10,000 Flash/100,000 EEPROM
– Data retention: 20 years at 85°C/ 100 years at 25°C
Roberto.
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

good to know we are set for 300 or so years. :)
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

So what is the margin of error with the code? I have seen what looks to be differences by over 40 minutes compared with the actual sunrise/sunset times.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

Thats about right from what I've seen. I ran a sim a while and came up with an offset that made it closer but for the most part its been as accurate as I need and the schedule is very gradual over the seasons.

I do recommend staying near the equator to minimize the winter/summer differences.

Please feel free to improve on any of the algorithms if you can figure it out :)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

FWIW i don't think any of these gps coordinate codes are super accurate and i think its because of the arduino floating point precision, but who knows..
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

Fun times... Well Let me take a look how NOAA does it... I am sure it is similar but maybe if I play around with moving the decimal point... I can figure something out... Do you know the floating point precision limitations or a link to them?
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

Truthfully i think its good enough. I would think time would better be spent making it an easy package to add to the libraries and all the other stuff. You could always fine tune the times with the offsets...
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

Agreed. If accuracy is not important, do you think it might be just easier to just re-write this as a simple sin wave for seasonal changes of sunrise and sunset. Provide a parameter that can adjust the amplitude and another as the start date of the wave? i.e. the crest of the wave could be on 12/21 and the trough on 6/21.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

I just double checked my rise/ set which is coming up as 9:10-11:10 currently and compared to Bahamas rise / set.. Bahamas is:

Jun 8, 2013 6:19 AM 7:58 PM 13h 39m 20s + 24s 1:08 PM 87.8° 94.362

So I'm 10 minutes each direction 20 minutes total...I personally like being tied to gps coordinates event though its a bit wacky with the way the GMT time is in the
RA. If I had to do it over again a sine wave would be fine...a similar function would work for moon, but I'm not so sure on that implementation..l
thekameleon
Posts: 137
Joined: Sat Feb 16, 2013 7:44 am

Re: Sunrise / sunset code based on location

Post by thekameleon »

I took a look an NOAA's website and I might try converting it to javascript. My thinking was to create it as a Sin Wav and then have templates for the caribeean, barrier reef, Hawaii, etc... While not 100% accurate, it would be simple for the average reefer to implement.
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: Sunrise / sunset code based on location

Post by clw143 »

Please look at my code below, the on time for today for my lights is 10:46 and off time of 00:00.

What did I miss?

Code: Select all

#include <ReefAngel_Features.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <Humidity.h>
#include <DCPump.h>
#include <ReefAngel.h>
#include <SunLocation.h>

////// Place global variable code below here

byte wp40mode; //Mode Byte
SunLocation sl;

////// Place global variable code above here


void setup()
{
  // This must be the first line
  ReefAngel.Init();  //Initialize controller
  // Add Standard Menu
  ReefAngel.AddStandardMenu();
  // Ports toggled in Feeding Mode
  ReefAngel.FeedingModePorts = Port3Bit | Port5Bit | Port6Bit | Port7Bit;
  // Ports toggled in Water Change Mode
  ReefAngel.WaterChangePorts = Port3Bit | Port4Bit | Port5Bit | Port6Bit | Port7Bit | Port8Bit;
  // Ports toggled when Lights On / Off menu entry selected
  ReefAngel.LightsOnPorts = Port1Bit | Port2Bit;
  // Ports turned off when Overheat temperature exceeded
  ReefAngel.OverheatShutoffPorts = Port1Bit | Port2Bit;
  // Use T1 probe as temperature and overheat functions
  ReefAngel.TempProbe = T1_PROBE;
  ReefAngel.OverheatProbe = T1_PROBE;

  // Ports that are always on
  ReefAngel.Relay.On( Port3 ); //Mag 12 Return Pump

  ////// Place additional initialization code below here

  {
    ReefAngel.Init();
    // initialize the sunrise / sunset location
    sl.Init(28.253831, -81.264846); // Latitude , Longitude values set
  }

  ////// Place additional initialization code above here
}

void loop()
{

  ReefAngel.DayLights( Port1 ); //T5HO Day Lights (Whites)
  ReefAngel.ActinicLights( Port2 ); //T5HO Actinic Lights (Blues)
  ReefAngel.Relay.DelayedOn( Port4 );  //Reef Octopus NW150 Skimmer and Phosban
  ReefAngel.StandardFan( Port6 ); //Cooling Fans
  ReefAngel.DosingPumpRepeat1( Port7 ); //Dosing Pump
  ReefAngel.StandardATO( Port8 ); //ATO Pump
  
  ////// Place your custom code below here

  {
    // handle updating sunrise and sunset values
    sl.CheckAndUpdate();
    ReefAngel.ShowInterface();
  }

  // Dimmable royal blues 100% if actinics (relay port 2) are on, else moonlight
  if ReefAngel.Relay.IsMaskOn(Port2); {
    ReefAngel.PWM.SetActinic( 100 );
  }
  else {
    ReefAngel.PWM.SetActinic( MoonPhase() );
  }

  //Start Feeding Mode at 17:00 (hour*60*60)
  if ( (now()%86400==61200) /*|| (now()%86400==39600) */) {  
    ReefAngel.FeedingModeStart();
  } 

  //Hydor Evolution 1400 Circulation Pump Port5 Off Between 10pm and 8am
  if (hour()<8 || hour()>=22)  {
    ReefAngel.Relay.Off(Port5);
  }  
  else {
    ReefAngel.Relay.On(Port5);
  }

  //assignmenets are = and comparisons are ==
  if (hour()>=8 && hour()<12) wp40mode=7; //8am - 11:59am
  else if (hour()>=12 && hour()<17) wp40mode=6; //12pm - 4:59pm
  else if (hour()>=17 && hour()<18) wp40mode=5; //5pm - 5:59pm
  else if (hour()>=18 && hour()<24) wp40mode=3; //6pm - 11:59pm
  else wp40mode=4; //12am - 7:59am

  if (wp40mode==9) ReefAngel.PWM.SetDaylight( 100 ); //Full Speed
  if (wp40mode==8) ReefAngel.PWM.SetDaylight( 100/3 ); //Third Speed
  if (wp40mode==7) ReefAngel.PWM.SetDaylight( millis()%1100>800?75:0); //Custom "W1"
  if (wp40mode==6) ReefAngel.PWM.SetDaylight( TidalSwellMode(80,true ) ); //WaveMaxSpeed, PulseSync
  if (wp40mode==5) ReefAngel.PWM.SetDaylight( NutrientTransportMode(0,65,2000,true ) ); //PulseMinSpeed, PulseMaxSpeed, PulseDuration (ms), PulseSync
  if (wp40mode==4) ReefAngel.PWM.SetDaylight( LongPulseMode(0,50,3,true ) ); //PulseMinSpeed, PulseMaxSpeed, PulseDuration (s), PulseSync
  if (wp40mode==3) ReefAngel.PWM.SetDaylight( ShortPulseMode(0,80,1200,true ) ); //PulseMinSpeed, PulseMaxSpeed, PulseDuration (ms), PulseSync
  if (wp40mode==2) ReefAngel.PWM.SetDaylight( ReefCrestMode(50,20,true ) ); //WaveSpeed, WaveOffset, PulseSync
  if (wp40mode==1) ReefAngel.PWM.SetDaylight( SineMode(0,100,10,true ) ); //PulseMinSpeed, PulseMaxSpeed, PulseDuration (ms), PulseSync
  if (wp40mode==0) ReefAngel.PWM.SetDaylight( TideMode(20,100,20 ) ); //WaveSpeed, minOffset, maxOffset

  ////// Place your custom code above here

  // This should always be the last line
  ReefAngel.Portal( "clw143" );
  ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
  byte x = 6;
  byte y = 2;
  byte t;
  char text[7];

  ReefAngel.LCD.Clear(DefaultBGColor,0,0,132,132);
  ReefAngel.LCD.DrawDate(6, 3);
  ReefAngel.LCD.Clear(COLOR_BLACK, 1, 11, 132, 11);
  pingSerial();

  ReefAngel.LCD.DrawText(0,255,18,14,"Jebao WP40 Mode:");
  if (wp40mode==0) ReefAngel.LCD.DrawLargeText(COLOR_GREEN,255,42,25,"Tide");
  else if(wp40mode==1) ReefAngel.LCD.DrawLargeText(COLOR_RED,255,42,25,"Sine");
  else if (wp40mode==2) ReefAngel.LCD.DrawLargeText(COLOR_ORANGE,255,23,25,"Reef Crest");
  else if (wp40mode==3) ReefAngel.LCD.DrawLargeText(COLOR_CORNFLOWERBLUE,255,23,25,"Short Pulse");
  else if (wp40mode==4) ReefAngel.LCD.DrawLargeText(COLOR_CORNFLOWERBLUE,255,23,25,"Long Pulse");
  else if (wp40mode==5) ReefAngel.LCD.DrawLargeText(COLOR_MAGENTA,255,8,25,"Nutrient Trnsp.");
  else if (wp40mode==6) ReefAngel.LCD.DrawLargeText(COLOR_MAGENTA,255,23,25,"Tidal Swell");
  else if (wp40mode==7) ReefAngel.LCD.DrawLargeText(COLOR_BLUE,255,40,25,"Custom");
  else if (wp40mode==8) ReefAngel.LCD.DrawLargeText(COLOR_BLUE,255,25,25,"1/3 Power");
  else if (wp40mode==9) ReefAngel.LCD.DrawLargeText(COLOR_BLUE,255,23,25,"Full Power");
  pingSerial();

  ReefAngel.LCD.Clear(COLOR_BLACK, 1, 34, 132, 34);
  ReefAngel.LCD.DrawText(0,255,10,37,"Disp");
  ConvertNumToString(text, ReefAngel.Params.Temp[T1_PROBE], 10);
  ReefAngel.LCD.DrawLargeText(COLOR_BLUE, 255, 8, 45, text, Num8x8);
  pingSerial();

  ReefAngel.LCD.DrawText(0,255,53,37,"Room");
  ConvertNumToString(text, ReefAngel.Params.Temp[T2_PROBE], 10);
  ReefAngel.LCD.DrawLargeText(COLOR_BLUE, 255, 50, 45, text, Num8x8);
  pingSerial();

  ReefAngel.LCD.DrawText(0,255,95,37,"Sump");
  ConvertNumToString(text, ReefAngel.Params.Temp[T3_PROBE], 10);
  ReefAngel.LCD.DrawLargeText(COLOR_BLUE, 255, 93, 45, text, Num8x8);
  pingSerial();

  ReefAngel.LCD.Clear(COLOR_BLACK, 1, 53, 132, 53);
  ReefAngel.LCD.DrawText(0,255,10,56,"pH");
  ConvertNumToString(text, ReefAngel.Params.PH, 100);
  ReefAngel.LCD.DrawLargeText(COLOR_BLUE, 255, 27, 56, text, Num8x8);
  pingSerial();

  ReefAngel.LCD.DrawText(0,255,67,56,"WP40");
  ConvertNumToString(text, ReefAngel.PWM.GetDaylightValue(),1);
  ReefAngel.LCD.DrawLargeText(COLOR_BLUE, 255, 95, 56, text, Num8x8);
  ReefAngel.LCD.DrawText(0,255,118,56," %");
  ReefAngel.LCD.Clear(COLOR_BLACK, 1, 65, 132, 65);
  pingSerial();

  ReefAngel.LCD.DrawLargeText(COLOR_BLUE,255,7,85,"Shaun");
  ReefAngel.LCD.DrawHugeText(COLOR_RED, 255, 50, 88,"&");
  ReefAngel.LCD.DrawLargeText(COLOR_GREEN,255,70,98,"Kerri's");
  ReefAngel.LCD.DrawLargeText(0,255,7,108,"90Gal Reef Tank");
  pingSerial();

  {
    char buf[16];
    sprintf(buf, "%02d:%02d", sl.GetRiseHour(), sl.GetRiseMinute());
    ReefAngel.LCD.DrawText(DefaultFGColor,DefaultBGColor,10,67,"Sunrise: ");
    ReefAngel.LCD.DrawText(DefaultFGColor,DefaultBGColor,58,67,buf);
    sprintf(buf, "%02d:%02d", sl.GetSetHour(), sl.GetSetHour());
    ReefAngel.LCD.DrawText(DefaultFGColor,DefaultBGColor,10,75,"Sunset:  ");
    ReefAngel.LCD.DrawText(DefaultFGColor,DefaultBGColor,58,75,buf);
  }

  byte TempRelay = ReefAngel.Relay.RelayData;
  TempRelay &= ReefAngel.Relay.RelayMaskOff;
  TempRelay |= ReefAngel.Relay.RelayMaskOn;
  ReefAngel.LCD.DrawOutletBox(12, 118, TempRelay);
  pingSerial();

}
void DrawCustomGraph()
{
}
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

Where are those coordinates supposed to be and what timezone are you in?

One thing to note is that the controller assumed it is in GMT timezone so whatever coordinates you calculate the time will be set accordingly. You also probably need to adjust the offset so it's in the window you want your lighting schedule to be.

Also your code is a bit odd in that the check and update is combined with showinterface and inside { } which changes the scope. I dont think thats interfering but i wouldnt do that and you should only have showinterface called once at the end of loop which you do.
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: Sunrise / sunset code based on location

Post by clw143 »

location is in Florida EST -5

Code: Select all

sl.Init(28.253831, -81.264846); // Latitude , Longitude values set
All the code added was from a post on usage from Binder on page 5, maybe that was wrong.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Sunrise / sunset code based on location

Post by lnevo »

Try this code

http://forum.reefangel.com/viewtopic.php?p=17804#p17804

I think there were some bugs in the initial version. Otherwise start setting an offset so that it gets earlier and see how many hours you need to pull back to not have midnight as the set time.
clw143
Posts: 118
Joined: Fri Jun 21, 2013 8:20 pm
Location: Louisiana

Re: Sunrise / sunset code based on location

Post by clw143 »

What is the difference in what you have done here

Code: Select all

ReefAngel.StandardLights( Port1 );
ReefAngel.ActinicLights( Port2 );
And what the wizard has done here

Code: Select all

  ReefAngel.DayLights( Port1 ); //T5HO Day Lights (Whites)
  ReefAngel.ActinicLights( Port2 ); //T5HO Actinic Lights (Blues)
Post Reply