Having issues with Time Function

Hi Folks,
I am kind of lost, so I need some help. The following code is part of my Home Thermostat using the Particle Photon, of course, this is not the whole
code, this part just tells me how long the heater has been ON or OFF.
The problem that I am having is with time.

This line is supposed to tell me how long the heater has been ON, the problem is that sometimes it tells me the correct time, like
4:23, this means 4 minutes and 23 seconds, most of the time, it says like 4:546785, 4 minutes and how many seconds?

Blynk.virtualWrite(V12, "Heater was ON for ", + String(heater_total_timeM), + String(":"), + String(heater_total_timeS), + String("\n")); 

I am using Blynk, that’s why you see: Blynk.virtualWrite…
I am trying to avoid configuring an external RTC like the DS3231, so using the Particle built in RTC will simplify the hardware design.
The first screenshot last line shows my issues, the second screenshot last line is what I expect to see, in this case: 2 minutes and 7 seconds.

Thank you.

bool heater_now, heater_previous = 1;
unsigned long heater_OFFm, heater_OFFs, heater_ONm, heater_ONs, heater_total_timeM, heater_total_timeS, heater_counter;

void heater_ON_OFF() 
{
    heater_now = digitalRead(HEATING);
   if (heater_now != heater_previous)
    {
    heater_previous = heater_now;
    if (heater_now == HIGH) 
    {
    heater_counter++; // Increment counter everytime the heating system is turned ON
    Blynk.virtualWrite(V12, "clr");
    Blynk.virtualWrite(V12,"=============== Heating ON =================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format(Time.now(), "%m/%d/%y at %I:%M:%S %p \n")); //  Cooling System turned ON\n"))); 
    Blynk.virtualWrite(V12, "Heating System Turned ON, \n"); 
    Blynk.virtualWrite(V12, ("Temperature was: ") + String(fTemp,2), "\n"); 
    Blynk.virtualWrite(V12, "Today numbers of ON so far: ", heater_counter, "\n"); 
    heater_ONm  = Time.minute(); // Track how long the heater has been ON.
    heater_ONs = Time.second();
    Blynk.virtualWrite(V12, "\n"); // Insert a blank line. 

    //Blynk.virtualWrite(V3, (Time.format(Time.now(), "%m/%d/%y %I:%M:%S %p Heating System turned ON\n"))); 
    }
   
   if (heater_now == LOW)
    {
    heater_OFFm  = Time.minute(); // Track how long the heater has been OFF
    heater_OFFs  = Time.second();
    Blynk.virtualWrite(V12,"=============== Heater OFF ================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format(Time.now(), "%m/%d/%y at %I:%M:%S %p \n")); 
    Blynk.virtualWrite(V12, "Heating System Turned OFF, \n"); 
    Blynk.virtualWrite(V12, "Temperature was: " + String(fTemp,2), + "\n"); 
    heater_total_timeM = heater_OFFm - heater_ONm;
    heater_total_timeS = heater_OFFs - heater_ONs;
    Blynk.virtualWrite(V12, "Heater was ON for ", + String(heater_total_timeM), + String(":"), + String(heater_total_timeS), + String("\n")); 
   heater_total_timeM = 0;
   heater_total_timeS = 0;
   
    }  
    
  }
}

! !

Just a few notes

When only working with the current time, the better way to write this would be

Time.format("%D at %r \n")

When not providing a distinct date to format the current time will be used automatically (the alternative format is optional for brevity)

But for your actual issue:
You cannot blindly subtract heater_ONm / heaterONs from heater_OFFm / heaterOFFs as these may well render negative numbers.
That’s probably also the reason for your odd numbers as you explicitly declare your heater_total_timeM / heater_total_timeS as unsigned long.

The better approach would be to store the onTime = Time.local() and then calculate secondsRunning = Time.local() - onTime - this will always render a positive result from which you can derive your desired minutes and seconds values.
If you can be sure that your minute count will never exceed 59 you can use Time.format(secondsRunning, "%M:%S").

1 Like

Thank you very much for your quick reply, ScruffR. I implement the changes you suggested, they work. Very much appreciated. By the way, the variables are listed in variables.h header file, that’s why you dont see them here. Here is the updated code:

void heater_ON_OFF() 
{
    heater_now = digitalRead(HEATING);
   if (heater_now != heater_previous)
    {
    heater_previous = heater_now;
    if (heater_now == HIGH) 
    {
    heater_counter++; // Increment counter everytime the heating system is turned ON
    Blynk.virtualWrite(V12, "clr"); // Clear terminal. 
    Blynk.virtualWrite(V12,"=============== Heater ON =================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format("%D at %r, \n"));
    Blynk.virtualWrite(V12, "Heating System Turned ON, \n"); 
    Blynk.virtualWrite(V12, ("Temperature was: ") + String(fTemp,2), "\n"); 
    Blynk.virtualWrite(V12, "Today numbers of ON so far: ", heater_counter, "\n"); 
    heater_ONm  = Time.minute();    // Track ON time in minutes.
    onTime = Time.local();          // Track ON time in seconds
    Blynk.virtualWrite(V12, "\n");  // Insert a blank line. 
    }
    
   if (heater_now == LOW)
    {
    heater_OFFm  = Time.minute(); // Track OFF time in minutes
    Blynk.virtualWrite(V12,"=============== Heater OFF ================\n"); 
    Blynk.virtualWrite(V12, "On ", Time.format("%D at %r, \n"));
    Blynk.virtualWrite(V12, "Heating System Turned OFF, \n"); 
    Blynk.virtualWrite(V12, "Temperature was: " + String(fTemp,2), + "\n"); 
    heater_total_timeM = heater_OFFm - heater_ONm;
    secondsRunning = Time.local() - onTime;
    Blynk.virtualWrite(V12, "Heater was ON for ", + String(heater_total_timeM), + String(":"), + String(Time.format(secondsRunning, "%S")), + String("\n")); 
    heater_OFFm = 0; // Reset heater OFF time tracker
    heater_ONm  = 0; // Reset heater ON time tracker
    onTime      = 0; // Reset ON seconds tracker
    }  
    
  }
   if(Time.hour() == 23 && Time.minute() == 59 && Time.second() >= 55) heater_counter = 0; // Reset Heat counter at mid night. 
}