Elapsed time function

Hi everyone,

i need to know how much time has passed since arduino is on because i have another function that tells me how much time a relay has been turned on. This is necessary because i am using arduino for turning on a heater so i want to know how long the heating has been on since arduino is on. It also helps me debug if arduino for some reason has rebooted or if he got stuck.

I'm actually using this function to know the elapsed time since arduino is on (millis) but i've seen that in a big sketch the count comes up to 59 seconds and then the arduino reboots.

I'm wondering if something is wrong and if it can be done better with less memory usage

Here's the code :

float ore, minuti, secondi, millisecondi;     
unsigned long avanzo;    
unsigned long trascorso;   

void setup() {
 
  Serial.begin(9600);                   // inizializzo la porta seriale da poter usare con il monitor di arduino

}

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;
  Serial.print (ore,0);
  Serial.print (":");
  Serial.print (minuti,0);
  Serial.print (":");
  Serial.println (secondi,0);
 }


void loop() {
 
  TempoTrascorso();

}

Thanks in advance for any help.

but i've seen that in a big sketch the count comes up to 59 seconds and then the arduino reboots.

Perhaps the word "big" is a clue.

I'm unclear why you have "hours" as a floating-point value.

Thanks for the explanation, somebody in the past said the same thing and I personally agree but first i've tried to use the integer as hour and nothing works, second if i use byte i won't be able to keep track of more than 10 days of working hours.

Can you tell me what would be your best solution? Maybe an example could get things easier than just the clue BIG!

Thanks.

Thanks for the explanation, somebody in the past said the same thing and I personally agree but first i've tried to use the integer as hour and nothing works,

Maybe you weren't trying hard enough.

Does the sketch you posted behave as you described, i.e resetting at 59 seconds?

yeah, this:

float ore, minuti, secondi, millisecondi;

is definitely wrong.

byte ore, minuti, secondi;
unsigned int millisecondi;

is better.

If you couldn't get it working, there was a bigger problem. floats are wrong for this.

fix the floats, then post the sketch that fails at 59 seconds. There is no point in posting a sketch that does not have have the problem. We will all just agree that yes, it does not crash. If you can post a small sketch that fails, great. If not, just post the big sketch.

Thanks for the replies.
I don't know why, but the code i posted before was failing as i said, then i went out the ide and back in and now works ok.

I've tried to change to byte from float but no value comes out.
Maybe a working sample could save time and make me understand what's wrong.

An example using bytes:

byte ore, minuti, secondi;
unsigned int millisecondi;
unsigned long avanzo;
unsigned long trascorso;

unsigned long lastPrint;

void setup() {
  Serial.begin(115200);
}

void loop() {
  trascorso = millis();
  if (trascorso - lastPrint > 1000) {
    lastPrint = trascorso;
    ore = (byte)(trascorso / 3600000UL);
    avanzo = trascorso % 3600000UL;
    minuti = (byte)(avanzo / 60000UL);
    avanzo = avanzo % 60000UL;
    secondi = (byte)(avanzo / 1000);
    millisecondi = avanzo % 1000;
    Serial.print(ore);
    Serial.print(F(":"));
    if (minuti < 10) {
      Serial.write('0');
    }
    Serial.print(minuti);
    Serial.print(F(":"));
    if (secondi < 10) {
      Serial.write('0');
    }
    Serial.println (secondi);
  }
}

marcomaroso:
Thanks for the replies.
I don't know why, but the code i posted before was failing as i said, then i went out the ide and back in and now works ok.

I've tried to change to byte from float but no value comes out.
Maybe a working sample could save time and make me understand what's wrong.

Or, you could post your sketch with the changes to byte, and we could tell you why it doesn't work.