Why is my screen not displaying every second?

@red_car
For some reason instead of displaying every second on my screen, now that I've added moon phases, my clock displays every second or third second. What did I do wrong now????

https://forum.arduino.cc/t/ds1302-not-displaying-seconds/1007353/7

#include <OneWire.h>
#include <DallasTemperature.h>
#include <virtuabotixRTC.h>  //Libraries needed
#include "Adafruit_GFX.h"    // Hardware-specific library
#include <MCUFRIEND_kbv.h>
#include <SPI.h>
#include <SD.h>
MCUFRIEND_kbv tft;

#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 1000

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

#define BLACK 0x0000
#define LIGHTGREY 0xC618
#define WHITE 0xFFFF
#define RED 0xF800

String nfm = "";  // days to next full moon

#include "Full.c"
#include "Draining.c"
#include "Drained.c"
#include "Empty.c"
#include "Charging.c"

#define SD_CS     48

#define NAMEMATCH ""        // "" matches any name
//#define NAMEMATCH "tiger"   // *tiger*.bmp
//#define PALETTEDEPTH   0     // do not support Palette modes
#define PALETTEDEPTH   8     // support 256-colour Palette

char namebuf[32] = "/";   //BMP files in root directory
//char namebuf[32] = "/bitmaps/";  //BMP directory e.g. files in /bitmaps/*.bmp

File root;
int pathlen;

virtuabotixRTC myRTC(6, 7, 8);

uint8_t  prevDay, prevMonth, prevSec, prevMin, prevHour, prevVolt, prevTemp, prevMoon;
uint16_t prevYear;

void setup() {
  Serial.begin(9600);
  sensors.begin();
  tft.reset();
  tft.begin(0x9488);
  tft.setRotation(1);
  myRTC.setDS1302Time(00, 54, 17, 04, 30, 06, 2022);
  tft.fillScreen(BLACK);

  tft.setTextSize(3);

  sensors.requestTemperatures();

  tft.drawLine(0, 27, 480, 27, LIGHTGREY);
  tft.drawLine(0, 28, 480, 28, LIGHTGREY);
  tft.drawLine(240, 0, 240, 28, LIGHTGREY);
  tft.drawLine(241, 0, 241, 28, LIGHTGREY);

  tft.drawLine(0, 288, 480, 288, LIGHTGREY);
  tft.drawLine(0, 289, 480, 289, LIGHTGREY);
  tft.drawLine(240, 288, 240, 320, LIGHTGREY);
  tft.drawLine(241, 289, 241, 320, LIGHTGREY);
  
  bool good = SD.begin(SD_CS);
  if (!good) {
    Serial.print(F("cannot start SD"));
    while (1);
  }
}

void loop() {
  myRTC.updateTime();
  sensors.requestTemperatures();

  if (myRTC.dayofmonth != prevDay)
  {
    prevDay = myRTC.dayofmonth;

    tft.setCursor(5, 3);
    tft.fillRect(5, 3, 33, 21, BLACK);
    if (myRTC.dayofmonth < 10)
    {
      tft.print("0");
      tft.print(myRTC.dayofmonth);
    }
    else
    {
      tft.print(myRTC.dayofmonth);
    }
  }
  tft.setCursor(41, 3);
  tft.print("/");
  if (myRTC.month != prevMonth)
  {
    prevMonth = myRTC.month;
    tft.setCursor(59, 3);
    tft.fillRect(59, 3, 33, 21, BLACK);
    if (myRTC.month < 10) {
      tft.print("0");
      tft.print(myRTC.month);
    }
    else {
      tft.print(myRTC.month);
    }
  }
  tft.setCursor(95, 3);
  tft.print("/");
  if (myRTC.year != prevYear)
  {
    prevYear = myRTC.year;
    tft.setCursor(113, 3);
    tft.fillRect(113, 3, 70, 21, BLACK);
    tft.print(myRTC.year);
  }
  if (myRTC.hours != prevHour)
  {
    prevHour = myRTC.hours;
    tft.setCursor(246, 3);
    tft.fillRect(246, 3, 33, 21, BLACK);
    if (myRTC.hours < 10) {
      tft.print("0");
      tft.print(myRTC.hours);
    }
    else {
      tft.print(myRTC.hours);
    }
  }
  tft.setCursor(279, 3);
  tft.print(":");
  if (myRTC.minutes != prevMin)
  {
    prevMin = myRTC.minutes;
    tft.setCursor(295, 3);
    tft.fillRect(295, 3, 33, 21, BLACK);
    if (myRTC.minutes < 10) {
      tft.print("0");
      tft.print(myRTC.minutes);
    }
    else {
      tft.print(myRTC.minutes);
    }
  }
  tft.setCursor(329, 3);
  tft.print(":");
  if (myRTC.seconds != prevSec)
  {
    prevSec = myRTC.seconds;
    tft.setCursor(345, 3);
    tft.fillRect(345, 3, 33, 21, BLACK);
    if (myRTC.seconds < 10) {
      tft.print("0");
      tft.print(myRTC.seconds);
    }
    else {
      tft.print(myRTC.seconds);
    }
  }
  int sensor = analogRead(A1);
  float voltage = (sensor * 5.0) / 1023;
  tft.setTextSize(3);
  if (voltage != prevVolt)
  {
    prevVolt = voltage;
    //tft.fillRect(3, 33, 70, 21, BLACK);
    //tft.setCursor(3, 33);
    Serial.println(voltage);
    battery();
  }
  if (sensors.getTempCByIndex(0) != prevTemp)
  {
    prevTemp = sensors.getTempCByIndex(0);
    tft.setCursor(5, 294);
    tft.fillRect(5, 294, 90, 21, BLACK);
    tft.print(sensors.getTempCByIndex(0));
  }
  if (moonPhase != prevMoon)
  {
    prevMoon = moonPhase;
    moonPhase();
  }
}

