Hi all, I'm working on a solar tracker and would like to calculate the sunrise/sunset position. The aim is to use a PIC controlling a pair of steppers and gear trains One algorithm frequently cited is this one http://williams.best.vwh.net/sunrise_sunset_algorithm.htm That page is reproduced below Now, the first problem has arisen. Do I assume correctly that the C function "floor" is similar to INT ? ie, the decimal part of a result is discarded ? http://en.wikipedia.org/wiki/Floor_and_ceiling_functions 15th November 2010, day of year (by addition) 319 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 15 =3D 319 day =3D 15, month =3D 11, year =3D 10 N1 =3D floor(275 * 11 / 9) N1 =3D 336 N2 =3D floor((month + 9) / 12) N2 =3D 1 N3 =3D 5 [ by way of (1 + INT(10 - (4 * (INT(10/4) + 2)/3))) ] N =3D 336 - (1 * 5) + 15 - 30 N =3D 316 Where did the other 3 days go ? Or what did I miss ? Working today's date, 28th February 2011, day of year 59 N1 =3D floor(275 * 2 / 9) N1 =3D 61 N2 =3D floor((2 + 9) /12) N2 =3D 0 N3 =3D 6 [ by way of (1 + INT(11 - (4 * (INT(11/4) + 2)/3))) ] N =3D 61 - (0 * 6) + 28 - 30 N =3D 59 Coincidentally correct ? Joe For your interest, timeanddate do solar tables (show all columns) eg, this month in Auckland http://www.timeanddate.com/worldclock/astronomy.html?n=3D22&month=3D2&year= =3D2011&obj=3Dsun&afl=3D-1&day=3D1 Compare Quito (equator) and Nome (way north) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Source: Almanac for Computers, 1990 published by Nautical Almanac Office United States Naval Observatory Washington, DC 20392 Inputs: day, month, year: date of sunrise/sunset latitude, longitude: location for sunrise/sunset zenith: Sun's zenith for sunrise/sunset offical =3D 90 degrees 50' civil =3D 96 degrees nautical =3D 102 degrees astronomical =3D 108 degrees NOTE: longitude is positive for East and negative for West NOTE: the algorithm assumes the use of a calculator with the trig functions in "degree" (rather than "radian") mode. Most programming languages assume radian arguments, requiring back and forth convertions. The factor is 180/pi. So, for instance, the equation RA =3D atan(0.91764 * tan(L)) would be coded as RA =3D (180/pi)*atan(0.91764 * tan((pi/180)*L)) to give a degree answer with a degree input for L. 1. first calculate the day of the year N1 =3D floor(275 * month / 9) N2 =3D floor((month + 9) / 12) N3 =3D (1 + floor((year - 4 * floor(year / 4) + 2) / 3)) N =3D N1 - (N2 * N3) + day - 30 2. convert the longitude to hour value and calculate an approximate time lngHour =3D longitude / 15 if rising time is desired: t =3D N + ((6 - lngHour) / 24) if setting time is desired: t =3D N + ((18 - lngHour) / 24) 3. calculate the Sun's mean anomaly M =3D (0.9856 * t) - 3.289 4. calculate the Sun's true longitude L =3D M + (1.916 * sin(M)) + (0.020 * sin(2 * M)) + 282.634 NOTE: L potentially needs to be adjusted into the range [0,360) by adding/subtracting 360 5a. calculate the Sun's right ascension RA =3D atan(0.91764 * tan(L)) NOTE: RA potentially needs to be adjusted into the range [0,360) by adding/subtracting 360 5b. right ascension value needs to be in the same quadrant as L Lquadrant =3D (floor( L/90)) * 90 RAquadrant =3D (floor(RA/90)) * 90 RA =3D RA + (Lquadrant - RAquadrant) 5c. right ascension value needs to be converted into hours RA =3D RA / 15 6. calculate the Sun's declination sinDec =3D 0.39782 * sin(L) cosDec =3D cos(asin(sinDec)) 7a. calculate the Sun's local hour angle cosH =3D (cos(zenith) - (sinDec * sin(latitude))) / (cosDec * cos(latitude= )) if (cosH > 1) the sun never rises on this location (on the specified date) if (cosH < -1) the sun never sets on this location (on the specified date) 7b. finish calculating H and convert into hours if if rising time is desired: H =3D 360 - acos(cosH) if setting time is desired: H =3D acos(cosH) H =3D H / 15 8. calculate local mean time of rising/setting T =3D H + RA - (0.06571 * t) - 6.622 9. adjust back to UTC UT =3D T - lngHour NOTE: UT potentially needs to be adjusted into the range [0,24) by adding/subtracting 24 10. convert UT value to local time zone of latitude/longitude localT =3D UT + localOffset --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .