TimeLord library issue

Hi
i am using for a project the timelord library to get the moon phases.
I have downloaded from

what i am trying is to print the moon phase in "words" from the function of the library that calculates the moon phase.
Below is the part of my code|:

byte day[] = {0, 0, 0, now.day(), now.month(), now.year()};

//Moon Phase
  tft.setCursor(35,210);
  tft.print(myLord.MoonPhase(day)*29.53059);
  tft.print(" / ");tft.print(myLord.MoonPhase(day));

The byte day returns a number which is between 0.0 and 0.99
As the manual of the library says the values are:

0.0 New moon
0.25 First Quarter
0.5 Full Moon
0.75 Last Quarter
0.99 Almost new

When i am trying to get the given number to convert to "words" it doesn't work.
This is my code for this:

if ((myLord.MoonPhase(day)*29.53059) == 0.80)
    {
      tft.print("Moon Phase");
    }

I know that i have to convert the byte to a number but i don't know how to do this.
Can anyone help with an example?

Comparing floating point numbers almost never works. They are not precise, like exactly 0.80. I would convert the number into an integer between 0-99 and then you could easily compare.

byte value = myLord.MoonPhase(day)*29.53059 * 100.0 + 0.5; // convert to 0-99 with rounding
if ( value == 80 ) {
  tft.print("Moon Phase");
}

you could also do a switch statement with ranges

switch (value) {
  case 0 ... 10:
    tft.print("New moon");
    break;

  case 11 .. 33:
    tft.print("Quarter moon");
    break;
 ...

}

That is what i want!
Thank you very much for your help.

float MoonPhase(uint8_t hhmmssddmmyy[6]); returns the portion of the current lunar cycle that has passed at the specified time and date.

29.53059 is the number of days in a lunar cycle.

You should only multiply MoonPhase() by 29.53059 to get the number of days into the current cycle.

float  phase = MoonPhase(day);

// Stretch New, FirstQtr, Full, and LastQtr to each cover 10% of the cycle
// (+/-5%).  The other 60% are the Crecent and Gibbous phases
const float phaseWidth = 0.05;  

if (phase < 0.0 + phaseWidth)
  tft.print("New Moon");
else
if (phase < 0.25 - phaseWidth)
  tft.print("Waxing Crecent");
else
if (phase < 0.25 + phaseWidth))
  tft.print("First Quarter");
else
if (phase < 0.50 - phaseWidth))
  tft.print("Waxing Gibbous");
else
if (phase < 0.50 + phaseWidth))
  tft.print("Full Moon");
else
if (phase < 0.75 - phaseWidth))
  tft.print("Waning Gibbous");
else
if (phase < 0.75 + phaseWidth))
  tft.print("Last Quarter");
else
if (phase < 1.0 - phaseWidth))
  tft.print("Waning Crescent");
else
if (phase < 1.0 + phaseWidth))
  tft.print("New Moon");

johnwasser:
float MoonPhase(uint8_t hhmmssddmmyy[6]); returns the portion of the current lunar cycle that has passed at the specified time and date.

29.53059 is the number of days in a lunar cycle.

You should only multiply MoonPhase() by 29.53059 to get the number of days into the current cycle.

float  phase = MoonPhase(day);

// Stretch New, FirstQtr, Full, and LastQtr to each cover 10% of the cycle
// (+/-5%).  The other 60% are the Crecent and Gibbous phases
const float phaseWidth = 0.05;

if (phase < 0.0 + phaseWidth)
  tft.print("New Moon");
else
if (phase < 0.25 - phaseWidth)
  tft.print("Waxing Crecent");
else
if (phase < 0.25 + phaseWidth))
  tft.print("First Quarter");
else
if (phase < 0.50 - phaseWidth))
  tft.print("Waxing Gibbous");
else
if (phase < 0.50 + phaseWidth))
  tft.print("Full Moon");
else
if (phase < 0.75 - phaseWidth))
  tft.print("Waning Gibbous");
else
if (phase < 0.75 + phaseWidth))
  tft.print("Last Quarter");
else
if (phase < 1.0 - phaseWidth))
  tft.print("Waning Crescent");
else
if (phase < 1.0 + phaseWidth))
  tft.print("New Moon");

Thanks for this.
So is there any way to calculate ane print the moon age in days at a certain date?

jxid:
Thanks for this.
So is there any way to calculate and print the moon age in days at a certain date?

Yes. Get the moon phase at whatever date and time you desire, then multiply by 29.53059 to get the age of the moon cycle in days.