double julianDate(int y, int m, int d) {
  // convert a date to a Julian Date}
  int mm, yy;
  double k1, k2, k3;
  double j;

  yy = y - int((12 - m) / 10);
  mm = m + 9;
  if (mm >= 12) {
    mm = mm - 12;
  }
  k1 = 365.25 * (yy + 4172);
  k2 = int((30.6001 * mm) + 0.5);
  k3 = int((((yy / 100) + 4) * 0.75) - 38);
  j = k1 + k2 + d + 59;
  j = j - k3; // j is the Julian date at 12h UT (Universal Time)

  return j;
}

int moon_phase() {
  // calculates the age of the moon phase(0 to 7)
  // there are eight stages, 0 is full moon and 4 is a new moon
//  DateTime now = RTC.now();
  double jd = 0; // Julian Date
  double ed = 0; //days elapsed since start of full moon
  int b = 0;
  jd = julianDate(myRTC.year, myRTC.month, myRTC.dayofmonth);
  //jd = julianDate(1972,1,1); // used to debug this is a new moon
  jd = int(jd - 2244116.75); // start at Jan 1 1972
  jd /= 29.53; // divide by the moon cycle
  b = jd;
  jd -= b; // leaves the fractional part of jd
  ed = jd * 29.53; // days elapsed this month
  nfm = String((int(29.53 - ed))); // days to next full moon
  b = jd * 8 + 0.5;
  b = b & 7;
  return b;

}

uint8_t ret;

void moonPhase() {
  int mp = moon_phase();
  Serial.print("Returned Moon Phase :"); Serial.println(mp);
  switch (mp) {
    case 0:
      ret = showBMP("/01.bmp", 3, 33);
      Serial.println("Printing full moon phase");
      break;
    case 1:
      ret = showBMP("/02.bmp", 3, 33);
      Serial.println("Printing Waning Gibbous phase");
      break;
    case 2:
      ret = showBMP("/03.bmp", 3, 33);
      Serial.println("Printing Third Quarter phase");
      break;
    case 3:
      ret = showBMP("/04.bmp", 3, 33);
      Serial.println("Printing Waning Crescent phase");
      break;
    case 4:
      ret = showBMP("/05.bmp", 3, 33);
      Serial.println("Printing new moon phase");
      break;
    case 5:
      ret = showBMP("/06.bmp", 3, 33);
      Serial.println("Printing Waxing Crescent phase");
      break;
    case 6:
      ret = showBMP("/07.bmp", 3, 33);
      Serial.println("Printing First Quarter phase");
      break;
    case 7:
      ret = showBMP("/08.bmp", 3, 33);
      Serial.println("Printing Waxing Gibbous phase");
      break;
  }
  const char* newNfm = (const char*) nfm.c_str();
}

void battery () {
  int sensor = analogRead(A1);
  Serial.println(sensor);
  float voltage = (sensor * 5.0) / 1023;
  Serial.println(voltage);
  if (voltage > 3.00) {
    tft.drawRGBBitmap(435, 3, Charging, 36, 21);
  }
  if ((voltage <= 3.00) && (voltage > 2.35)) {
    tft.drawRGBBitmap(435, 3, Full, 36, 21);
  }
  if ((voltage <= 2.35) && (voltage > 1.70)) {
    tft.drawRGBBitmap(435, 3, Draining, 36, 21);
  }
  if ((voltage <= 1.70) && (voltage > 1.00)) {
    tft.drawRGBBitmap(435, 3, Drained, 36, 21);
  }
  if (voltage <= 1.00) {
    tft.drawRGBBitmap(435, 3, Empty, 36, 21);
  }
}

#define BMPIMAGEOFFSET 54

#define BUFFPIXEL      20

uint16_t read16(File& f) {
  uint16_t result;         // read little-endian
  f.read((uint8_t*)&result, sizeof(result));
  return result;
}

uint32_t read32(File& f) {
  uint32_t result;
  f.read((uint8_t*)&result, sizeof(result));
  return result;
}

