DS1302 not displaying seconds

I am using this code:

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

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

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

virtuabotixRTC myRTC(6, 7, 8);

unsigned long previousMillis = 0;

const long interval = 5000;

void setup() {
  Serial.begin(9600);
  sensors.begin();
  tft.reset();
  tft.begin(0x9488);
  tft.setRotation(1);
  //myRTC.setDS1302Time(00, 32, 16, 03, 28, 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.setCursor(3, 290);
  tft.fillRect(3, 290, 90, 21, BLACK);
  tft.print(sensors.getTempCByIndex(0));
}

void loop() {
  uint8_t  prevDay, prevMonth, prevSec, prevMin, prevHour, prevVolt;
  uint16_t prevYear;
  myRTC.updateTime();
  sensors.requestTemperatures();

  tft.setCursor(5, 3);
  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.print("/");
  if (myRTC.month != prevMonth)
  {
    prevMonth = myRTC.month;
    tft.fillRect(59, 3, 33, 21, BLACK);
    if (myRTC.month < 10) {
      tft.print("0");
      tft.print(myRTC.month);
    }
    else {
      tft.print(myRTC.month);
    }
  }
  tft.print("/");
  if (myRTC.year != prevYear)
  {
    prevYear = myRTC.year;
    tft.fillRect(112, 3, 70, 21, BLACK);
    tft.print(myRTC.year);
  }
  tft.setCursor(246, 3);
  if (myRTC.hours != prevHour)
  {
    prevHour = myRTC.hours;
    tft.fillRect(246, 3, 33, 21, BLACK);
    if (myRTC.hours < 10) {
      tft.print("0");
      tft.print(myRTC.hours);
    }
    else {
      tft.print(myRTC.hours);
    }
  }
  tft.print(":");
  if (myRTC.minutes != prevMin)
  {
    prevMin = myRTC.minutes;
    tft.fillRect(300, 3, 33, 21, BLACK);
    if (myRTC.minutes < 10) {
      tft.print("0");
      tft.print(myRTC.minutes);
    }
    else {
      tft.print(myRTC.minutes);
    }
  }
    tft.print(":");
    if (myRTC.seconds != prevSec)
    {
    prevSec = myRTC.seconds;
    tft.fillRect(354, 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);
    tft.print(voltage);
  }
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;
    tft.setCursor(3, 290);
    tft.fillRect(3, 290, 90, 21, BLACK);
    tft.print(sensors.getTempCByIndex(0));
  }
}

But it never displays 00 seconds. It displays 59 seconds, changes the minutes, then goes straight to 01 seconds.
Is there a way to fix it? How do I fix it?

Move these to the top of the program. Currently they will get re-initialised every loop.

That fixed one issue, but now I can't even read the seconds because it is printing them on top of the minutes :roll_eyes::roll_eyes::roll_eyes::roll_eyes::roll_eyes:

Did you forget these statements each time you print something...

    tft.setCursor(x, y);

Remember you are not printing everything anymore, so you need to move the cursor for each element.

Ok, that has fixed everything. Seconds are in the right spot, and now it prints 00 seconds!

Solution!

Great that you solved your own problem :thinking:

@red_car
I followed what you said to do and it worked, but I can't say two posts are both solutions, so I copied what you said and marked that as the solution

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.