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.
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.
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?
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.
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
You know when "now" is. You know when a cycle started. You should, therefore, be able to tell how far into that cycle you are.
The value that you are looking for starts at 0 when you are at the start of the cycle, and rises to 255, and then drops to 0. If you are less then 1/2 way though the cycle, the value will be:
(timeInCycle/lengthOfHalfCycle) * 255.
If you are more then 1/2 way through the cycle, the value will be:
This assumes you want a linear increase in brightness from start of cycle to midpoint of cycle, and a linear decrease in brightness from midpoint of cycle to end of cycle.
If not, you'll need to compute the value using the sin() function, with the percent through the cycle * 360 degrees (for a whole cycle) as the input value (may need to convert to radians). Using the sin() function, it won't matter which half of the cycle you are in. Multiply the output by 255 to get the PWM value.
Ok then how do i get just the fraction part of a number for example here is a quick sketch will prints the value of 3.33 the only part i need is the 33. I can do it the otherway turning 33 into 3.3 but just not the way i need to do it.
here is the mock sketch
unsigned long x;
unsigned long y;
float z;
void setup()
{
Serial.begin(9600);
}
void loop()
{
x = 1000000000;
z = (float) x / 300000000.0;
delay(1000);
Serial.println(z); // prints out 3.33
}