Moon rise/set implementation

Share you PDE file with our community
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

The use of "" or <> depends on where you put the library.

I guess in your case the <> is appropriate.

Scope should not be a problem here if its included properly.

Can you try again now. #included was incorrect so maybe now that its #include?
Aemornion
Posts: 5
Joined: Fri Jun 07, 2013 8:45 pm

Re: Moon rise/set implementation

Post by Aemornion »

Ah whoops, the "included" is an error on my part in the post, not in the actual program. It is most certainly include, not included in the ino

I noticed something else, I was using a lower case m rather than M in Moon.h

Now when i change it to a capital M

Code: Select all

#include <Moon.h>
I get a slew of new errors about things inside the library :?
at least I know it is actually reading the library...i guess...



Code: Select all

In file included from moon_test.ino:5:
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:35: error: 'boolean' does not name a type
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:36: error: 'boolean' does not name a type
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:37: error: 'byte' does not name a type
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:38: error: 'byte' does not name a type
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:39: error: 'byte' does not name a type
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:40: error: 'byte' does not name a type
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:51: error: 'PI' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h: In function 'float lst(int, int, float)':
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:63: error: 'floor' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h: In function 'void moon(float)':
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:78: error: 'floor' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:85: error: 'PI' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:92: error: 'sin' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:106: error: 'cos' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:129: error: 'sqrt' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:130: error: 'atan' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h: In function 'float test_moon(int, float, int, float)':
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:165: error: 'PI' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:173: error: 'sin' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:174: error: 'cos' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:196: error: 'sqrt' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:203: error: 'floor' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:209: error: 'atan2' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:214: error: 'struct moon_t' has no member named 'riseH'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:215: error: 'struct moon_t' has no member named 'riseM'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:217: error: 'struct moon_t' has no member named 'isRise'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:222: error: 'struct moon_t' has no member named 'setH'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:223: error: 'struct moon_t' has no member named 'setM'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:225: error: 'struct moon_t' has no member named 'isSet'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h: At global scope:
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:231: error: 'boolean' has not been declared
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h: In function 'void riseset(int, int, int)':
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:235: error: 'struct moon_t' has no member named 'riseH'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:236: error: 'struct moon_t' has no member named 'riseM'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:237: error: 'struct moon_t' has no member named 'setH'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:238: error: 'struct moon_t' has no member named 'setM'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:271: error: 'PI' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:274: error: 'PI' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:279: error: 'struct moon_t' has no member named 'isRise'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:280: error: 'struct moon_t' has no member named 'isSet'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h: In function 'void moon_init(int, int)':
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:303: error: 'boolean' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:303: error: expected `;' before 'moonUp'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:309: error: 'struct moon_t' has no member named 'isRise'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:310: error: 'struct moon_t' has no member named 'riseH'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:310: error: 'struct moon_t' has no member named 'riseM'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:310: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:312: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:315: error: 'struct moon_t' has no member named 'isSet'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:316: error: 'struct moon_t' has no member named 'setH'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:316: error: 'struct moon_t' has no member named 'setM'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:316: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:318: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:321: error: 'moonUp' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:331: error: 'struct moon_t' has no member named 'isSet'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:334: error: 'struct moon_t' has no member named 'setH'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:334: error: 'struct moon_t' has no member named 'setM'
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:334: error: 'ScheduleTime' was not declared in this scope
Aemornion
Posts: 5
Joined: Fri Jun 07, 2013 8:45 pm

Re: Moon rise/set implementation

Post by Aemornion »

Ok interesting,
I was able to knock out many errors by including

Code: Select all

#if ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif
in my header, I have no idea why its just something random i found online to try.

anyway, now my errors read as follows. All are based around the 'ScheduleTime' function within the library.
Strange indeed.

Code: Select all

In file included from moon_test.ino:13:
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h: In function 'void moon_init(int, int)':
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:310: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:312: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:316: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:318: error: 'ScheduleTime' was not declared in this scope
C:\Users\Jay\Documents\Arduino\libraries\Moon/Moon.h:334: error: 'ScheduleTime' was not declared in this scope
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

ScheduleTime is a function in the Time library. Not sure if that's a standard arduino function or exclusive in the RA library. Anyway, you'll need that package. I did some conversion from the original package I found to make it work with RA.

You may also want to try the original as that may be more based on just Arduino standard libs...
http://code.google.com/p/stilo/
Aemornion
Posts: 5
Joined: Fri Jun 07, 2013 8:45 pm

Re: Moon rise/set implementation

Post by Aemornion »

Hmmm, after much mullings
I removed

Code: Select all

if (Moon.isRise) {
    rise_time=ScheduleTime(Moon.riseH, Moon.riseM, 0);
  } else {
    rise_time=ScheduleTime(0, 0, 0);
  }

  if (Moon.isSet) {
    set_time=ScheduleTime(Moon.setH, Moon.setM, 0);
  } else {
    set_time=ScheduleTime(23, 59, 59);
  }

  moonUp = 0;
  if (rise_time < set_time) {
    if ((tnow >=rise_time) && (tnow < set_time)) {
     moonUp = 1;
    }
  } else if ((tnow >= rise_time) || (tnow < set_time)) {
    moonUp = 1;
  }

  if (moonUp) {
      if ( (!Moon.isSet) || ((rise_time > set_time)&&(tnow>=rise_time)) ) {      //calculate for next day
        riseset(latitude, longitude, 1);
      }
      set_time=ScheduleTime(Moon.setH, Moon.setM, 59)+SECS_PER_DAY;
  }
From the code. with the idea that from the looks of it that portion is not actually modifying the rise time and set time values, but rather sending those values elsewhere into your program.

and YAY it compiled!


Except the rise and set times are incorrect, so maybe that portion of code really does do something. sigh oh well ill figure it out eventually i suppose.

That you for all your help =)
-Jay
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

Yeah those times are used during moon_init. Schedule time function returns number of minutes for that time...if you look at the original link it doesn't use that function so it may be in your library.
bencollinz
Posts: 151
Joined: Wed Mar 13, 2013 5:36 pm

Re: Moon rise/set implementation

Post by bencollinz »

thekameleon wrote:Here is the code for the slope. its based off of Parabola

Code: Select all

byte PWMSlopeOvernight(byte startHour, byte startMinute, byte endHour, byte endMinute, byte startPWM, byte endPWM, byte Duration, byte oldValue)
{
	int start = NumMins(startHour, startMinute);
	int end = NumMins(endHour, endMinute);
	if (start > end) //Start is greater than End so its over midnight
	{
		//Example: 2300hrs to 0200hrs
		if (hour() < endHour) start -= 1440; //past midnight
		if (hour() >= startHour) end += 1440; //before midnight
	}
	int current = NumMins(hour(), minute());
	int startD = start + Duration;
	int stopD = end - Duration;

	if ( current >= start && current <= startD )
		return constrain(map(current, start, startD, startPWM, endPWM),startPWM, endPWM);
	else if ( current >= stopD && current <= end )
	{
		byte v = constrain(map(current, stopD, end, startPWM, endPWM),startPWM, endPWM);
		return endPWM - v + startPWM;
	}
	else if ( current > startD && current < stopD )
		return endPWM;

    // lastly return the existing value
    return oldValue;
}
So, can you dumb this down for me, please? Am I putting values into somewhere or are they declared somewhere else? And I still need the moon.h file, etc?
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

You don't need that function anymore. The regular PWMSlope function now works during any timeframe.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

Latest version 1.1.0 compatible
Attachments
Moon.zip
(3.29 KiB) Downloaded 707 times
Amos Poh
Posts: 107
Joined: Sun Jul 22, 2012 4:51 am
Location: Singapore

Re: Moon rise/set implementation

Post by Amos Poh »

lnevo wrote:Hey guys,

So, I found this other aquarium controller code at http://code.google.com/p/stilo/ that had ported a javascript implementation of a Moonrise/set calculator into Arduino. I have adapted it to the RA libraries and it looks like it's working great.

Anyway if you want to have your moonlights follow an actual rise/set calculated based on GPS location, then here's what you have to do..

Code: Select all

#include Moon.h
And then in your loop() function, add this

Code: Select all

moon_init(21,-73); // pass it lat / lon - it uses ints for the caclulation...
You will then have access to the Moon struct which has the following definition:

Code: Select all

struct moon_t
{
  boolean isRise;
  boolean isSet;
  byte riseH;
  byte riseM;
  byte setH;
  byte setM;
  float riseAZ;
  float setAZ;
}
So you'll be able to do something like this:

Code: Select all

  if (Moon.isRise) {
    ReefAngel.PWM.SetDaylight(MoonPhase());
    ReefAngel.PWM.SetActinic(MoonPhase());
  } else {
    ReefAngel.PWM.SetDaylight(0);
    ReefAngel.PWM.SetActinic(0);
  }
or add this to your display

Code: Select all

    sprintf(buf, "%02d:%02d", Moon.riseH, Moon.riseM);
    ReefAngel.LCD.DrawText(COLOR_BLACK,DefaultBGColor,x,y,"MR:"); x+=21;
    ReefAngel.LCD.DrawText(COLOR_RED,DefaultBGColor,x,y,buf);
    sprintf(buf, "%02d:%02d", Moon.setH, Moon.setM); x+=36;
    ReefAngel.LCD.DrawText(COLOR_BLACK,DefaultBGColor,x,y,"MS:"); x+=21;
    ReefAngel.LCD.DrawText(COLOR_RED,DefaultBGColor,x,y,buf); x+=36;
    if(Moon.isRise) ReefAngel.LCD.DrawText(COLOR_RED,DefaultBGColor,x,y,"@");
    else ReefAngel.LCD.DrawText(COLOR_RED,DefaultBGColor,x,y,"_");

Hi Lee thanks for sharing,

Just a few question hope you can assist me.
I am running 2 Rapid dimmable LED lightest, using analog dimming expansion.
the light set has 3 channels, 1 channel blue, 1 channel white and 1 channel UV red and green.

Currently iam running it to ramp up the different channels at different timing.
1)How can i use this code that you provided to fit into the different channel?
2)what should i do with the Moon.zip files?

Pardon my noob questions :)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

The moon library only provides the times based on a gps coordinate.

First unzip the package into your libraries folder. The in your INO you need to put this in the top

#include <Moon.h>

You the have to initialize the library with your coordinates

moon_init(21,-73);

That will populate the Moon rise/set variables. Here's a sample to set the daylight channel based on the moon rise/set

ReefAngel.PWM.SetDaylight(PWMSlope(Moon.riseH,Moon.riseM,Moon.setH,Moon.setM, 0,MoonPhase(),30,0));

It's not exact I think because of the lack of time zone support and that arduino thinks it's in GMT. I have not had time or care to get it to line up, but the key is that it creates a great lunar simulation with changing rise/set times.

Enjoy!
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: Moon rise/set implementation

Post by Sacohen »

Trying to understand how to set this up.
My Lunar Hub and lights should be arriving tomorrow.

I take the moon.h file and put it in my RA libraries folder.
Then add #include <Moon.h> to the top section of my code.
In the loop() function, add this

moon_init(21,-73); // pass it lat / lon - it uses ints for the caclulation...

ReefAngel.PWM.SetChannel(5, PWMSlope(Moon.riseH,Moon.riseM,Moon.setH,Moon.setM, 0,MoonPhase(),30,0));

I'm putting this on channel 5 of the dimming module.

According to a table I found online the Moonrise for tonight is 4:13 PM and the Moonset is 4:47 AM with the Meridian Passing time at 10:51 PM (is the the peak or azimuth).

Do I need to do anything with the numbers I made red aare those variables that can be adjusted?
ReefAngel.PWM.SetChannel(5, PWMSlope(Moon.riseH,Moon.riseM,Moon.setH,Moon.setM, 0,MoonPhase(),30,0));

Also if my Actinic lights come on at Noon and go off at 10pm will there be a period of time where no lights are on like 4:47am to 12pm, what will be happening there?
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

0 is the starting point for the slope. 30 is the slope duration. The last 0 os the default when the moon is not out.

As i said before i think timezone may skew when the ra calculates rise/set since ra does not use timezones.

Also you are comparing your rise/set but the gps coordinates i think were mine which is somewhere in the carribean...i guess close enough to you ;)

If you want set Moon.riseH etc to a custom variable or display it on a custom screen to see what times are calculated...
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: Moon rise/set implementation

Post by Sacohen »

Yes you did mention the offset because of the time zones.
If I wanted to be more accurate with GPS coordinates I would put in moon_init(20,-80); // pass it lat / lon - it uses ints for the caclulation...

I got those from http://www.gps-coordinates.net/ for my address.

This works with Parabola too, correct?
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

Your just getting the rise and set time. You can use it for whatever function you want.

You could even add a check to see if moon is up and run your jaebos at a different mode or speed :)

moon_init just loads up the variables and then you can use them for whatever you want.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: Moon rise/set implementation

Post by Sacohen »

I don't want to get too complicated right now.
I just want to set up a realistic Moon cycle.

I want to try to set this up on my own and not have to get others involved like I did on the de-nitrification system.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Moon rise/set implementation

Post by rimai »

Do you want to just know what % of full moon today is or you need to know the time of moon rise/set??
I use just the % of the full moon and it works just fine :)
New moon, the tank is super dark and full moon the tank has a nice shine at night.
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: Moon rise/set implementation

Post by Sacohen »

I think what I want to do is use % of the moon, but set it to my on and off time.
For example my lights go out at 10pm so I would want the moon to be fully up by 10pm and then set at 12noon when my lights come back on.

My LED's are not set up yet so I'm just using my old Power PC's as an on/off.

When I get the LEDS running I will probably want to do a more realistic sunrise/sunset & moon rise/moon set.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Moon rise/set implementation

Post by lnevo »

So you just need the MoonPhase() function thats built into the libraries
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: Moon rise/set implementation

Post by Sacohen »

That will do % of the full moon?

Sent from my HTC One VX using Tapatalk
Post Reply