johnwasser:
Yes. Get the moon phase at whatever date and time you desire, then multiply by 29.53059 to get the age of the moon cycle in days.

Thanks John for your help.
But i have tried your example for moon phases and i think that something is wrong.
I am in Greece at LONGITUDE = 23.40 LATITUDE = 38.03
Today ( 22/11/2019) your example gives me First Quarter and according to

the moon phase is Waning Crescent.

Can you please confirm it?
Thank you,
Yannis

I set the time and data to roughly your local current time and date and it reports that the phase is waxing crescent, about 3.475 days old. The new moon was on the 26th at 5:05 PM local time so just under 3.5 days (4:33 AM on the 30th).

Did you get any warnings when you compiled your sketch?

#include <TimeLord.h>
byte today[] = {0, 33, 04, 30, 11, 19}; // Set time to 21:00:00 29-Nov-2019
const float LONGITUDE = 23.40;
const float LATITUDE = 38.03;


const float DaysInLunarCycle = 29.53059;


// Stretch New, FirstQtr, Full, and LastQtr to each cover two days of the lunar cycle
// The other 21+ days are the Crecent and Gibbous phases
const float phaseWidth = 1.0 / DaysInLunarCycle;  // 1 day of moon phase


TimeLord tardis;


void setup()
{
  Serial.begin (115200);


  tardis.Position(LATITUDE, LONGITUDE); // tell TimeLord where in the world we are


  float  phase = tardis.MoonPhase(today);


  Serial.print("Phase: ");
  Serial.println(phase, 7);


  Serial.print("Phase age: ");
  Serial.println(phase * DaysInLunarCycle, 5);


  if (phase < 0.0 + phaseWidth)
    Serial.print("New Moon");
  else if (phase < 0.25 - phaseWidth)
    Serial.print("Waxing Crecent");
  else if (phase < 0.25 + phaseWidth)
    Serial.print("First Quarter");
  else if (phase < 0.50 - phaseWidth)
    Serial.print("Waxing Gibbous");
  else if (phase < 0.50 + phaseWidth)
    Serial.print("Full Moon");
  else if (phase < 0.75 - phaseWidth)
    Serial.print("Waning Gibbous");
  else if (phase < 0.75 + phaseWidth)
    Serial.print("Last Quarter");
  else if (phase < 1.0 - phaseWidth)
    Serial.print("Waning Crescent");
  else if (phase < 1.0 + phaseWidth)
    Serial.print("New Moon");
  Serial.println();
}


void loop()
{
}

johnwasser:
I set the time and data to roughly your local current time and date and it reports that the phase is waxing crescent, about 3.475 days old. The new moon was on the 26th at 5:05 PM local time so just under 3.5 days (4:33 AM on the 30th).

Did you get any warnings when you compiled your sketch?

#include <TimeLord.h>

byte today[] = {0, 33, 04, 30, 11, 19}; // Set time to 21:00:00 29-Nov-2019
const float LONGITUDE = 23.40;
const float LATITUDE = 38.03;

const float DaysInLunarCycle = 29.53059;

// Stretch New, FirstQtr, Full, and LastQtr to each cover two days of the lunar cycle
// The other 21+ days are the Crecent and Gibbous phases
const float phaseWidth = 1.0 / DaysInLunarCycle;  // 1 day of moon phase

TimeLord tardis;

void setup()
{
  Serial.begin (115200);

tardis.Position(LATITUDE, LONGITUDE); // tell TimeLord where in the world we are

float  phase = tardis.MoonPhase(today);

Serial.print("Phase: ");
  Serial.println(phase, 7);

Serial.print("Phase age: ");
  Serial.println(phase * DaysInLunarCycle, 5);

if (phase < 0.0 + phaseWidth)
    Serial.print("New Moon");
  else if (phase < 0.25 - phaseWidth)
    Serial.print("Waxing Crecent");
  else if (phase < 0.25 + phaseWidth)
    Serial.print("First Quarter");
  else if (phase < 0.50 - phaseWidth)
    Serial.print("Waxing Gibbous");
  else if (phase < 0.50 + phaseWidth)
    Serial.print("Full Moon");
  else if (phase < 0.75 - phaseWidth)
    Serial.print("Waning Gibbous");
  else if (phase < 0.75 + phaseWidth)
    Serial.print("Last Quarter");
  else if (phase < 1.0 - phaseWidth)
    Serial.print("Waning Crescent");
  else if (phase < 1.0 + phaseWidth)
    Serial.print("New Moon");
  Serial.println();
}

void loop()
{
}

No warning but i will give a try again.
Thank for your answer.
Yannis