uint8_t showBMP(char *nm, int x, int y)
{
  File bmpFile;
  int bmpWidth, bmpHeight;    // W+H in pixels
  uint8_t bmpDepth;           // Bit depth (currently must be 24, 16, 8, 4, 1)
  uint32_t bmpImageoffset;    // Start of image data in file
  uint32_t rowSize;           // Not always = bmpWidth; may have padding
  uint8_t sdbuffer[3 * BUFFPIXEL];    // pixel in buffer (R+G+B per pixel)
  uint16_t lcdbuffer[(1 << PALETTEDEPTH) + BUFFPIXEL], *palette = NULL;
  uint8_t bitmask, bitshift;
  boolean flip = true;        // BMP is stored bottom-to-top
  int w, h, row, col, lcdbufsiz = (1 << PALETTEDEPTH) + BUFFPIXEL, buffidx;
  uint32_t pos;               // seek position
  boolean is565 = false;      //

  uint16_t bmpID;
  uint16_t n;                 // blocks read
  uint8_t ret;

  if ((x >= tft.width()) || (y >= tft.height()))
    return 1;               // off screen

  bmpFile = SD.open(nm);      // Parse BMP header
  bmpID = read16(bmpFile);    // BMP signature
  (void) read32(bmpFile);     // Read & ignore file size
  (void) read32(bmpFile);     // Read & ignore creator bytes
  bmpImageoffset = read32(bmpFile);       // Start of image data
  (void) read32(bmpFile);     // Read & ignore DIB header size
  bmpWidth = read32(bmpFile);
  bmpHeight = read32(bmpFile);
  n = read16(bmpFile);        // # planes -- must be '1'
  bmpDepth = read16(bmpFile); // bits per pixel
  pos = read32(bmpFile);      // format
  if (bmpID != 0x4D42) ret = 2; // bad ID
  else if (n != 1) ret = 3;   // too many planes
  else if (pos != 0 && pos != 3) ret = 4; // format: 0 = uncompressed, 3 = 565
  else if (bmpDepth < 16 && bmpDepth > PALETTEDEPTH) ret = 5; // palette
  else {
    bool first = true;
    is565 = (pos == 3);               // ?already in 16-bit format
    // BMP rows are padded (if needed) to 4-byte boundary
    rowSize = (bmpWidth * bmpDepth / 8 + 3) & ~3;
    if (bmpHeight < 0) {              // If negative, image is in top-down order.
      bmpHeight = -bmpHeight;
      flip = false;
    }

    w = bmpWidth;
    h = bmpHeight;
    if ((x + w) >= tft.width())       // Crop area to be loaded
      w = tft.width() - x;
    if ((y + h) >= tft.height())      //
      h = tft.height() - y;

    if (bmpDepth <= PALETTEDEPTH) {   // these modes have separate palette
      //bmpFile.seek(BMPIMAGEOFFSET); //palette is always @ 54
      bmpFile.seek(bmpImageoffset - (4 << bmpDepth)); //54 for regular, diff for colorsimportant
      bitmask = 0xFF;
      if (bmpDepth < 8)
        bitmask >>= bmpDepth;
      bitshift = 8 - bmpDepth;
      n = 1 << bmpDepth;
      lcdbufsiz -= n;
      palette = lcdbuffer + lcdbufsiz;
      for (col = 0; col < n; col++) {
        pos = read32(bmpFile);    //map palette to 5-6-5
        palette[col] = ((pos & 0x0000F8) >> 3) | ((pos & 0x00FC00) >> 5) | ((pos & 0xF80000) >> 8);
      }
    }

    // Set TFT address window to clipped image bounds
    tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
    for (row = 0; row < h; row++) { // For each scanline...
      // Seek to start of scan line.  It might seem labor-
      // intensive to be doing this on every line, but this
      // method covers a lot of gritty details like cropping
      // and scanline padding.  Also, the seek only takes
      // place if the file position actually needs to change
      // (avoids a lot of cluster math in SD library).
      uint8_t r, g, b, *sdptr;
      int lcdidx, lcdleft;
      if (flip)   // Bitmap is stored bottom-to-top order (normal BMP)
        pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
      else        // Bitmap is stored top-to-bottom
        pos = bmpImageoffset + row * rowSize;
      if (bmpFile.position() != pos) { // Need seek?
        bmpFile.seek(pos);
        buffidx = sizeof(sdbuffer); // Force buffer reload
      }

      for (col = 0; col < w; ) {  //pixels in row
        lcdleft = w - col;
        if (lcdleft > lcdbufsiz) lcdleft = lcdbufsiz;
        for (lcdidx = 0; lcdidx < lcdleft; lcdidx++) { // buffer at a time
          uint16_t color;
          // Time to read more pixel data?
          if (buffidx >= sizeof(sdbuffer)) { // Indeed
            bmpFile.read(sdbuffer, sizeof(sdbuffer));
            buffidx = 0; // Set index to beginning
            r = 0;
          }
          switch (bmpDepth) {          // Convert pixel from BMP to TFT format
            case 24:
              b = sdbuffer[buffidx++];
              g = sdbuffer[buffidx++];
              r = sdbuffer[buffidx++];
              color = tft.color565(r, g, b);
              break;
            case 16:
              b = sdbuffer[buffidx++];
              r = sdbuffer[buffidx++];
              if (is565)
                color = (r << 8) | (b);
              else
                color = (r << 9) | ((b & 0xE0) << 1) | (b & 0x1F);
              break;
            case 1:
            case 4:
            case 8:
              if (r == 0)
                b = sdbuffer[buffidx++], r = 8;
              color = palette[(b >> bitshift) & bitmask];
              r -= bmpDepth;
              b <<= bmpDepth;
              break;
          }
          lcdbuffer[lcdidx] = color;

        }
        tft.pushColors(lcdbuffer, lcdidx, first);
        first = false;
        col += lcdidx;
      }           // end cols
    }               // end rows
    tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1); //restore full screen
    ret = 0;        // good render
  }
  bmpFile.close();
  return (ret);
}

