Go Down

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

#### PaulS #15
##### Feb 10, 2010, 08:12 pm
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:

255 - (((timeInCycle - lengthOfHalfCycle)/lengthOfHalfCycle) * 255)

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. #16
##### Feb 10, 2010, 10:11 pmLast Edit: Feb 10, 2010, 11:54 pm by adambrum Reason: 1

I got what was said earlier about using the fractions for the cycle.

So what i was thinking was if i just used the fraction to 2 decimal places then i would get a 0-99 value depending on were i was in the cycle.

For example

1263539400 is the last new moon and would have a value of 1.00
1264815121 is the last full moon and would have a value of 1.50
and so on.

problem is how do i do the calculations in the arduino sketch, i take it done with floats ?

What i want to do is

timeincycle = ((current time - last new moon) / lenghtofhalfcycle)

So the current cycle would be for example 0.83 i just need the 83 part of this.

I do appriciate your help guys  #17
##### Feb 11, 2010, 12:00 amLast Edit: Feb 11, 2010, 12:04 am by adambrum Reason: 1
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

Code: [Select]
`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}`

#### PaulS #18
##### Feb 11, 2010, 12:14 amLast Edit: Feb 11, 2010, 12:14 am by PaulS Reason: 1
To extract the fractional part of a number:

Code: [Select]
`float wholeNumber = 8.453;int integerPart = wholeNumber;float fractionalPart = wholeNumber - fractionalPart;`

Then, you can multiply fractionalPart by 100, if you want two digits, and stuff the result into an integer:

Code: [Select]
`int twoDigits = 100 * fractionalPart; // will contain 45` #19
##### Feb 11, 2010, 12:54 am
I added the new bits to the sketch and its not quite right. Next to the serial.print i have added whats being printed.

Code: [Select]
`unsigned long x;unsigned long y;float z;int twoDigits;void setup(){Serial.begin(9600);}void loop(){  x = 1000000000;  z = (float) x / 300000000.0;  delay(2000);  float wholeNumber = z;int integerPart = wholeNumber;float fractionalPart = wholeNumber - fractionalPart; Serial.print("z = ");  Serial.println(z);                         //printing 3.33Serial.print("wholeNumber");Serial.println(wholeNumber);               //printing 3.33Serial.print("fractionalPart = ");Serial.println(fractionalPart);            //printing 3.33Serial.print("twoDigits =");Serial.println(twoDigits);                 //printing 0  int twoDigits = 100 * fractionalPart; } ` #20
##### Feb 11, 2010, 01:09 am
Sorted that one.

this line

float fractionalPart = wholeNumber - fractionalPart;

should have been

float fractionalPart = wholeNumber - integerPart;

Thanks for the help so far, this should keep me busy for a while but i think i have the makings of a code.

#### PaulS #21
##### Feb 11, 2010, 01:12 am
Damn keyboard didn't type what I was muttering to myself as I typed... 