Problem with RTC countdown programming

Hi, I want to make an UV exposure device controlled by an Arduino, but I can't make the countdown, I tried this code:

DateTime now = rtc.now();
  DateTime endtime;
  DateTime left;

  if(settime == false){
  endtime = now.unixtime() + MinuteIns * 60 + SecondsIns;
  settime = true;
  }
  
  left = endtime.unixtime() - now.unixtime();


    Serial.print(left.year(), DEC);
    Serial.print('/');
    Serial.print(left.month(), DEC);
    Serial.print('/');
    Serial.print(left.day(), DEC);
    Serial.print(' ');
    Serial.print(left.hour(), DEC);
    Serial.print(':');
    Serial.print(left.minute(), DEC);
    Serial.print(':');
    Serial.print(left.second(), DEC);
    Serial.println();
  delay(1000);
  }

This is a part of all the code but is here where it fails. If i put 10 in the SecondsIns variable and 0 in MinuteIns, in the serial monitor It shows me this:

2106/2/6 6:28:26
2060/2/8 5:46:40
2060/2/8 5:46:39
2060/2/8 5:46:38
2060/2/8 5:46:37
2060/2/8 5:46:36
2060/2/8 5:46:35

What do you think? Am I doing something wrong?

For starters, using calendar years, months and days to express a time difference? What time library are you using?

I tried this code

Among other code that we need to see.

 left = endtime.unixtime() - now.unixtime();

So, the value assigned to left is a value in seconds. It is a NOT a DateTime value.

aarg:
For starters, using calendar years, months and days to express a time difference? What time library are you using?

I'm Using RTClib.h as library

PaulS:
Among other code that we need to see.

 left = endtime.unixtime() - now.unixtime();

So, the value assigned to left is a value in seconds. It is a NOT a DateTime value.

I put this instead of all the other Serial.print lines

Serial.println(left.unixtime(),DEC);

and it happens the same, the serial monitor show me this, the first value is correct because the time is in seconds but the next iteration is surprisingly wrong :confused:

10
2843443416
2843443415
2843443414
2843443413

The value is not a DateTime value, so it makes no sense to assign it to a variable of type DateTime. The value is a number of seconds, so assign it to a variable of type int or long, depending on the magnitude of the value.

Do NOT expect to get valid data from left when the value assigned to it is garbage.

MrWilliam932:
I'm Using RTClib.h as library

Look at dateCalc.ino for ideas.

@MrWilliam932:

Please post your entire sketch.

odometer:
@MrWilliam932:

Please post your entire sketch.

This is all the code (some variables are in spanish), this time the left.unixtime() tells me the correct time in seconds but i can't show it in minutes and seconds, I think its easy to resolve the code having the time in seconds.

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_Millis rtc;
LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8}; 
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String Tiempo = "00:00";
String Minutos ="00";
String Segundos ="00";
boolean Insolando = false;
byte menupos = 0;
byte TiempoCursor = 0;
byte MinutosIns = 0;
byte SegundosIns = 0;
byte Capas = 0;
char key;
boolean waitforconf = true;
boolean settime = false;
unsigned long endtime;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  lcd.init();
  lcd.backlight();
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  
  
  
  lcd.print("Bienvenido");
  delay(2000);
  lcd.clear();
}

void loop() {
  switch(Insolando){
  case false:
  //Programa de configuración
  while(Insolando == false){
    delay(200);
    switch(menupos){
      case 0:
    lcd.setCursor(0,0);  
    lcd.print("Tiempo de Insol.");
    lcd.setCursor(0,1);
    lcd.print(Tiempo);
    key = keypad.getKey();
    if (key){
   TiempoCursor = constrain(TiempoCursor,0,4);
   if(TiempoCursor== 2){
     TiempoCursor = 3;
     }
     if(key == '*'){ 
       Minutos = Tiempo.substring(0,2);
       MinutosIns = Minutos.toInt();
       Segundos = Tiempo.substring(3);
       SegundosIns = Segundos.toInt();
         if(SegundosIns >= 60 || MinutosIns >= 60 || SegundosIns == 0 && MinutosIns == 0){
           
           lcd.setCursor(0,0);
           lcd.clear();
           lcd.print("Valor Incorrecto");
           delay(1000);
           
           Tiempo = "00:00";  
           TiempoCursor = 0;
           menupos = 0;
           lcd.clear();
           break;
         }
       TiempoCursor = 0;
       menupos = 1;
       lcd.clear();
       break;
       }
       else if(key == '#'){
       Tiempo = "00:00";  
       TiempoCursor = 0;
       menupos = 0;
       lcd.clear();
       break;
       }
   Tiempo.setCharAt(TiempoCursor, key);
     TiempoCursor++; 
    }
      break;
    case 1:
      lcd.setCursor(0,0);
      lcd.print("Cara:1->Inf.");
      lcd.setCursor(0,1);
      lcd.print("2->Sup 3->Ambas");
      key = keypad.getKey();
      if (key){
        
        lcd.clear();
        if(key == '1'){
          Capas = 1;
          menupos = 2;
          }
         else if(key == '2'){
          Capas = 2;
          menupos = 2;
          }
          else if(key == '3'){
          Capas = 3;
          menupos = 2;
          }
          else if(key == '#'){
          
          menupos = 0;
          
          }
          else{
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Selecciona un");
          lcd.setCursor(0,1);
            lcd.print("valor correcto");
          delay(2000);
          lcd.clear();
          
         menupos=1;
         Capas = 0;
          
            }
        }
    break;
  case 2:
    lcd.setCursor(0,0);
    lcd.print(MinutosIns);
    lcd.print(" Mins y ");
    lcd.print(SegundosIns);
    lcd.print(" Segs");
    lcd.setCursor(0,1);
    if(Capas == 1){
      lcd.print("Capa Inferior");
      }
      else if (Capas == 2){
        lcd.print("Capa Superior");
        }
        else if (Capas == 3){
        lcd.print("Doble Capa");
        }
        delay(2500);
        lcd.clear();
        waitforconf = true;
        while(waitforconf == true){
        
        lcd.setCursor(0,0);
        lcd.print("ACEPTAR?");
        lcd.setCursor(0,1);
        lcd.print("*->Si #->Atras");
        
        
        key = keypad.getKey();
      if (key){
        lcd.clear();
        if(key == '*'){
          waitforconf = false;
          Insolando = true;
          break;
          }
         else if(key == '#'){
          waitforconf = false;
          menupos = 1;
          }
        }
        }
  break;  
      
  }
    }
    break;
    lcd.clear();
  case true:
  while(Insolando == true){

DateTime now = rtc.now();


  
if(settime == false){
  endtime = now.unixtime() + MinutosIns * 60 + SegundosIns;
  settime = true;
  }
  
  DateTime left = endtime - now.unixtime();
  Serial.println(left.unixtime(),DEC);
  Serial.println(endtime);
  Serial.println(now.unixtime());
  
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(left.minute());
  lcd.print(':');
  lcd.print(left.second());
//  lcd.print("Insolando...");
//  lcd.setCursor(0,1);
//  lcd.print(left.minute());
//  lcd.print(':');
//  lcd.print(left.second());

//if(left.unixtime() == 0){
//  settime = false;
//  Tiempo = "00:00";
//  Minutos ="00";
//Segundos ="00";
//Insolando = false;
//menupos = 0;
//TiempoCursor = 0;
//MinutosIns = 0;
//SegundosIns = 0;
//Capas = 0;
//waitforconf = true;
//break;
//  }
  delay(1000);
  break;
  }
  
}
}