@red_car
I think the issue is here somewhere?? Or is it because of the case statements?

if (moonPhase != prevMoon)
  {
    prevMoon = moonPhase;
    moonPhase();
  }

What part of the code is supposed to cause the display to update once a second?

@Idahowalker

if (myRTC.seconds != prevSec)
  {
    prevSec = myRTC.seconds;
    tft.setCursor(345, 3);
    tft.fillRect(345, 3, 33, 21, BLACK);
    if (myRTC.seconds < 10) {
      tft.print("0");
      tft.print(myRTC.seconds);
    }
    else {
      tft.print(myRTC.seconds);
    }
  }

How does the code if (myRTC.seconds != prevSec) determine that only a single second has passed?

@Idahowalker
Because my RTC is constantly being refreshed, and when I read the seconds and they have changed since last time I read them, it will reprint them. If they HAVEN'T changed, it won't reprint them.

@Idahowalker
This link explains it
https://forum.arduino.cc/t/ds1302-not-displaying-seconds/1007353/7

unsigned long pastMillis = millis();
unsigned long timetoupdate = 1000;

voiding loopy()
{

if ( (millis() - pastMillis) >= timetoupdate )
{
////NOW DO THE UPDATE after 1000 millis seconds
///after the display has been updated 
pastMillis= pastMillis=millis();
}

}

Please look at the link, as that doesn't work. I have tried a few things, and my code in post#1 of this thread works if I remove the moon phases.

Those consume a lot of clock ticks, as a note. Anyways good luck.

@Idahowalker
2 seconds worth??????

Your loop generally looks quite heavy.
Some things don't need to be done every loop iteration, for example getting the bitmap for the moon phase.
You can use the Arduino TimeLib.h and synchronise occasionally with the RTC instead of reading the RTC directly each time.
If you can isolate the code which writes the time to the display, you might be able to do that part in a timer interrupt service routine. The timer triggers the ISR say 4 times a second which checks if the second is new and, if so, writes to the display. That makes it insensitive to what happens in the loop.

@6v6gt
I've never done a timer interrupt service routine, do you know of any REALLY GOOD tutorials on HOW to use that? A example is nice, but how does it work??

You can look for examples using the arduino timer1 (TimerOne) library. There is one at the end of this reference. Arduino Playground - Timer1 In this case the function callback is an ISR. Be aware that not all activities can be done in an ISR. For example, you should not use Serial.print() in an ISR.

It works by configuring one of the hardware timers to trigger a function in your code. The routine attachInterrupt() does that configuration.

This is indeed the problem. You do not have a variable called moonPhase in your program... moonPhase is a function. I expect you are getting compiler warnings about this. It is probably getting called every loop at the moment.

You do not need a separate check for this, as moon phase is date dependant you can just add the call to moonPhase() when the date changes.

The rendering of the bitmap looks fairly intensive, so potentially takes longer than a second... but if you are only doing it at mid night when the day changes then I expect this won't really matter.

EDIT: Compiler warnings below. You should make sure you have them turned on in Preferences.

/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_490287/Blink.ino: In function 'void loop()':
/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_490287/Blink.ino:15:20: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   if (moonPhase != prevMoon)
                    ^~~~~~~~
/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_490287/Blink.ino:17:16: warning: invalid conversion from 'void (*)()' to 'int' [-fpermissive]
     prevMoon = moonPhase;
                ^~~~~~~~~

@red_car

 if (myRTC.dayofmonth != prevDay)
    {
      prevDay = myRTC.dayofmonth;

      tft.setCursor(5, 3);
      tft.fillRect(5, 3, 33, 21, BLACK);
      if (myRTC.dayofmonth < 10)
      {
        tft.print("0");
        tft.print(myRTC.dayofmonth);
        moonPhase();
      }
      else
      {
        tft.print(myRTC.dayofmonth);
        moonPhase();
      }
    }

This fixed it!
Thanks so much for your help!

Tidier to call outside the if... it always gets called.

 if (myRTC.dayofmonth != prevDay)
    {
      prevDay = myRTC.dayofmonth;

      tft.setCursor(5, 3);
      tft.fillRect(5, 3, 33, 21, BLACK);
      if (myRTC.dayofmonth < 10)
      {
        tft.print("0");
        tft.print(myRTC.dayofmonth);
      }
      else
      {
        tft.print(myRTC.dayofmonth);
      }
      moonPhase();
    }

@6v6gt @Idahowalker @red_car
I have added buttons to my screen (actually using it as a touchscreen), only if I follow post #16, it does not work at all!!
It doesn't refresh anything when it actually changes UNLESS I press one of the buttons, so I removed it. This is the trimmed down code:

