How do I make my Mega 2560 check if something has changed ten times a second?

I am displaying the time from a RTC module on a screen, but sometimes the seconds change really quickly, and sometimes they change every 2-3 seconds, which isn't ideal.
Is there a easy way to make my board check if the time has changed multiple times a second?
I also have a lot of other sensors connected which I want to be displayed at the same moment they change as well.
Anyone have some ideas??
Thank you!

P.S. If you need code I can post it.

your code is that information that is

most needed in every case!

except maybe project-guidance

seconds changing quickly that come from a RTC would mean that you do something wrong with actively adjusting the RTC.
or your RTC uses an external clock-signal that is unausable

The one and only purpose of an RTC is counting seconds on high precisision

Maybe your description is not precise enough.

if this is possible depends on what sensors you are using.
There is no generalised solution for this.
You are working on an informatic project and what is most needed in an informatic project is information.

Do you want to finish your projects minimum 2 days faster?
If yes take 20 minutes time to read this and take the recommendations to your heart

best regards Stefan

@StefanL38
I thought the code would put a lot of people off...
It's very heavy.

unsigned long previousMillis = 0;
const long interval = 100;

#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

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

static float prevTemp = 999.99 ; // an unlikely value
float currentTempF = sensors.getTempCByIndex(0) ;

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);
  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() {
  unsigned int IntensityOfLight;

  IntensityOfLight = analogRead(A0);

  myRTC.updateTime();
  sensors.requestTemperatures();

  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(true);
    more_btn.drawButton();
    temp_btn.drawButton();
    humidity_btn.drawButton();
  }
  if (more_btn.justReleased()) {
    more_btn.drawButton(true);
    home_btn.drawButton();
    temp_btn.drawButton();
    humidity_btn.drawButton();
  }
  if (temp_btn.justReleased()) {
    home_btn.drawButton();
    temp_btn.drawButton(true);
    more_btn.drawButton();
    humidity_btn.drawButton();
  }
  if (humidity_btn.justReleased()) {
    home_btn.drawButton();
    temp_btn.drawButton();
    humidity_btn.drawButton(true);
    more_btn.drawButton();
  }
  if (home_btn.justPressed())
  {
    tft.fillScreen(BLACK);
    prevDay = 99;
    prevMonth = 99;
    prevYear = 9999;
    prevHour = 99;
    prevMin = 99;
    prevSec = 99;
    prevTemp = 999.99;
    prevVolt = 9.99;
    drawHomeScreen();
    displayScreenNo = 1;
    home_btn.drawButton(true);
    temp_btn.drawButton(false);
    humidity_btn.drawButton(false);
    more_btn.drawButton(false);
  }
  if (more_btn.justPressed())
  {
    prevDay = 99;
    prevMonth = 99;
    prevYear = 9999;
    prevHour = 99;
    prevMin = 99;
    prevSec = 99;
    prevTemp = 999.99;
    prevVolt = 9.99;
    tft.fillScreen(BLACK);
    drawMoreScreen();
    displayScreenNo = 2;
    more_btn.drawButton(true);
    temp_btn.drawButton(false);
    humidity_btn.drawButton(false);
    home_btn.drawButton(false);
  }
  if (temp_btn.justPressed())
  {
    prevDay = 99;
    prevMonth = 99;
    prevYear = 9999;
    prevHour = 99;
    prevMin = 99;
    prevSec = 99;
    prevTemp = 999.99;
    prevVolt = 9.99;
    tft.fillScreen(BLACK);
    drawDetailScreenTemp();
    displayScreenNo = 3;
    temp_btn.drawButton(true);
    home_btn.drawButton(false);
    humidity_btn.drawButton(false);
    more_btn.drawButton(false);
  }
  if (humidity_btn.justPressed())
  {
    prevDay = 99;
    prevMonth = 99;
    prevYear = 9999;
    prevHour = 99;
    prevMin = 99;
    prevSec = 99;
    prevTemp = 999.99;
    prevVolt = 9.99;
    tft.fillScreen(BLACK);
    drawDetailScreenHumidity();
    displayScreenNo = 4;
    humidity_btn.drawButton(true);
    temp_btn.drawButton(false);
    home_btn.drawButton(false);
    more_btn.drawButton(false);
  }
  static uint32_t prevMillis = 0;
  if (millis() - prevMillis > 1000 ) {
    prevMillis = millis() ;
    if ( displayScreenNo == 1 ) drawDynHomeScreen();
    else if ( displayScreenNo == 2 ) drawDynMoreScreen();
    else if ( displayScreenNo == 3 ) drawDynDetailScreenTemp();
    else if ( displayScreenNo == 4 ) drawDynDetailScreenHumidity();
  }
}

