The main goal of my project is to demonstrate that we can see on a TFT screen the astronomical data of the Moon in real time, from commercial hardware such as the Arduino MEGA 2560 board, the DS3231 clock module and the TFT LCD Touch 2.4 " Shield. For a better understanding we have divided our work into the following sections: Hardware, Software, Test and Conclusion.


HARDWARE:
This watch is developed with the Arduino MEGA 2560 board, the DS3231 clock module, and a TFT LCD TOUCH 2.4".
The electrical diagram of this clock is shown in the figure below.

Features and more details of the DS3231 you can see on: https://www.anodas.lt/en/ds3231-at24c32-real-time-clock-module

Datasheet: https://www.terraelectronica.ru/pdf/show?pdf_file=%252Fz%252FDatasheet%252FD%252FDS3231.pdf

The best TFT LCD TOUCH 2.4"  guide on: https://electropeak.com/learn/absolute-beginners-guide-to-tft-lcd-displays-by-arduino/

SOFTWARE:

I have used version 1.8.9 of Arduino IDE and you can download it at: https://www.arduino.cc/en/main/software

Then I show the code in sections for a better understanding:

First, we are going to declare the libraries of the TFT screen and the DS3231 clock module. We also declare a list of colors that we use in text colors.

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;      
#include <TouchScreen.h>
#include "images.h"

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h" 
RTC_DS1307 rtc; 
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 
String moon;
int16_t BOXSIZE;
//int16_t PENRADIUS = 1;
uint16_t ID, currentcolor;
uint8_t Orientation = 0;    //PORTRAIT

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Now, we go to the void setup where we test the clock to see if it is working. We also do the same with the TFT screen drawing small squares.

void setup(void)
{   
    while (!Serial);
  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }    
    uint16_t tmp;
    tft.reset();
    ID = tft.readID();
    tft.begin(ID);
    tft.setRotation(Orientation);
    tft.fillScreen(BLACK);
    BOXSIZE = tft.width() / 6;
    tft.fillScreen(BLACK);
    tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
    tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
    tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
    tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
    tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
    tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
    tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
    currentcolor = RED;
}

From the data of year, month and day, then we create the code to know the phases of the moon through calculations. This information was obtained from the following algorithm published on: https://www.nano-reef.com/forums/topic/217305-a-lunar-phase-function-for-the-arduino/

    DateTime now = rtc.now(); 

    int nYear = now.year();
    int nMonth = now.month();
    int nDay = now.day();
    float phase;
    double AG, IP;
    long YY, MM, K1, K2, K3, JD;
    YY = nYear - floor((12 - nMonth) / 10);
    MM = nMonth + 9;

    if (MM >= 12)
    {
    MM = MM - 12;
    }
    K1 = floor(365.25 * (YY + 4712));
    K2 = floor(30.6 * MM + 0.5);
    K3 = floor(floor((YY / 100) + 49) * 0.75) - 38;
    JD = K1 + K2 + nDay + 59;
    if (JD > 2299160)
    {
    JD = JD - K3;
    }
    IP = MyNormalize((JD - 2451550.1) / 29.530588853);
    AG = IP*29.53;
    phase = 0;
    if ((AG < 1.84566) && (phase == 0))
    {
    phase = 0; //new; 0% illuminated
    moon= "New";     
    }
    if ((AG < 5.53699) && (phase == 0))
    {
    phase = .25; //Waxing crescent; 25% illuminated
    moon = "Waxing crescent";   
    }
    if ((AG < 9.922831) && (phase == 0))
    {
    phase = .50; //First quarter; 50% illuminated
    moon = "First quarter";  
    }
    if ((AG < 12.91963) && (phase == 0))
    {
 phase...
Read more »