#include "Full.c"
#include "Draining.c"
#include "Drained.c"
#include "Empty.c"
#include "Charging.c"

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 1000

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

#define USE_TOUCH    0x2046

#include <virtuabotixRTC.h> //Libraries needed

virtuabotixRTC myRTC(6, 7, 8);

#include "Adafruit_GFX.h"    // Hardware-specific library
#include <MCUFRIEND_kbv.h>
#include <SPI.h>
#include <SD.h>
MCUFRIEND_kbv tft;       //
#define TFT_BEGIN()    tft.begin(0x9488)

#include <XPT2046_Touchscreen.h>           // Hardware SPI library
char *name = "Shield XPT2046 Calibration";  //edit name of shield
const int TS_LANDSCAPE = 1; //XPT2046_TouchScreen.h
const int TS_LEFT = 261, TS_RT = 3770, TS_TOP = 3869, TS_BOT = 296;
#define XPT_CS  53      // MEGA2560 SHIELD 
#define XPT_IRQ 255     // XPT2046 library does not like IRQ
XPT2046_Touchscreen ts(XPT_CS, XPT_IRQ);

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
  bool pressed = ts.touched();
  if (pressed) {
    TS_Point p = ts.getPoint();
    if (TS_LANDSCAPE) mapxy(p.y, p.x);
    else mapxy(p.x, p.y);
  }
  return pressed;
}

void Touch_init(void)
{
  ts.begin();
}

void mapxy(int x, int y)                //maps ADC to pixel_x, pixel_y
{
  int aspect = tft.getRotation();     //LANDSCAPE
  int tft_width = tft.width();
  int tft_height = tft.height();
  switch (aspect & 3) {
    case 0:      //PORTRAIT
      pixel_x = map(x, TS_LEFT, TS_RT, 0, tft_width);
      pixel_y = map(y, TS_TOP, TS_BOT, 0, tft_height);
      break;
    case 1:      //LANDSCAPE
      pixel_x = map(y, TS_TOP, TS_BOT, 0, tft_width);
      pixel_y = map(x, TS_RT, TS_LEFT, 0, tft_height);
      break;
    case 2:      //PORTRAIT REV
      pixel_x = map(x, TS_RT, TS_LEFT, 0, tft_width);
      pixel_y = map(y, TS_BOT, TS_TOP, 0, tft_height);
      break;
    case 3:      //LANDSCAPE REV
      pixel_x = map(y, TS_BOT, TS_TOP, 0, tft_width);
      pixel_y = map(x, TS_LEFT, TS_RT, 0, tft_height);
      break;
  }
}

Adafruit_GFX_Button temp_btn, humidity_btn, home_btn, more_btn;

#define BLACK 0x0000       /*   0,   0,   0 */
#define NAVY 0x000F        /*   0,   0, 128 */
#define DARKGREEN 0x03E0   /*   0, 128,   0 */
#define DARKCYAN 0x03EF    /*   0, 128, 128 */
#define MAROON 0x7800      /* 128,   0,   0 */
#define PURPLE 0x780F      /* 128,   0, 128 */
#define OLIVE 0x7BE0       /* 128, 128,   0 */
#define LIGHTGREY 0xC618   /* 192, 192, 192 */
#define DARKGREY 0x7BEF    /* 128, 128, 128 */
#define BLUE 0x001F        /*   0,   0, 255 */
#define GREEN 0x07E0       /*   0, 255,   0 */
#define CYAN 0x07FF        /*   0, 255, 255 */
#define RED 0xF800         /* 255,   0,   0 */
#define MAGENTA 0xF81F     /* 255,   0, 255 */
#define YELLOW 0xFFE0      /* 255, 255,   0 */
#define WHITE 0xFFFF       /* 255, 255, 255 */
#define ORANGE 0xFD20      /* 255, 165,   0 */
#define GREENYELLOW 0xAFE5 /* 173, 255,  47 */
#define PINK 0xF81F


void setup() {
  Serial.begin(9600);
  sensors.begin();
  TFT_BEGIN();
  Touch_init();
  Serial.println("Calibrate for your Touch Panel");
  tft.setRotation(1);            //PORTRAIT
  tft.fillScreen(BLACK);
  drawHomeScreen();
  home_btn.initButton(&tft,  440, 280, 80, 80, LIGHTGREY, CYAN, BLACK, "HOME", 2);
  home_btn.drawButton(false);
  more_btn.initButton(&tft,  440, 40, 80, 80, LIGHTGREY, CYAN, BLACK, "MORE", 2);
  more_btn.drawButton(false);
  temp_btn.initButton(&tft,  440, 120, 80, 80, LIGHTGREY, CYAN, BLACK, "TEMP", 2);
  humidity_btn.initButton(&tft, 440, 200, 80, 80, LIGHTGREY, CYAN, BLACK, "%", 2);
  temp_btn.drawButton(false);
  humidity_btn.drawButton(false);
  tft.setTextSize(3);
}

