Go Down

Topic: moon phasing (Read 10677 times) previous topic - next topic


Does anyone an easy way to simulate moon phasing. I have found one example but i cant make head or tale of the coding, I know the bits i need are in there its just a little over my head. i have an rtc running so thats not a problem.

Any Ideas.

Thanks Adam


Feb 10, 2010, 10:47 am Last Edit: Feb 10, 2010, 10:47 am by Grumpy_Mike Reason: 1
So what sort of output do you need, graphical, text, LEDs?

or do you just want to calculate what the phase is from the RTC?


Just need an anolog out for leds.

It does not have to be spot on, It for a reef aquarium so im sure the fish will not be bothered if its not perfect.


Is it the shape of the Moon, or its output you need to simulate?
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.


Its just the output i need.

because i have the rtc set up and running i have the day,month,and year variables available to use.


Feb 10, 2010, 12:27 pm Last Edit: Feb 10, 2010, 12:45 pm by Grumpy_Mike Reason: 1
Just need an anolog out for leds.

Do you mean that you just want the equivalent light output?

Basically it is simple. You take an epoch, that is just a time when you know the moon is in an exact state, say full or new. Then you take the moon synodic period (29.530588 days), that is the apparent time it takes to orbit the Earth not the real sidereal period. And wind on the time and see how many orbits it has completed since then up to now. The fractional part of this count of orbits is how far through the cycle you are at the moment. The illumination approximates to a sin wave through the cycle.



Feb 10, 2010, 12:51 pm Last Edit: Feb 10, 2010, 01:21 pm by adambrum Reason: 1
Do you mean that you just want the equivalent light output?

I should have said that in the first place  :)

I will go away and have a look at the epoch thing, sounds about right for what i need.

Edit, i have the new time libary so i have the use of unix time.

So the last full moon was 30/1/2010 at 6:18 which is unix time 1264832280 and a complete cycle is 2551442.8032 second


Quick play about and i have come up with the following.

The last full moon was at unix time 1264832280.

The time now is 1265804820

The difference is 972540

A full cycle is 2551442.803 seconds


( 255 / 2551442.803 ) * 972540 = 97.199

So today an anolog value of 97.199 would give the current moon phase, well sort of, need to do some reading on moon phase.

Does this look about right so far


A full cycle is 2551442.803 seconds

If you round that off, you can use integer arithmetic.

( 255 / 2551442.803 ) * 972540 = 97.199

Do the multiplication first, and multiply by 100, then divide. The integer result will be a %-age. I doubt that the fish can distinguish between 14.3% and 15%, or, even if they can, do they care?

Where did the 255 come from?


The 255 is the analog value for the pwm.

i will try rounding up and using % and see if i can get a more usable format


Just tried this and i seems to work

Code: [Select]

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>

long dif;  
int moon;  

void setup()  {

void loop()
  dif =(now()-1264832280);
  moon = (dif * 255 / 2551443);

How does that look ?


Feb 10, 2010, 02:58 pm Last Edit: Feb 10, 2010, 03:00 pm by Grumpy_Mike Reason: 1
So today an anolog value of 97.199 would give the current moon phase,

Well I did look at the moon this morning driving in to work and it was a very thin crescent so I imagine the illumination would be close to zero today.
So 97 out of a full 255 doesn't sound right.
You need only take the fractional part of the dif not the whole of it. Plus if that is phase from new the light peaks at half way (50% phase) and then drops down again.

However, I assume you want to make the light the same a the moon contribution at night, when the moon is anything but full it is not up all night.

@PaulS - You need to avoid integer arithmetic on this because the rounding errors would accumulate every orbit period.  


dif should be an unsigned long.

What does the 255 mean?

I'd use this:

Code: [Select]
#define CYCLELENGTH 2551443
moon = (dif * 255 / CYCLELENGTH);

It makes it clear(er) what 2551443 means.


I have made the changes suggested.

When this code is sorted it will go in my main code which can fade the moonlight in and out at set times. What i need from this code is a max setting for the moon brightness to fade in and out too.

Think ineed to have a look at my maths again for the current moon


My heads hurting now, Cant work the maths out.

So far i have

The last new moon on the 15/1/2010 at 7:10 which is unix time 1263539400

the last full moon on the 30/1/2010 at 6:18 which is unix time 1264832280

A full cycle is 2551442.803 seconds

A half cycle is 1275721.4015

So what i need to do is increase a value from 0-max over half a cycle then from max-0 for the other half of the cycle.

I just cant think of a way to do this ?

Go Up