Time jumps seconds

Hello to everyone,my name is Nick and I’m having the following problem.
I’m building a controller for my aquarium. I wrote the code for the arduino and when I upload it I found out that regarding the time the clock (tft.print) jumps the seconds (2 or 3 seconds each time).
I’ve even checked the loops and I found out that there is a delay in executing them (2 or 3 seconds).
Below is my code. Can anyone tell me why is this happening?
Time keeping is very critical to me, as I will have the controller to turn on lights, co2, fertilize and feed. Thanks in advance

void setup() {
Serial.begin(9600); //gia to RTC
Wire.begin(); //gia to RTC
// Start up the library for temp sensor
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
sensors.setResolution(11);
tft.begin(0x9341); // SDFP5408
tft.fillScreen(BLACK); // Clear screen
tft.setRotation(1); // screen orientation  
while (!Serial) ; // wait until Arduino Serial Monitor opens
//tft.setCursor(0,30);
tft.setTextSize(2);
tft.println("  Waiting 10 seconds ");
tft.println("   for RTC  to Sync ");
delay(10000);//wait for RTC to Respond, if not set time
tft.fillScreen(BLACK); // Clear screen
if (! RTC.isrunning()) {
tft.println("  RTC was NOT running!");
tft.println("   Please check RTC ");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime dt = RTC.now();
Serial.print("===> ARDUINO TIME: "); 
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.print(" @ ");
Serial.print(day());
Serial.print(" - ");
Serial.print(month());
Serial.print(" - ");
Serial.println(year());
Serial.print("Setting Arduino time from RTC: "); 
int cHour = dt.hour();
int cMinute = dt.minute();
int cSec = dt.second();
int cDay = dt.day();
int cMonth = dt.month();
int cYear = 15;
Serial.print(cHour);
Serial.print(":");
Serial.print(cMinute);
Serial.print(":");
Serial.print(cSec);
Serial.print(" @ ");
Serial.print(cDay);
Serial.print(" - ");
Serial.print(cMonth);
Serial.print(" - ");
Serial.println(cYear);
setTime(cHour, cMinute, cSec, cDay, cMonth, cYear); // set time to Saturday 8:29:00am Jan 1 2011
Serial.print("===> ARDUINO TIME: ");
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.print(" @ ");
Serial.print(day());
Serial.print(" - ");
Serial.print(month());
Serial.print(" - ");
Serial.println(year());

tft.fillScreen(BLACK); // Clear screen

//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(RelayT8Lights,0 );
digitalWrite(RelayT5Lights,0 );
digitalWrite(RelayCO2,0 );
digitalWrite(RelayMicroPump,0 );  
digitalWrite(RelayKalioPump,0 ); 
digitalWrite(RelayAlgiPump,0 );
digitalWrite(RelayFuns,0 );
digitalWrite(RelayHeater,0 );

//---( THEN set pins as outputs )----  
pinMode(RelayT8Lights,  OUTPUT);   
pinMode(RelayT5Lights,  OUTPUT);  
pinMode(RelayCO2,       OUTPUT);  
pinMode(RelayMicroPump, OUTPUT);  
pinMode(RelayKalioPump, OUTPUT);  
pinMode(RelayAlgiPump,  OUTPUT);
pinMode(RelayFuns,      OUTPUT);     
pinMode(RelayHeater,    OUTPUT);
delay(3000); //Check that all relay

}
// end setup

void TestAlarm()
{
Serial.println("Alarm triggered!"); 
}

static int cnt = 0;
void  loop(){

Serial.print("Loop #");
Serial.print(cnt);
cnt++;
Serial.print(" | ");
Serial.println(now());
tft.setCursor(50,10);
tft.setTextColor(WHITE,BLACK);
tft.setTextSize(2);
tft.println("AQUARIUM CONTROLLER");
sensors.requestTemperatures(); // Send the command to get temperatures
CurrentTemp  = sensors.getTempCByIndex(0);
tft.setTextColor(YELLOW,BLACK);//set text color & size for  DATE coming from TinyRTC
tft.setTextSize(3);
tft.setCursor(0,35);
DateTime now = RTC.now(); //GRAB DATE AND TIME FROM RTC
tft.print(now.day(), DEC);
tft.print('/');
tft.print(now.month(), DEC);
tft.print('/');
tft.print(now.year(), DEC);
tft.println(' ');
tft.setCursor(175,35);
tft.setTextColor(YELLOW,BLACK); //set color for TIME
tft.setTextSize(3);//set text  size for  TIME coming from TinyRTC
if (now.hour() < 10) {//PRINT A 0 IN FRONT OF HOUR IF LESS THAN 10
tft.print ('0');
tft.print(now.hour(), DEC);
}
else {
  tft.print(now.hour(), DEC);
}
tft.print(':');
if(now.minute() < 10) { //PRINT A 0 IN FRONT OF THE MINUTE IF LESS THAN 10
tft.print('0');
tft.print(now.minute(), DEC);
}
 else {
 tft.print(now.minute(), DEC);
}
 tft.print(':');
if(now.second() < 10) {//PRINT A 0 IN FRONT OF THE SECONDS IF LESS THAN 10
tft.print('0');
tft.print(now.second(), DEC);
}
 else {
 tft.print(now.second(), DEC);
}
tft.println(" ");
digitalTempDisplay (); // ΕΜΦΑΝΙΣΗ ΘΕΡΜΟΚΡΑΣΙΑΣ
// ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΦΩΤΩΝ -CO2 - ΛΙΠΑΝΣΗΣ
//ΑΝΑΜΑ-ΣΒΗΣΙΜΟ Τ5 ΛΑΜΠΩΝ
if ((now.hour() < 12 || now.hour () > 23) || (now.hour() == 12 && now.minute() < 30) || (now.hour () ==22 && now.minute() > 30)) 
{
T5LightsOFF(); 
}  
else //if (now.hour() >= T5OffHour && now.minute()>= T5OffMinute) || (now.hour() < T5OnHour && now.minute() < T5OffMinute)
{
T5LightsON();  
}
//ΑΝΑΜΑ-ΣΒΗΣΙΜΟ Τ8 ΛΑΜΠΩΝ
if (now.hour() < 13  || now.hour () > 22)   //&& now.minute() >= T8OnMinute) && (now.hour() < T8OffHour && now.minute()< T8OffMinute )
{
T8LightsOFF(); 
}  else                                     //if (now.hour() >= T8OffHour && now.minute() >= T8OffMinute)|| (now.hour() < T8OnHour && now.minute() < T8OffMinute) 
{
T8LightsON(); 
}
//ΑΝΑΜΑ-ΣΒΗΣΙΜΟ CO2
if ((now.hour() < 11 || now.hour() > 22) && (now.hour() < 11 && now.minute() == 30) || (now.hour ()==21 && now.minute ()>30))   
{
CO2_OFF(); //ANOIGMA CO2
} 
else                                         //if now.hour() >= CO2OffHour && now.minute() >= CO2OffMinute || (now.hour() < CO2OnHour && now.minute() < CO2OffMinute) && now.minute() >= CO2OffMinute 
{
CO2_ON(); 
}
}
// ΕΜΦΑΝΙΣΗ ΘΕΡΜΟΚΡΑΣΙΑΣ (ΛΕΙΤΟΥΡΓΙΑ ΘΕΡΜΟΣΤΑΤΗ-ΑΝΕΜΙΣΤΗΡΩΝ ΑΝΑΛΟΓΑ ΜΕ ΤΗ ΘΕΡΜΟΚΡΑΣΙΑ)
void digitalTempDisplay ()
{
if (CurrentTemp > HighestTemp){   //ΣΥΝΑΡΤΗΣΗ ΓΙΑ ΝΑ ΦΑΙΝΕΤΑΙ ΤΟ ΧΡΩΜΑ ΤΗΣ ΘΕΡΜΟΚΡΑΣΙΑΣ ΔΙΑΦΟΡΕΤΙΚΟ ΑΝΑΛΟΓΑ ΜΕ ΤΟ ΕΥΡΟΣ ΤΗΣ
tft.setCursor(5,70);
tft.setTextColor(RED,BLACK); 
tft.setTextSize(3);
tft.print("Water Temp:");
tft.setTextSize(3);
tft.println(sensors.getTempCByIndex(0));
HeaterOFF ();
FunsON ();
}
else if (CurrentTemp < LowestTemp)
{
tft.setCursor(5,70);
tft.setTextColor(CYAN,BLACK); //set color for TEMP
tft.setTextSize(3);
tft.print("Water Temp:");
tft.setTextSize(3);
tft.println(sensors.getTempCByIndex(0));
HeaterON();
FunsOFF();  
}
else {
tft.setCursor(5,70);
tft.setTextColor(GREEN,BLACK); //set color for TEMP
tft.setTextSize(3);
tft.print("Water Temp:");
tft.setTextSize(3);
tft.println(sensors.getTempCByIndex(0));
HeaterOFF ();
FunsOFF ();
}
}
// functions to be called when an alarm triggers:
void T5LightsON() //ANAMA T5 LAMPES
{
digitalWrite(RelayT5Lights, 1);
tft.setCursor(85,110);
tft.setTextColor(GREEN,BLACK); //set color for TIME ON
tft.setTextSize(2);//set text  size
tft.print ("T5 lights are ON!");  
}
void T5LightsOFF() //SBHSIMO T5 LAMPES
{
digitalWrite(RelayT5Lights, 0);
tft.setCursor(85,110);
tft.setTextColor(RED,BLACK); //set color for TIME OFF
tft.setTextSize(2);
tft.print("T5 lights are OFF");       
}
void T8LightsON() //ANAMA T8 LAMPES
{
digitalWrite(RelayT8Lights, 1);
tft.setCursor(85,130);
tft.setTextColor(GREEN,BLACK); //set color for TIME ON
tft.setTextSize(2);//set text  size
tft.println ("T8 lights are ON!");  
}
void T8LightsOFF() //SBHSIMO T5 LAMPES
{
digitalWrite(RelayT8Lights, 0);
tft.setCursor(85,130);
tft.setTextColor(RED,BLACK); //set color for TIME OFF
tft.setTextSize(2);
tft.println ("T8 lights are OFF");      
}
void CO2_ON() //ANAMA CO2
{
 digitalWrite(RelayCO2, 1);
 tft.setCursor(85,150);
 tft.setTextColor(GREEN,BLACK); //set color for TIME ON
 tft.setTextSize(2);//set text  size
 tft.println ("CO2 is ON!");
     
}
void CO2_OFF() //SBHSIMO CO2
{
digitalWrite(RelayCO2, 0);
tft.setCursor(85,150);
tft.setTextColor(RED,BLACK); //set color for TIME OFF
tft.setTextSize(2);
tft.println ("CO2 is OFF");
   
}

Hello to everyone,my name is Nick …

Hi, Nick! Welcome to the forum.

My name is also Nick, so I hope you enjoy your stay here. Now, please read the following:

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

Please use code tags.

Read this before posting a programming question

How to use this forum

Ok done!
Thanks for the info

Here is what i mean

Do you really need to display seconds?
Maybe just update the seconds part every 10 seconds.

By the way I don’t think you included all the program - there seems to be definition stuff missing at the top.

And use the autoFormat feature to indent your code. It will make it much easier to read.

And I believe this piece of code (for example)

if(now.minute() < 10) { //PRINT A 0 IN FRONT OF THE MINUTE IF LESS THAN 10
    tft.print('0');
    tft.print(now.minute(), DEC);
}
else {
    tft.print(now.minute(), DEC);
}

can be simplified to

if(now.minute() < 10) { //PRINT A 0 IN FRONT OF THE MINUTE IF LESS THAN 10
    tft.print('0');
}
tft.print(now.minute(), DEC);

…R

I dont need to display seconds. The time is critical to me as I use it to do certain things.
Here is the first part of my code:
#include <SPFD5408_Adafruit_GFX.h>    // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h>
#include <Wire.h>   //gia to RTC
//#include <DS1307RTC.h> //gia to RTC
#include <OneWire.h>
#include <DallasTemperature.h>
#include "RTClib.h" //RTC Library
#include <Time.h>
#include <TimeAlarms.h>

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// 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

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

RTC_DS1307 RTC;

// Data wire is plugged into pin 1 on the Arduino (TEMP SENSOR)
#define ONE_WIRE_BUS 19
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
//temp sensor unique addresses
DeviceAddress WaterThermometer = {0x28, 0xFF, 0xE0, 0xAD, 0x62, 0x15, 0x03, 0xBD};
// ΟΡΙΣΜΟΣ PIN Relay Board
int RelayT5Lights = 22;
int RelayT8Lights = 23;
int RelayCO2 = 24;
int RelayFuns = 25;    
int RelayHeater = 26;    
int RelayMicroPump = 27;
int RelayKalioPump = 28; 
int RelayAlgiPump = 29;
// ΟΡΙΣΜΟΣ ΜΕΓΙΣΤΗΣ,ΤΡΕΧΟΥΣΑΣ ΚΑΙ ΕΛΑΧΙΣΤΗΣ ΘΕΡΜΟΚΡΑΣΙΑΣ
float HighestTemp = 26.5;
float CurrentTemp = 0.0;
float LowestTemp  = 25.5;

here is the setup:

void setup() {
  Serial.begin(9600); //gia to RTC
  Wire.begin(); //gia to RTC
  // Start up the library for temp sensor
  sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
  sensors.setResolution(11);
  tft.begin(0x9341); // SDFP5408
  tft.fillScreen(BLACK); // Clear screen
  tft.setRotation(1); // screen orientation  
  while (!Serial) ; // wait until Arduino Serial Monitor opens
  //tft.setCursor(0,30);
  tft.setTextSize(2);
  tft.println("  Waiting 10 seconds ");
  tft.println("   for RTC  to Sync ");
  delay(10000);//wait for RTC to Respond, if not set time
  tft.fillScreen(BLACK); // Clear screen
  
if (! RTC.isrunning()) {
    tft.println("  RTC was NOT running!");
    tft.println("   Please check RTC ");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
}
      DateTime dt = RTC.now();
      Serial.print("===> ARDUINO TIME: "); 
      Serial.print(hour());
      Serial.print(":");
      Serial.print(minute());
      Serial.print(":");
      Serial.print(second());
      Serial.print(" @ ");
      Serial.print(day());
      Serial.print(" - ");
      Serial.print(month());
      Serial.print(" - ");
      Serial.println(year());
      Serial.print("Setting Arduino time from RTC: "); 
      int cHour = dt.hour();
      int cMinute = dt.minute();
      int cSec = dt.second();
      int cDay = dt.day();
      int cMonth = dt.month();
      int cYear = 15;
      Serial.print(cHour);
      Serial.print(":");
      Serial.print(cMinute);
      Serial.print(":");
      Serial.print(cSec);
      Serial.print(" @ ");
      Serial.print(cDay);
      Serial.print(" - ");
      Serial.print(cMonth);
      Serial.print(" - ");
      Serial.println(cYear);
      setTime(cHour, cMinute, cSec, cDay, cMonth, cYear); // set time to Saturday 8:29:00am Jan 1 2011
      Serial.print("===> ARDUINO TIME: ");
      Serial.print(hour());
      Serial.print(":");
      Serial.print(minute());
      Serial.print(":");
      Serial.print(second());
      Serial.print(" @ ");
      Serial.print(day());
      Serial.print(" - ");
      Serial.print(month());
      Serial.print(" - ");
      Serial.println(year());
    tft.fillScreen(BLACK); // Clear screen
   
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(RelayT8Lights,0 );
  digitalWrite(RelayT5Lights,0 );
  digitalWrite(RelayCO2,0 );
  digitalWrite(RelayMicroPump,0 );  
  digitalWrite(RelayKalioPump,0 ); 
  digitalWrite(RelayAlgiPump,0 );
  digitalWrite(RelayFuns,0 );
  digitalWrite(RelayHeater,0 );
   
  //---( THEN set pins as outputs )----  
  pinMode(RelayT8Lights,  OUTPUT);   
  pinMode(RelayT5Lights,  OUTPUT);  
  pinMode(RelayCO2,       OUTPUT);  
  pinMode(RelayMicroPump, OUTPUT);  
  pinMode(RelayKalioPump, OUTPUT);  
  pinMode(RelayAlgiPump,  OUTPUT);
  pinMode(RelayFuns,      OUTPUT);     
  pinMode(RelayHeater,    OUTPUT);
  //delay(3000); //Check that all relay
  Alarm.timerRepeat(15, TestAlarm);
}
// end setup

void TestAlarm()
{
  Serial.println("Alarm triggered!"); 
}

and here is the rest:

static int cnt = 0;
void  loop(){
      
      Serial.print("Loop #");
      Serial.print(cnt);
      cnt++;
      Serial.print(" | ");
      Serial.println(now());
      tft.setCursor(50,10);
      tft.setTextColor(WHITE,BLACK);
      tft.setTextSize(2);
      tft.println("AQUARIUM CONTROLLER");
      sensors.requestTemperatures(); // Send the command to get temperatures
      CurrentTemp  = sensors.getTempCByIndex(0);
      tft.setTextColor(YELLOW,BLACK);//set text color & size for  DATE coming from TinyRTC
      tft.setTextSize(3);
      tft.setCursor(0,35);
      DateTime now = RTC.now(); //GRAB DATE AND TIME FROM RTC
      tft.print(now.day(), DEC);
      tft.print('/');
      tft.print(now.month(), DEC);
      tft.print('/');
      tft.print(now.year(), DEC);
      tft.println(' ');
      tft.setCursor(175,35);
      tft.setTextColor(YELLOW,BLACK); //set color for TIME
      tft.setTextSize(3);//set text  size for  TIME coming from TinyRTC
      if (now.hour() < 10) {//PRINT A 0 IN FRONT OF HOUR IF LESS THAN 10
      tft.print ('0');
      tft.print(now.hour(), DEC);
      }
      else {
        tft.print(now.hour(), DEC);
      }
      tft.print(':');
      if(now.minute() < 10) { //PRINT A 0 IN FRONT OF THE MINUTE IF LESS THAN 10
      tft.print('0');
      tft.print(now.minute(), DEC);
      }
       else {
       tft.print(now.minute(), DEC);
      }
       tft.print(':');
      if(now.second() < 10) {//PRINT A 0 IN FRONT OF THE SECONDS IF LESS THAN 10
      tft.print('0');
      tft.print(now.second(), DEC);
      }
       else {
       tft.print(now.second(), DEC);
      }
      tft.println(" ");
      digitalTempDisplay (); // ΕΜΦΑΝΙΣΗ ΘΕΡΜΟΚΡΑΣΙΑΣ
// ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΦΩΤΩΝ -CO2 - ΛΙΠΑΝΣΗΣ
//ΑΝΑΜΑ-ΣΒΗΣΙΜΟ Τ5 ΛΑΜΠΩΝ
if (now.hour() < 12 || now.hour () > 23)
{
  T5LightsOFF(); 
}  
  else 
{
    T5LightsON();  
}
//ΑΝΑΜΑ-ΣΒΗΣΙΜΟ Τ8 ΛΑΜΠΩΝ
if (now.hour() < 13  || now.hour () > 22)   
{
  T8LightsOFF(); 
}  else                                     
  {
    T8LightsON(); 
 }
//ΑΝΑΜΑ-ΣΒΗΣΙΜΟ CO2
if (now.hour() < 11 || now.hour() > 22) 
{
  CO2_OFF(); //ANOIGMA CO2
} 
else                                         
  {
    CO2_ON(); 
  }

  if ((now.hour() ==11 && now.minute()==30 && now.second()== 0 ) && (now.hour()==11 && now.minute()<=30 && now.second()==5 ))
{
  MicroDosing();
}
  
}//end loop

void digitalTempDisplay ()
{
  if (CurrentTemp > HighestTemp){   
  tft.setCursor(5,70);
  tft.setTextColor(RED,BLACK); 
  tft.setTextSize(3);
  tft.print("Water Temp:");
  tft.setTextSize(3);
  tft.println(sensors.getTempCByIndex(0));
  HeaterOFF ();
  FunsON ();
  }
  else if (CurrentTemp < LowestTemp)
  {
  tft.setCursor(5,70);
  tft.setTextColor(CYAN,BLACK); //set color for TEMP
  tft.setTextSize(3);
  tft.print("Water Temp:");
  tft.setTextSize(3);
  tft.println(sensors.getTempCByIndex(0));
  HeaterON();
  FunsOFF();  
  }
  else {
  tft.setCursor(5,70);
  tft.setTextColor(GREEN,BLACK); //set color for TEMP
  tft.setTextSize(3);
  tft.print("Water Temp:");
  tft.setTextSize(3);
  tft.println(sensors.getTempCByIndex(0));
  HeaterOFF ();
  FunsOFF ();
}
}
// functions to be called when an alarm triggers:
void T5LightsON() //ANAMA T5 LAMPES
{
  digitalWrite(RelayT5Lights, 1);
  tft.setCursor(85,110);
  tft.setTextColor(GREEN,BLACK); //set color for TIME ON
  tft.setTextSize(2);//set text  size
  tft.print ("T5 lights are ON!");  
  }
void T5LightsOFF() //SBHSIMO T5 LAMPES
{
  digitalWrite(RelayT5Lights, 0);
  tft.setCursor(85,110);
  tft.setTextColor(RED,BLACK); //set color for TIME OFF
  tft.setTextSize(2);
  tft.print("T5 lights are OFF");       
}
void T8LightsON() //ANAMA T8 LAMPES
{
  digitalWrite(RelayT8Lights, 1);
  tft.setCursor(85,130);
  tft.setTextColor(GREEN,BLACK); //set color for TIME ON
  tft.setTextSize(2);//set text  size
  tft.println ("T8 lights are ON!");  
}
void T8LightsOFF() //SBHSIMO T5 LAMPES
{
      digitalWrite(RelayT8Lights, 0);
      tft.setCursor(85,130);
      tft.setTextColor(RED,BLACK); //set color for TIME OFF
      tft.setTextSize(2);
      tft.println ("T8 lights are OFF");      
}
void CO2_ON() //ANAMA CO2
{
       digitalWrite(RelayCO2, 1);
       tft.setCursor(85,150);
       tft.setTextColor(GREEN,BLACK); //set color for TIME ON
       tft.setTextSize(2);//set text  size
       tft.println ("CO2 is ON!");
       
}
void CO2_OFF() //SBHSIMO CO2
{
      digitalWrite(RelayCO2, 0);
      tft.setCursor(85,150);
      tft.setTextColor(RED,BLACK); //set color for TIME OFF
      tft.setTextSize(2);
      tft.println ("CO2 is OFF");
         
}
void MicroDosing() //DOSOMETRIKH IXNOSTOIXEIA
{
       digitalWrite(RelayMicroPump, 1);
       tft.setCursor(10,200);
       tft.setTextColor(GREEN,BLACK); //set color for DOSING
       tft.setTextSize(1);
       tft.println ("Dosing 5ml Micros :"); 
       delay(5000); 
       digitalWrite(RelayMicroPump, 0); 
       tft.setTextColor(RED,BLACK); //set color for DOSING
       tft.setTextSize(1);
       tft.println(" DONE!");      
}
void HeaterON()
{
        digitalWrite(RelayHeater,1);
        tft.setCursor(85,170);
        tft.setTextColor(GREEN,BLACK); //set color for DOSING
        tft.setTextSize(2);
        tft.print ("Heater is ON!");
}
void HeaterOFF()
{
        digitalWrite(RelayHeater,0);
        tft.setCursor(85,170);
        tft.setTextColor(RED,BLACK); //set color for DOSING
        tft.setTextSize(2);
        tft.print ("Heater is OFF");
}
void FunsON ()
{
        digitalWrite(RelayFuns,1);
        tft.setCursor(85,190);
        tft.setTextColor(GREEN,BLACK); //set color for DOSING
        tft.setTextSize(2);
        tft.print ("Funs are ON!"); 
}
void FunsOFF()
{
        digitalWrite(RelayFuns,0);
        tft.setCursor(85,190);
        tft.setTextColor(RED,BLACK); //set color for DOSING
        tft.setTextSize(2);
        tft.print ("Funs are OFF"); 
}

We're not going to piece together different parts of your program. Please post the entire sketch unless they're actually different files.

aarg: We're not going to piece together different parts of your program. Please post the entire sketch unless they're actually different files.

I'm sorry but I cannot post the hole code, as I get an error message that I have exceedeed 9000 characters

Any help please?

I would definitely write yourself a function

void print2digits(int n) {
   tft.print('0' + (n/10) % 10);  
   tft.print('0' + n % 10);  
}

This bit of code

  if ((now.hour() ==11 && now.minute()==30 && now.second()== 0 ) 
  && (now.hour()==11 && now.minute()<=30 && now.second()==5 ))

is problematic in a couple of ways. First, if now.minute == 30, then it’s going to be <=30, so the second set of conditions is redundant.

The way to go “if the time is between 11:30:00 and 11:30:05” is:

  long daysec = (long)now.hour() * 60 * 60 + (long)now.minute() * 60 + (long)now.second();

  if(daysec >= (11L*60*60+30*60) && daysec < (11L*60*60+30*60+5))

I use an long because 246060 is 86400, which is over the limit for a two byte quantity. Notice the L on the numeric constant - this makes it a long values constant. It’s worthwhile making this into a macro

#define HMS(h,m,s) ((long)(h)*60L*60L+(long)(m)*60+(long)(s))

  long daysec = HMS(now.hour(), now.minute(), now.second());

  if(daysec >=HMS(11,30,0) && daysec < HMS(11,30,5))

Once you do that, it’s time to use some named constants rather than magic numbers

const long START_MICRODOSING = HMS(11,30,0);
const long STOP_MICRODOSING = HMS(11,30,5);

  if(daysec >=START_MICRODOSING&& daysec < STOP_MICRODOSING)

That way, if you want to change your schedule, all the numbers you need are at the top of the file in one spot. By removing the const, you might even be able to add some sort of interface to allow you to change the schedule with buttons.

Your other problem is that you spam digital writes to the pins each time the loop runs. It won’t hurt, but it’s good to get into the habit of thinking in terms of state. Rather than “if the time is X, turn on the thing”, it’s “if the time is X, and the thing is not on, then turn on the thing”. This becomes important when turning on the thing is more complicated than just setting a single pin.

In particular, you are writing to your LCD every single time the loop runs. Maybe you should only write to it when what is on the LCD needs to change.

PaulMurrayCbr:
I would definitely write yourself a function

void print2digits(int n) {

tft.print(‘0’ + (n/10) % 10); 
  tft.print(‘0’ + n % 10); 
}




This bit of code 


if ((now.hour() ==11 && now.minute()==30 && now.second()== 0 )
  && (now.hour()==11 && now.minute()<=30 && now.second()==5 ))




is problematic in a couple of ways. First, if now.minute == 30, then it's going to be <=30, so the second set of conditions is redundant.

The way to go "if the time is between 11:30:00 and 11:30:05" is:



long daysec = (long)now.hour() * 60 * 60 + (long)now.minute() * 60 + (long)now.second();

if(daysec >= (11L6060+3060) && daysec < (11L6060+3060+5))




I use an long because 24*60*60 is 86400, which is over the limit for a two byte quantity. Notice the L on the numeric constant - this makes it a long values constant. It's worthwhile making this into a macro



#define HMS(h,m,s) ((long)(h)60L60L+(long)(m)*60+(long)(s))

long daysec = HMS(now.hour(), now.minute(), now.second());

if(daysec >=HMS(11,30,0) && daysec < HMS(11,30,5))




Once you do that, it's time to use some named constants rather than magic numbers



const long START_MICRODOSING = HMS(11,30,0);
const long STOP_MICRODOSING = HMS(11,30,5);

if(daysec >=START_MICRODOSING&& daysec < STOP_MICRODOSING)




That way, if you want to change your schedule, all the numbers you need are at the top of the file in one spot. By removing the const, you might even be able to add some sort of interface to allow you to change the schedule with buttons.

Your other problem is that you spam digital writes to the pins each time the loop runs. It won't hurt, but it's good to get into the habit of thinking in terms of state. Rather than "if the time is X, turn on the thing", it's "if the time is X, and the thing is not on, then turn on the thing". This becomes important when turning on the thing is more complicated than just setting a single pin.

In particular, you are writing to your LCD every single time the loop runs. Maybe you should only write to it when what is on the LCD needs to change.

Thanks for answer. I will look to it.
Another question I have: I use a tiny RTC DS1307 I2C module for time keeping. Is there a possibillity that the problem that I have with seconds has to do that the RTC has a CR2032 battery abd not an LIR2303 that I read on the net?

If the sensors object is a DallasTemperature, then the default behaviour for that library is to hang for 1/2 second or so when reading the sensor rather than acting asynchronously. This will slow down the whole sketch - asynchronous drving would solve that.

The nature of the battery to the RTC is immaterial.

nt7561: I'm sorry but I cannot post the hole code, as I get an error message that I have exceedeed 9000 characters

The add your .ino file as an attachment.

...R

Robin2:
The add your .ino file as an attachment.

…R

Ok here is the .ino file (I have modified it a bit, and now the “jump” is 2 secs

NEW_CODE_20-9-15v2.ino (15.4 KB)

I wonder if this always works properly - I imagine it could miss a second by (perhaps) a few millisecs.

if (dose_H3 == now.hour() && dose_M3 == now.minute() && (dose_S3) == now.second())

If it was my program I would figure out the total number of seconds and then test

if (doseSeconds >= nowSeconds) {

that way you could be sure of the test succeeding

Also, if it was my program I would put all the TFT code into a function that was called every (say) 500 millisecs. That in itself might solve the timing problem.

Code is much easier to debug and maintain if you keep the different activities in separate functions. have a look at Planning and Implementing a Program

...R

Robin2: I wonder if this always works properly - I imagine it could miss a second by (perhaps) a few millisecs.

if (dose_H3 == now.hour() && dose_M3 == now.minute() && (dose_S3) == now.second())

If it was my program I would figure out the total number of seconds and then test

if (doseSeconds >= nowSeconds) {

that way you could be sure of the test succeeding

Also, if it was my program I would put all the TFT code into a function that was called every (say) 500 millisecs. That in itself might solve the timing problem.

Code is much easier to debug and maintain if you keep the different activities in separate functions. have a look at Planning and Implementing a Program

...R

First of all thanks for your reply

Could you please explain a bit more? Thanks in advance

Time code is easily handled by (surprise!) the Time library.

PaulMurrayCbr: ```

define HMS(h,m,s) ((long)(h)*60L*60L+(long)(m)*60+(long)(s))

If you don't like macros (I really don't like macros), then use a function:

long HMS(int h, int m, int s) {
  return ((h*3600L)+(m*60L)+s);
}