void loop() {
  bool down = Touch_getXY();
  home_btn.press(down && home_btn.contains(pixel_x, pixel_y));
  more_btn.press(down && more_btn.contains(pixel_x, pixel_y));
  temp_btn.press(down && temp_btn.contains(pixel_x, pixel_y));
  humidity_btn.press(down && humidity_btn.contains(pixel_x, pixel_y));
  if (home_btn.justReleased()) {
    home_btn.drawButton();
    more_btn.drawButton();
    temp_btn.drawButton();
    humidity_btn.drawButton();
  }
  if (more_btn.justReleased()) {
    more_btn.drawButton();
    home_btn.drawButton();
    temp_btn.drawButton();
    humidity_btn.drawButton();
  }
  if (temp_btn.justReleased()) {
    home_btn.drawButton();
    temp_btn.drawButton();
    more_btn.drawButton();
    humidity_btn.drawButton();
  }
  if (humidity_btn.justReleased()) {
    home_btn.drawButton();
    temp_btn.drawButton();
    humidity_btn.drawButton();
    more_btn.drawButton();
  }
  if (home_btn.justPressed()) {
    home_btn.drawButton(true);
    temp_btn.drawButton(false);
    humidity_btn.drawButton(false);
    more_btn.drawButton(false);
    drawHomeScreen();
  }
  if (more_btn.justPressed()) {
    more_btn.drawButton(true);
    temp_btn.drawButton(false);
    humidity_btn.drawButton(false);
    home_btn.drawButton(false);
    drawmoreScreen();
  }
  if (temp_btn.justPressed()) {
    temp_btn.drawButton(true);
    home_btn.drawButton(false);
    humidity_btn.drawButton(false);
    more_btn.drawButton(false);
    drawDetailScreenTemp();
  }
  if (humidity_btn.justPressed()) {
    humidity_btn.drawButton(true);
    temp_btn.drawButton(false);
    home_btn.drawButton(false);
    more_btn.drawButton(false);
    drawDetailScreenHumidity();
  }
}

void drawHomeScreen() {
  sensors.requestTemperatures();

  tft.fillScreen(BLACK);

  tft.drawLine(0, 27, 480, 27, LIGHTGREY);
  tft.drawLine(0, 28, 480, 28, LIGHTGREY);
  tft.drawLine(200, 0, 200, 28, LIGHTGREY);
  tft.drawLine(201, 0, 201, 28, LIGHTGREY);

  tft.drawLine(0, 288, 480, 288, LIGHTGREY);
  tft.drawLine(0, 289, 480, 289, LIGHTGREY);
  tft.drawLine(200, 288, 200, 320, LIGHTGREY);
  tft.drawLine(201, 289, 201, 320, LIGHTGREY);

  myRTC.updateTime();
  tft.setTextSize(3);
  tft.setTextColor(WHITE);
  tft.setCursor(5, 3);
  if (myRTC.dayofmonth < 10)
  {
    tft.print("0");
    tft.print(myRTC.dayofmonth);
  }
  else
  {
    tft.print(myRTC.dayofmonth);
  }
  tft.print("/");
  if (myRTC.month < 10) {
    tft.print("0");
    tft.print(myRTC.month);
  }
  else {
    tft.print(myRTC.month);
  }
  tft.print("/");
  tft.print(myRTC.year);
  tft.setCursor(206, 3);
  if (myRTC.hours < 10) {
    tft.print("0");
    tft.print(myRTC.hours);
  }
  else {
    tft.print(myRTC.hours);
  }
  tft.print(":");
  if (myRTC.minutes < 10) {
    tft.print("0");
    tft.print(myRTC.minutes);
  }
  else {
    tft.print(myRTC.minutes);
  }
  int sensor = analogRead(A1);
  float voltage = (sensor * 5.0) / 1023;
  battery();

  tft.setCursor(5, 294);
  tft.fillRect(5, 294, 90, 21, BLACK);
  tft.print(sensors.getTempCByIndex(0));
  tft.print(" ");
  tft.print((char)247);
  tft.print("C");
}

