How to know total elapsed time while condition true

Hello,
i already use a time function that show in real time how much time has passed since the Arduino is on.

This is it, i just call the void TempoTrascorso() everytime i want the value of “trascorso” to be updated and shown in a decent manner (HH,MM,ss,ms) :

void TempoTrascorso()                   // quando viene richiamata questa void, vengono aggiornati i parziali del tempo trascorso da quando Arduino in funzione.
{
  trascorso=millis();
  ore=int(trascorso/3600000);
  avanzo=trascorso%3600000;
  minuti=int(avanzo/60000);
  avanzo=avanzo%60000;
  secondi=int(avanzo/1000);
  millisecondi=avanzo%1000;
 
}

I have a sketch that turns on or off a Relay for heating and i would like to show in realtime how much time has elapsed for heating purpose and also update it in realtime when it is heating.

In my Loop i use this :

 if (digitalRead(RELAY1) == LOW) tempo_trascorso_riscaldamento();

It recalls a void that should keep track of time elapsed for heating purpose (but it seems to add more time!)

void tempo_trascorso_riscaldamento()    // quando viene richiamata questa void, vengono aggiornati i parziali del tempo trascorso nella durata del riscaldamento.
{
  fine=millis();
  parziale=parziale + (fine-inizio);
  h=int(parziale/3600000);
  over=parziale%3600000;
  m=int(over/60000);
  over=over%60000;
  s=int(over/1000);
  ms=over%1000;
}

Then i have the function that Turns ON the heating :

void accendi_riscaldamento()  // 1 = spento     0 = acceso
{

  float temp = readTemp();                      // legge il valore della temperatura esterna e la memorizza nella var. temp.
  
  //Serial.print("Temp. Esterna = ");
  //Serial.println(temp);
                   
     if (generale == 1 )                                              // se la stuffetta abilitata
          {
            // se fa buio o se la temperatura esterna pari o inferiore al valore della variabile temp_est_minima o se bypass_fotosensore attivo
            if (analogRead(analogInPin3) < 200 || (temp <= temp_est_minima) || (bypass_fotosensore==1))         
              {
                if (digitalRead(RELAY1) == HIGH)                      // se spento
                    {
                      if (tweet_temp == 1)
                          {   
                               sprintf(tweetText, "Accendo riscaldamento!\nTemperatura interna %d\nms %d ", temp_dht ,millis()); 
                               tweet(tweetText);
                          }
                      contatore_accensioni=contatore_accensioni+1;
                      inizio=millis();                                // Tiene in memoria i millisecondi attuali appena letti per contare quanto resta acceso riscaldamento
                      digitalWrite(RELAY1, LOW);                      // accende riscaldamento
                      //Serial.println ("Accendo Relay");
                      delay(1000);
                    }
              }
         }       
}

and the other that turns of the heating :

void spegni_riscaldamento()                   // 1 = spento     0 = acceso
{
    if (digitalRead(RELAY1) == LOW)           // se acceso
    {
                  if (tweet_temp == 1)
                          {         
                             sprintf(tweetText, "Spengo riscaldamento!\nTemperatura interna %d\nms %d ", temp_dht ,millis()); 
                             tweet(tweetText);
                          }
                          
                      fine=millis();                          // memorizza quanti millisecondi sono trascorsi da quano l'arduino si acceso. Utile per capire la durata di accensione riscaldamento
                      tempo_trascorso_riscaldamento();        // aggiorna i parziali della durata di accensione del riscaldamento
                      digitalWrite(RELAY1, HIGH);             // spengo Relay
                      delay(1000);
    }
}

The problem i’m facing is that i have to time counters :

The total time elapsed since Arduino is ON
The total time elapsed while heating is ON

My heating time counter tells me heating time is more than elapsed time since Arduino is ON, this is NOT POSSIBLE!

Surely a formula is not done correctly by me, but which?

Thanks for any help.

My heating time counter tells me heating time is more than elapsed time since Arduino is ON, this is NOT POSSIBLE!

Which variables are you printing that show the problem ?

What you want to do should be simple, At any time you can calculate the time since the Arduino was started or reset by taking the current value of millis(). To know how long the heating has been on subtract the heating start time and subtract it from millis()

