Code not behaving as expected, I'm doing something wrong

I got this code, below, from sparkfun github.
a simple timer that displays the seconds, minutes and hours
I have tried playing around with it in resetting everything to 0
at a giving time.

An example is that I wanted everything to reset back to zero
when then minutes is 1
but nothing happens
it just keeps going.

I have tried setting runTime = 0;
startingRuntime = millis(); which produces some random behaviour
and minutes = 0; etc etc but nothing seems to work..
I'll admit that i haven't quite gotten a good grasp of using millis();
thus that might be the problem.
any help ?

BEGIN CODE**
int startingRuntime = 0;
unsigned long runTime = 0;
int hours = runTime / (60 * 60 * 1000L);
int minutes = runTime / (60 * 1000L);
int seconds = runTime / 1000L;

void printRunTime();
void setup() {
Serial.begin(9600);
}

void loop(){
printRunTime();
}

void printRunTime(){

char buffer[50];

runTime = millis() - startingRuntime;
hours = runTime / (60 * 60 * 1000L);
runTime %= (60 * 60 * 1000L);
minutes = runTime / (60 * 1000L);
runTime %= (60 * 1000L);
seconds = runTime / 1000L;

sprintf(buffer, "RunTime[%02d:%02d:%02d]", hours, minutes, seconds);
Serial.println(buffer);
if(minutes >= 1){
Serial.println("RESET!");
minutes = 0;
seconds = 0;
}
}
END CODE*

The real question is : What, in the end, do you really want it to do?

-jim lee

millis() - startingRuntime Big clue there.

Please remember to use code tags when posting code

Everything is calculated relative to 'startingRuntime'. If you set 'startingRuntime' to millis() that should re-start the timer.

johnwasser:
Everything is calculated relative to 'startingRuntime'. If you set 'startingRuntime' to millis() that should re-start the timer.

Hi John,
I have tried what you said but still not working :frowning:
You suggested that i do this:
if(minutes == 1){
startingRuntime = millis();
}
but to no avail. timer kept rolling on

We can’t see your code.

TheMemberFormerlyKnownAsAWOL:
We can't see your code.

Here's what i did, as suggested by John

https://dpaste.org/TJC4

at line 31

gitduino:
Here's what i did, as suggested by John

https://dpaste.org/TJC4

at line 31

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to follow, download and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

gitduino:
Here’s what i did, as suggested by John

https://dpaste.org/TJC4

at line 31

int startingRuntime = 0; Oops

A 16-bit integer ('int') can only hold about 32 seconds worth of milliseconds. That's why millis() returns a 32-bit integer ('unsigned long int'). That can hold about 47 days worth of milliseconds.

Here is how I would write it:

unsigned long StartingRuntime = 0;

const unsigned long SECOND = 1000UL;
const unsigned long MINUTE = 60 * SECOND;
const unsigned long HOUR = 60 * MINUTE;

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

void loop()
{
  printRunTime();
}

void printRunTime()
{
  char buffer[50];

  unsigned long runTime = millis() - StartingRuntime;
  int hours = runTime / HOUR;
  int minutes = (runTime % HOUR) / MINUTE;
  int seconds = (runTime % MINUTE) / SECOND;

  sprintf(buffer, "RunTime[%02d:%02d:%02d]", hours, minutes, seconds);
  Serial.println(buffer);
  
  if (minutes >= 1)
  {
    Serial.println("RESET!");
    StartingRuntime = millis();  // restart the timer
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.