void drawDetailScreenHumidity() {
  tft.fillScreen(BLACK);

  tft.drawLine(0, 27, 480, 27, LIGHTGREY);
  tft.drawLine(0, 28, 480, 28, LIGHTGREY);
  tft.drawLine(200, 0, 200, 28, LIGHTGREY);
  tft.drawLine(201, 0, 201, 28, LIGHTGREY);

  myRTC.updateTime();
  tft.setTextSize(3);
  tft.setTextColor(WHITE);
  tft.setCursor(5, 3);
  if (myRTC.dayofmonth < 10)
  {
    tft.print("0");
    tft.print(myRTC.dayofmonth);
  }
  else
  {
    tft.print(myRTC.dayofmonth);
  }
  tft.print("/");
  if (myRTC.month < 10) {
    tft.print("0");
    tft.print(myRTC.month);
  }
  else {
    tft.print(myRTC.month);
  }
  tft.print("/");
  tft.print(myRTC.year);
  tft.setCursor(206, 3);
  if (myRTC.hours < 10) {
    tft.print("0");
    tft.print(myRTC.hours);
  }
  else {
    tft.print(myRTC.hours);
  }
  tft.print(":");
  if (myRTC.minutes < 10) {
    tft.print("0");
    tft.print(myRTC.minutes);
  }
  else {
    tft.print(myRTC.minutes);
  }
  int sensor = analogRead(A1);
  float voltage = (sensor * 5.0) / 1023;
  battery();
  tft.drawLine(27, 33, 339, 33, LIGHTGREY);
  tft.drawLine(27, 59, 339, 59, LIGHTGREY);
  tft.drawLine(27, 85, 339, 85, LIGHTGREY);
  tft.drawLine(27, 111, 339, 111, LIGHTGREY);
  tft.drawLine(27, 137, 339, 137, LIGHTGREY);
  tft.drawLine(27, 163, 339, 163, LIGHTGREY);
  tft.drawLine(27, 189, 339, 189, LIGHTGREY);
  tft.drawLine(27, 215, 339, 215, LIGHTGREY);
  tft.drawLine(27, 241, 339, 241, LIGHTGREY);
  tft.drawLine(27, 267, 339, 267, LIGHTGREY);
  tft.drawLine(27, 293, 339, 293, LIGHTGREY);

  tft.drawLine(339, 33, 339, 293, LIGHTGREY);
  tft.drawLine(313, 33, 313, 293, LIGHTGREY);
  tft.drawLine(287, 33, 287, 293, LIGHTGREY);
  tft.drawLine(261, 33, 261, 293, LIGHTGREY);
  tft.drawLine(236, 33, 236, 293, LIGHTGREY);
  tft.drawLine(209, 33, 209, 293, LIGHTGREY);
  tft.drawLine(183, 33, 183, 293, LIGHTGREY);
  tft.drawLine(157, 33, 157, 293, LIGHTGREY);
  tft.drawLine(131, 33, 131, 293, LIGHTGREY);
  tft.drawLine(105, 33, 105, 293, LIGHTGREY);
  tft.drawLine(79, 33, 79, 293, LIGHTGREY);
  tft.drawLine(53, 33, 53, 293, LIGHTGREY);
  tft.drawLine(27, 33, 27, 293, LIGHTGREY);

  tft.setTextSize(1);
  tft.setTextColor(LIGHTGREY);
  tft.setCursor(2, 33);
  tft.print("100%");
  tft.setCursor(6, 59);
  tft.print("90%");
  tft.setCursor(6, 85);
  tft.print("80%");
  tft.setCursor(6, 111);
  tft.print("70%");
  tft.setCursor(6, 137);
  tft.print("60%");
  tft.setCursor(6, 163);
  tft.print("50%");
  tft.setCursor(6, 189);
  tft.print("40%");
  tft.setCursor(6, 215);
  tft.print("30%");
  tft.setCursor(6, 241);
  tft.print("20%");
  tft.setCursor(6, 267);
  tft.print("10%");
  tft.setCursor(6, 293);
  tft.print("0%");

  tft.setTextSize(1);
  tft.setTextColor(LIGHTGREY);
  tft.setCursor(26, 300);
  tft.print("6");
  tft.setCursor(44, 300);
  tft.print("5.5");
  tft.setCursor(77, 300);
  tft.print("5");
  tft.setCursor(96, 300);
  tft.print("4.5");
  tft.setCursor(128, 300);
  tft.print("4");
  tft.setCursor(148, 300);
  tft.print("3.5");
  tft.setCursor(181, 300);
  tft.print("3");
  tft.setCursor(200, 300);
  tft.print("2.5");
  tft.setCursor(234, 300);
  tft.print("2");
  tft.setCursor(253, 300);
  tft.print("1.5");
  tft.setCursor(285, 300);
  tft.print("1");
  tft.setCursor(305, 300);
  tft.print("0.5");
  tft.setCursor(337, 300);
  tft.print("0");
}

void drawmoreScreen() {
  tft.fillScreen(BLACK);
}

