Moon Phase for Picaxe ?

marky26uk

New Member
Hi all, does anybody know of a way to get the picaxe to calculate the Moon phases ?
Would be really cool to get the lcd to display this.

Thanks,
Mark
 

tikeda

Member
Julian date code for Picaxe

Do you want percent illumination on a daily basis or would something like 'new, first quarter, full, last quarter' be sufficient? Here is a link to the US Naval Observatory pages for moon phases. You could store phase information in a serial EEPROM using a table referenced to a Julian date. Alternatively, you can calculate a phase based on a synodic lunar month of 29.5 days and determine the number of days into the present cycle. Doing the latter would look something like this:

1) Find a Julian date in the past that corresponds with a new moon. Store it in a constant (= JD_NewMoonZero).
2) Calculate the current Julian date (= JD_Now).
3) Calculate the number of days into the current phase (=DayNumber) where:
DayNumber = ((JD_Now - JD_NewMoonZero) * 10) // 295

With word variables, you should be able to calculate phases out to about 15 years from the "JD_NewMoonZero" date without overflowing and the numbers should be reasonably accurate to within day or two. If you need to calculate a longer range of years, store more new moon dates spaced about 10-15 years apart.

Here is a Julian date calculator that is reasonably optimized for Picaxe math:

Code:
'Julian day number (JDN) calculator
'Created: 2007-12-23 (T.Ikeda - KA1OS)
'Latest mod: 2008-01-27

'Works with dates from 2000-01-01 to 2178-12-31
'
'On 2000-01-01, JDN = 0 (Epoch: 2000-01-01) and increments
'from there. This program properly accounts for 2100
'not being a leap year.
'
'In the simulator, this doesn't over overflow until
'2178-06-05 (05-Jun-2179). In theory, the processor should
'overflow in one of the calculation steps (367 * YYY) when
'the YYY is 179
'
'Formulas derived from the following sources and optimized
'for Picaxe math (minimizing multiplication & division
'steps in the calculations):
'	http://scienceworld.wolfram.com/astronomy/JulianDate.html
'		by Eric W. Weisstein
'	http://www.emesystems.com/BS2math4.htm
'		by Tracy Allen
'
'Note that this program uses *no* error checking for illegal
'	dates: e.g. 2007-13-32
'

Symbol JDN = w4
Symbol YYY = b5	'0 to 178, corresponding to years 2000-2178
Symbol MM = b6	'Months: 1-12
Symbol DD = b7	'Day: 1-31

'Plug-in values for testing... Remove after debugging.
'06-Jun-2010 =
YYY = 10
MM = 6
DD = 6

JDN = 0
If MM > 2 Then
	JDN = 1
End If

JDN = YYY + JDN * 7 / 4
JDN = 367 * YYY - JDN
JDN = MM * 3912 / 128 + DD - 31 + JDN

'*******************************************
'X1/X2 parts can use shift right instead of divides.
'JDN = (YYY + JDN) * 7 >> 2
'JDN = (367 * YYY) - JDN
'JDN = ((MM * 3912) >> 7) + DD - 31 + JDN
'*******************************************

'Add correction for 2100 not being a leap year...
If JDN > 36584 Then
	JDN = JDN -1
End If

'Testing output... Remove after debugging.
bintoascii JDN, b0, b1, b2, b3, b4
sertxd (b0,b1,b2,b3,b4)

End
 
Last edited:

marky26uk

New Member
Moon Phase

Hi Tikeda, i have now implemented you're wonderful code & made a moon graphic to show phases of the moon in my project, seems to work a treat, brilliant !!!!

Yours,
Mark
 

Michael V

Senior Member
Pictures of pictures?

I think this would also be very cool, and intrigues as to the characters. I'm learning that with clever programming LCDs can be a really nice user interface. Pictures often speak better than numbers, especially in low education level environments.

What is the operation of the LCD? Are you doing special characters, lookup tables, serial or I2C or SPI? Is it 16 x 12 or 20 x 4, or something more cosmic?

Any chance of seeing a photo of what the LCD graphics look like?

Michael
 

marky26uk

New Member
Moon Phase

Hi Michael, i'm using the 16x2 lcd display from www.tech-supplies.co.uk
For each moon phase i'm defining my own character using the lcd wizard in the programming editor, example :-
This is just one part of my moon graphic,
serout 0,t600, (254, 72, 32, 32, 32, 32, 48, 48, 48, 32)

Then display this on the lcd:-
serout 0,t600,(254,198,9)

So my full moon looks like this the way i've done it:-
Code:
. . . . . 
. . . . .
. . . . .
. # # # .
# # # # #
# # # # #
# # # # #
. # # # .
 
Half moon:-
. . . . . 
. . . . .
. . . . .
. # # . .
# # # . .
# # # . .
# # # . .
. # # . .
etc,
Will soon purchase the graphic lcd from tech-supplies as a much bigger & better graphic could be produced, but the above still looks pretty cool, it is abit small but looks good.
Would love to incorporate moonrise & set times but the maths is more involved, maybe using the floating math chip would do it.
Still, a nice little addition to picaxe lcd projects.
If you would like to see a pic i'd me more than pleased, just send me you're email.
mdennisond@sky.com

P.S. Graphics above not perfect, something with the spaces went funny but you get the general idea.

Yours,
Mark
 
Last edited by a moderator:

tikeda

Member
I have a Casio watch that displays sunrise/set times. It takes about 30-40 seconds to recalculate a change and it is normally accurate to within about 2-4 minutes of more precise calculations. If your device will remain in one location or at least at the same latitude, storing a table in a serial eeprom would be a less expensive option than the math coprocessor chip. Using interpolation could probably reduce the table sizes significantly. Or, by approximating the change over the years with equations of polynomials and/or periodic functions, one might be able to fit data for a range of latitudes in relatively little memory space.

To get an idea of what the calculations can look like, open this web page and examine the Javascript code. I’d bet my watch isn’t running an algorithm quite like that…
 
Last edited:
Top