Go Down

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

PaulS

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.
The art of getting good answers lies in asking good questions.

adambrum

#16
Feb 10, 2010, 10:11 pm Last Edit: Feb 10, 2010, 11:54 pm by adambrum Reason: 1
Stuck all ready !

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  :)


adambrum

#17
Feb 11, 2010, 12:00 am Last 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 am Last 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

The art of getting good answers lies in asking good questions.

adambrum

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.33

Serial.print("wholeNumber");
Serial.println(wholeNumber);               //printing 3.33

Serial.print("fractionalPart = ");
Serial.println(fractionalPart);            //printing 3.33

Serial.print("twoDigits =");
Serial.println(twoDigits);                 //printing 0
 
int twoDigits = 100 * fractionalPart;
}

adambrum

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

Damn keyboard didn't type what I was muttering to myself as I typed...
The art of getting good answers lies in asking good questions.

adambrum

Thanks for all your help.

i should be able to tweak the code now to do what i want and the good thing is due to using a 0-99 int for the value i can assign differnt values to for any of the 0-99 ints so i should be able to get a really good simulation instead of just a liniar increase/decrease.

Again Thanks

Go Up