Yours snippets of code are confusing.

Please post your whole program

The heating on time has two parts, one is the historical heating time, due to previous
times the heating was on (but not including the current period if currently heating),
and the current heating period (if any).

When the heating is switched off you add the current heating period to the historical
variable:

historical += millis () - on_time;

when the heating is switched on record the on-time:

on_time = millis ();

To calculate the total heating time:

if heating currently on: historical + millis() - on_time
if heating currently off: historical

Thanks guys,

I have already posted the full code here : http://forum.arduino.cc/index.php?topic=368166.msg2538641#msg2538641

Please can you help me?

@MarkT : thanks so much, but i don’t know if it will work for me since the value of time elapsed is shown in HTML by arduino reading a variable, due to what you have written i should implent an if statement to be able to show the write data.

Here is the part showing the result in realtime (Please notice also the part of elapsed time to understand how i did).

If you think there is a better way in doing what i’ve done (i bet there is), please let me know

EthernetClient client = server.available();

  if (client) {
    
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        }
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println(F("HTTP/1.1 200 OK"));
          client.println(F("Content-Type: text/html"));
          client.println();

          //genera la pagina data
          if (readString.indexOf("data") > 0) { //checks for "data" page
            x = x + 1; //page upload counter
            client.print(F("<HTML><HEAD>"));
            client.println(F("<meta name='apple-mobile-web-app-capable' content='yes' />"));
            client.println(F("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />"));
            client.println(F("<body style='font-family:verdana;font-size:10px'/>"));
            
            if (readString.indexOf("datastart") > 0) client.print(F("<meta http-equiv='refresh' content='1'>"));
            //meta-refresh 0 for fast data
            if (readString.indexOf("datafast") > 0) client.print(F("<meta http-equiv='refresh' content='0'>"));

            client.print(F("<title>Monitoraggio casa gatti</title></head><BODY>"));
            client.println(F("<table style='width:100%;font-family:verdana;font-size:10px';align:left>"));
            client.println(F("<tr bgcolor='#b3ffb3'>"));
            client.print(F("<td><b>Aggiornamento n.</b></td><td>"));
            client.print(x); //contatore dei refresh
            client.print(F("</td></tr><tr bgcolor='#b3ffb3'>"));
            client.print(F("<td><b>N. Accensioni</b></td><td>"));
            client.print(contatore_accensioni); //contatore delle accensioni
            client.print(F("</td></tr><tr bgcolor='#ffff1a'>"));
            client.print(F("<td><b>Tempo trascorso H:M:S:ms</b></td><td>"));
            client.print(ore,0);
            client.print(F(":"));
            client.print(minuti,0);
            client.print(F(":"));
            client.print(secondi,0);
            client.print(F(":"));
            client.print(millisecondi,0);
            client.print(F("</td></tr><tr bgcolor='#ffff1a'>"));            
            client.print(F("<td><b>Durata riscaldamento H:M:S:ms</b></td><td>"));
            client.print(h,0);
            client.print(F(":"));
            client.print(m,0);
            client.print(F(":"));
            client.print(s,0);
            client.print(F(":"));
            client.print(ms,0);
            client.print(F("</td></tr><tr bgcolor='#ffff1a'>"));
            client.print(F("<td><b>Costo riscaldamento &euro;</b></td><td>"));

MarkT:
The heating on time has two parts, one is the historical heating time, due to previous
times the heating was on (but not including the current period if currently heating),
and the current heating period (if any).

When the heating is switched off you add the current heating period to the historical
variable:

historical += millis () - on_time;

when the heating is switched on record the on-time:

on_time = millis ();

To calculate the total heating time:

if heating currently on: historical + millis() - on_time
if heating currently off: historical

Ok , i put your code into mine, i have to try it now, but the only part i'm not sure where and how to put is the "if heating currently off: historical"

ok, i tried in so many ways that i'm going crazy.
The elapsed time of heating is shown only when heating stops, it does not show time elapsing while heating.
I thought it could be simple but it doesnt seem so.

Post your whole program as it is now

here it is in attachment

att.txt (30.1 KB)