void drawDetailScreenTemp() {
  tft.fillScreen(BLACK);

  tft.drawLine(0, 27, 480, 27, LIGHTGREY);
  tft.drawLine(0, 28, 480, 28, LIGHTGREY);
  tft.drawLine(200, 0, 200, 28, LIGHTGREY);
  tft.drawLine(201, 0, 201, 28, LIGHTGREY);

  myRTC.updateTime();
  tft.setTextSize(3);
  tft.setTextColor(WHITE);
  tft.setCursor(5, 3);
  if (myRTC.dayofmonth < 10)
  {
    tft.print("0");
    tft.print(myRTC.dayofmonth);
  }
  else
  {
    tft.print(myRTC.dayofmonth);
  }
  tft.print("/");
  if (myRTC.month < 10) {
    tft.print("0");
    tft.print(myRTC.month);
  }
  else {
    tft.print(myRTC.month);
  }
  tft.print("/");
  tft.print(myRTC.year);
  tft.setCursor(206, 3);
  if (myRTC.hours < 10) {
    tft.print("0");
    tft.print(myRTC.hours);
  }
  else {
    tft.print(myRTC.hours);
  }
  tft.print(":");
  if (myRTC.minutes < 10) {
    tft.print("0");
    tft.print(myRTC.minutes);
  }
  else {
    tft.print(myRTC.minutes);
  }
  int sensor = analogRead(A1);
  float voltage = (sensor * 5.0) / 1023;
  battery();
  tft.drawLine(27, 33, 339, 33, LIGHTGREY);
  tft.drawLine(27, 59, 339, 59, LIGHTGREY);
  tft.drawLine(27, 85, 339, 85, LIGHTGREY);
  tft.drawLine(27, 111, 339, 111, LIGHTGREY);
  tft.drawLine(27, 137, 339, 137, LIGHTGREY);
  tft.drawLine(27, 163, 339, 163, LIGHTGREY);
  tft.drawLine(27, 189, 339, 189, LIGHTGREY);
  tft.drawLine(27, 215, 339, 215, LIGHTGREY);
  tft.drawLine(27, 241, 339, 241, LIGHTGREY);
  tft.drawLine(27, 267, 339, 267, LIGHTGREY);
  tft.drawLine(27, 293, 339, 293, LIGHTGREY);

  tft.drawLine(339, 33, 339, 293, LIGHTGREY);
  tft.drawLine(313, 33, 313, 293, LIGHTGREY);
  tft.drawLine(287, 33, 287, 293, LIGHTGREY);
  tft.drawLine(261, 33, 261, 293, LIGHTGREY);
  tft.drawLine(236, 33, 236, 293, LIGHTGREY);
  tft.drawLine(209, 33, 209, 293, LIGHTGREY);
  tft.drawLine(183, 33, 183, 293, LIGHTGREY);
  tft.drawLine(157, 33, 157, 293, LIGHTGREY);
  tft.drawLine(131, 33, 131, 293, LIGHTGREY);
  tft.drawLine(105, 33, 105, 293, LIGHTGREY);
  tft.drawLine(79, 33, 79, 293, LIGHTGREY);
  tft.drawLine(53, 33, 53, 293, LIGHTGREY);
  tft.drawLine(27, 33, 27, 293, LIGHTGREY);

  tft.setTextSize(1);
  tft.setTextColor(LIGHTGREY);
  tft.setCursor(6, 33);
  tft.print("45");
  tft.print((char)247);
  tft.setCursor(6, 59);
  tft.print("40");
  tft.print((char)247);
  tft.setCursor(6, 85);
  tft.print("35");
  tft.print((char)247);
  tft.setCursor(6, 111);
  tft.print("30");
  tft.print((char)247);
  tft.setCursor(6, 137);
  tft.print("25");
  tft.print((char)247);
  tft.setCursor(6, 163);
  tft.print("20");
  tft.print((char)247);
  tft.setCursor(6, 189);
  tft.print("15");
  tft.print((char)247);
  tft.setCursor(6, 215);
  tft.print("10");
  tft.print((char)247);
  tft.setCursor(6, 241);
  tft.print(" 5");
  tft.print((char)247);
  tft.setCursor(6, 267);
  tft.print(" 0");
  tft.print((char)247);
  tft.setCursor(6, 285);
  tft.print("-5");
  tft.print((char)247);

  tft.setTextSize(1);
  tft.setTextColor(LIGHTGREY);
  tft.setCursor(26, 300);
  tft.print("6");
  tft.setCursor(44, 300);
  tft.print("5.5");
  tft.setCursor(77, 300);
  tft.print("5");
  tft.setCursor(96, 300);
  tft.print("4.5");
  tft.setCursor(128, 300);
  tft.print("4");
  tft.setCursor(148, 300);
  tft.print("3.5");
  tft.setCursor(181, 300);
  tft.print("3");
  tft.setCursor(200, 300);
  tft.print("2.5");
  tft.setCursor(234, 300);
  tft.print("2");
  tft.setCursor(253, 300);
  tft.print("1.5");
  tft.setCursor(285, 300);
  tft.print("1");
  tft.setCursor(305, 300);
  tft.print("0.5");
  tft.setCursor(337, 300);
  tft.print("0");
}

void battery () {
  int sensor = analogRead(A1);
  //Serial.println(sensor);
  float voltage = (sensor * 5.0) / 1023;
  //Serial.println(voltage);
  if (voltage > 3.00) {
    tft.drawRGBBitmap(355, 3, Charging, 36, 21);
  }
  if ((voltage <= 3.00) && (voltage > 2.35)) {
    tft.drawRGBBitmap(355, 3, Full, 36, 21);
  }
  if ((voltage <= 2.35) && (voltage > 1.70)) {
    tft.drawRGBBitmap(355, 3, Draining, 36, 21);
  }
  if ((voltage <= 1.70) && (voltage > 1.00)) {
    tft.drawRGBBitmap(355, 3, Drained, 36, 21);
  }
  if (voltage <= 1.00) {
    tft.drawRGBBitmap(355, 3, Empty, 36, 21);
  }
}

I'm guessing that the justPressed() method cancels itself so that it is true the first time it is called following a button press but won't be true again until the next time the button is pressed.
You have to have your own state variable to hold what should be currently displayed (home or humidity or temperature). Set it when a button is pressed. Read it every time in the loop() and call the appropriate "draw..." function to show the required information on the screen.

@6v6gt

I have no idea what all of this means... How do I do this?