void drawHomeScreen() {
  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);
}

void drawDetailScreenHumidity() {

  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(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() {
  more_btn.drawButton(true);
}

void drawDetailScreenTemp() {

  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(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);
  }
}

void buttons () {
  home_btn.drawButton(false);
  more_btn.drawButton(false);
  temp_btn.drawButton(false);
  humidity_btn.drawButton(false);
}
void drawDynHomeScreen() {
  tft.setTextSize(3);
  tft.setTextColor(WHITE);
  if (myRTC.dayofmonth != prevDay)
  {
    prevDay = myRTC.dayofmonth;
    tft.fillRect(5, 3, 33, 21, BLACK);
    tft.setCursor(5, 3);
    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.fillRect(59, 3, 33, 21, BLACK);
    tft.setCursor(59, 3);
    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.fillRect(113, 3, 70, 21, BLACK);
    tft.setCursor(113, 3);
    tft.print(myRTC.year);
  }
  if (myRTC.hours != prevHour)
  {
    prevHour = myRTC.hours;
    tft.fillRect(206, 3, 33, 21, BLACK);
    tft.setCursor(206, 3);
    if (myRTC.hours < 10) {
      tft.print("0");
      tft.print(myRTC.hours);
    }
    else {
      tft.print(myRTC.hours);
    }
  }
  tft.setCursor(239, 3);
  tft.print(":");
  if (myRTC.minutes != prevMin)
  {
    prevMin = myRTC.minutes;
    tft.fillRect(256, 3, 33, 21, BLACK);
    tft.setCursor(256, 3);
    if (myRTC.minutes < 10) {
      tft.print("0");
      tft.print(myRTC.minutes);
    }
    else {
      tft.print(myRTC.minutes);
    }
  }
  tft.setCursor(290, 3);
  tft.print(":");
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (myRTC.seconds != prevSec)
    {
      prevSec = myRTC.seconds;
      tft.fillRect(306, 3, 33, 21, BLACK);
      tft.setCursor(306, 3);
      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;
  if (voltage != prevVolt)
  {
    prevVolt = voltage;
    battery();
  }
  sensors.requestTemperatures();
  // abs() is a special Arduino function defined in Arduino.h but maybe not on all platforms
  // in case of problems, look at fabs() instead.
  if ( abs( currentTempF - prevTemp ) > 0.01  )  // if the value change is outsid 0.01 degrees C
  {
    prevTemp = currentTempF ;
    tft.setCursor(5, 294);
    tft.fillRect(5, 294, 90, 21, BLACK);
    tft.print( currentTempF );
    tft.print(" ");
    tft.print((char)247);
    tft.print("C");
  }
}

void drawDynDetailScreenHumidity() {
  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;
  if (voltage != prevVolt)
  {
    prevVolt = voltage;
    battery();
  }
}

void drawDynMoreScreen() {
  buttons();
}

void drawDynDetailScreenTemp() {
  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;
  if (voltage != prevVolt)
  {
    prevVolt = voltage;
    battery();
  }
}

I am using a Mega2560.
The sensors I have are being added to one at time. It will be different tomorrow. I am working on making a weather station, so far I have a DS1302 RTC, a DS18B20 waterproof temperature sensor, a 3.95" TFT LCD, working on adding a transceiver to recieve data from the outside unit.

SOP in fault-finding is to prune code to the minimum that still exhibits the problem under discussion.

2 Likes

Hello @proton_aligner

This link says to post the WHOLE code, so I followed it as @StefanL38 said.

"Please supply your complete code in code tags </>"

@StefanL38 @proton_aligner
This is code edition LT.

uint8_t prevSec;
unsigned long previousMillis = 0;
const long interval = 100;
unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (myRTC.seconds != prevSec)
    {
      prevSec = myRTC.seconds;
      tft.fillRect(306, 3, 33, 21, BLACK);
      tft.setCursor(306, 3);
      if (myRTC.seconds < 10) {
        tft.print("0");
        tft.print(myRTC.seconds);
      }
      else {
        tft.print(myRTC.seconds);
      }
    }

Code that exhibits the problem, is whole code, @outbackhut

The same link also says:

it will be much easier to help you if you can write a short program that illustrates the problem you are having and post that.

1 Like

@proton_aligner True. I do not know which part IS breaking things though. I think the problem is in post #6.

The code is pretty well organised in functions. So the full code is not a problem.
I guess the small code-version does not contain the root-cause of the bug.

You are using DS18B20-onewire-sensors for measuring temperature. Which is IMHO a good choice.
Though you have to knwo some details of how the the dallas-temperature-library works.
It takes aprox. 780 milliseconds to finish the command

  sensors.requestTemperatures();

This function-call is blocking.
This means you should not call it more often than nescessary.
There exists a non-blocking variant of the dallasTemperature-library, but me personally I haven't used this variant.

This variant enables to send a request-temperature-command where the function-call is finished withing a few milliseconds and then you can check with another function if the new temperature-values are received in the microcontroller and are "ready to use"

As a quick test you could replace the real function-call

  sensors.requestTemperatures();

with a dummy-call that assignes fixed values

If I have seen right you are not yet using real temperatures
You are just calling

float currentTempF = sensors.getTempCByIndex(0) ;

which seems odd to me to assign a real measured temperature at declaring the variable

So replace your

  sensors.requestTemperatures();

with

  // sensors.requestTemperatures();
 currentTempF = 67.89;

remove the second call to sensors.requestTemperatures();

  if (voltage != prevVolt)
  {
    prevVolt = voltage;
    battery();
  }
  sensors.requestTemperatures();

and then test your code if the seconds are displayed regularly

there might be more things like re-drawing a tft-display might take too much time too
But I guess the main reason is the 780 millisecond long call to
sensors.requestTemperatures();

best regards Stefan

Which makes it all the easier to isolate the area containing the problem, by //'ing various parts out.

I don't actually understand the OP @outbackhut 's very first sentence, quoted below with my emphasis:

What does "really quickly" mean? My first thought was that you meant the seconds change faster than once a second, which is impossible. So I'm wondering what it actually means?- presumably simply means that the seconds as displayed change on time, once a second?

Could the screen updates be buffered? In that case it would indeed be possible to get fast-changing seconds.

1 Like

But then, would those shall we say "bursts" of seconds not themselves be a few seconds apart? It's not that time has sped up....

@proton_aligner

Yes, SOMETIMES it changes every ~750 milliseconds (I checked by printing the seconds to serial and showing the time stamp. It doesn't happen very often though, plus printing to serial slows things down slightly.

Hence post #6.

@SteveThackery
I'm not sure this screen does terribly much buffering.
http://www.lcdwiki.com/3.95inch_Arduino_Display-Mega2560

the code that is posted in post #6 is not a full sketch.
You would have to provide a reduced but still a full sketch that still does reproduce the problem.

I have proposed a step to analyse the problem and I have proposed a solution.

Go back reading the posts to find this solution
or
if reading these posts again is too much work for you
post a new posting that tells how many lines of text you are able to read and understand.

best regards Stefan

Just a thought - if each function in your loop() works by itself but not when all functions are included, the loop repetition interval may have become too long. That you can diagnose by debug printing millis().

When I saw this, my mind immediately thought "english barrier, what he really means is it skips values, like 9:45, 9:48 sometimes, and other times 9:45, 9:45, 9:45, 9:46..." in his Serial Monitor statements, which would be quite likely with a main loop with multiple paths due to if statements sometimes causing many calls between time updates, and sometimes doing nothing much between time updates.
@outbackhut Fix this one thing immediately. Change your Serial.begin(9600) to Serial.begin(115200). That will reduce by >100x the time penalty you pay for calling Serial.print() for debugging. It won't fix your whole problem, necessarily, but it will reduce the pain of adding print statements a whole bunch.
C

It appears that you are updating the display no more often than once per second. In drawDynHomeScreen() you only check the time every tenth of a second and only display it if seconds have changed from the last time it was displayed.

1 Like

Secondly, one thing you can do which will increase complexity BUT reduce CPU load is to map out which sensors you really need to update frequently. In most processes, there are slow-moving parameters and faster-moving parameters. If you're just doing weather monitoring, temperature is of interest minute-by-minute, not second-by-second, so quit hammering away at that CPU-busting Dallas sensor. This can be easily effected in a state machine architecture, but takes a whole lot more scrambling in cascaded if-then-else statements; hence, monolithic read-em-all, write-em-all codes are characteristic of early development. Not slinging stones, just an observation. Kudos to you for your structuring into subroutines for easy calling, now move to the next level with a FSM, using millis() to organize the calls only when they need to be done.
C

user @gfvalvo has written a library that reads out the DS18B20-sensors in a nonblocking way

using this library can reduce the time it takes to request new temperature-values

best regards Stefan