Using the Chrono Library

Hi everyone,

I have recently discovered the chrono library and have been using for a project i am working on, however i am getting some unusal behaviour, which i am struggling to get to the bottom of, and i am hoping someone will be able to point out my error.

Specifically the problem is found when i stop and resume a chrono timer, and i have made a sketch to demonstrate what i am finding..

#include <Chrono.h>

boolean toggle1 = HIGH;

// Instantiate Chronos
Chrono chronoA; 
Chrono chronoB;
Chrono chronoC;
Chrono chronoDisplay;

void setup() {
  Serial.begin(57600);
  while (!Serial) {
  }
}

void loop() {
  if (chronoA.hasPassed(5000)) {  //Every 5 seconds..
    toggle1 = !toggle1;           //..toggle..
    chronoA.restart();            //..and restart the chrono
  }

  if (toggle1 == LOW) {   //If the toggle is low..
    chronoB.stop();       //..pause the chrono
  }

  if (toggle1 == HIGH) {  //If the toggle is High..
    chronoB.resume();     //..resume the chrono
  } 
  
  if (chronoDisplay.hasPassed(500)) {   //Every 0.5seconds..
    Serial.print("ChronoA: ");          //..output the values to serial
    Serial.println(chronoA.elapsed());
    Serial.print("ChronoB: ");
    Serial.println(chronoB.elapsed());
    Serial.print("ChronoC: ");
    Serial.println(chronoC.elapsed());
    Serial.print("Toggle Position: ");
    Serial.println(toggle1);
    Serial.print("Is chronoB Running: ");
    Serial.println(chronoB.isRunning());
    chronoDisplay.restart();
  }
  
}

The sketch is intended to stop and resume chonoB every 5 seconds, so for 5 seconds it would count up, then stop for 5 seconds, and then count up for a additional 5 seconds etc etc.

However i am finding that chronoB does count up but does not follow any kind of pattern.

If someone could point out my error i would be very grateful!

boolean toggle1 = HIGH;

Boolean variables should be assigned values of true or false.

However i am finding that chronoB does count up but does not follow any kind of pattern.

I can't see any pattern, either. Hint, hint.

Right - like what is this supposed to do, got a link?

Cause the name alone - Chronos, intrigues me. It blends 50's style sci-fi with Arduino.

Paul, I'll remember that about boolean assigned values, thanks.

Chris, here is the description of the chrono library

http://sofapirate.github.io/Chrono/#heading_toc_j_11

Thank you for your replies.

If we could see the output, perhaps the pattern would be obvious to someone, who could then suggest what the problem is, and maybe even a solution.

Hi Paul,

doh, I guess that would help :slight_smile:

ChronoA: 1003
ChronoB: 0
ChronoC: 1003
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 1506
ChronoB: 1
ChronoC: 1506
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 2009
ChronoB: 0
ChronoC: 2009
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 2512
ChronoB: 0
ChronoC: 2512
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 3015
ChronoB: 1
ChronoC: 3015
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 3518
ChronoB: 0
ChronoC: 3518
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 4021
ChronoB: 0
ChronoC: 4021
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 4524
ChronoB: 0
ChronoC: 4524
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 27
ChronoB: 1
ChronoC: 5027
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 530
ChronoB: 1
ChronoC: 5530
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 1033
ChronoB: 1
ChronoC: 6033
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 1536
ChronoB: 1
ChronoC: 6536
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 2040
ChronoB: 1
ChronoC: 7040
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 2543
ChronoB: 1
ChronoC: 7543
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 3046
ChronoB: 1
ChronoC: 8046
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 3549
ChronoB: 1
ChronoC: 8549
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 4052
ChronoB: 1
ChronoC: 9052
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 4555
ChronoB: 1
ChronoC: 9555
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 59
ChronoB: 1
ChronoC: 10059
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 562
ChronoB: 1
ChronoC: 10562
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 1065
ChronoB: 1
ChronoC: 11065
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 1568
ChronoB: 1
ChronoC: 11568
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 2071
ChronoB: 1
ChronoC: 12071
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 2575
ChronoB: 2
ChronoC: 12575
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 3078
ChronoB: 1
ChronoC: 13078
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 3581
ChronoB: 1
ChronoC: 13581
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 4084
ChronoB: 2
ChronoC: 14084
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 4587
ChronoB: 1
ChronoC: 14587
Toggle Position: 1
Is chronoB Running: 1
ChronoA: 90
ChronoB: 2
ChronoC: 15090
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 593
ChronoB: 2
ChronoC: 15593
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 1096
ChronoB: 2
ChronoC: 16096
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 1599
ChronoB: 2
ChronoC: 16599
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 2102
ChronoB: 2
ChronoC: 17102
Toggle Position: 0
Is chronoB Running: 0
ChronoA: 2605
ChronoB: 2
ChronoC: 17605
Toggle Position: 0
Is chronoB Running: 0

PaulS:
If we could see the output, perhaps the pattern would be obvious to someone, who could then suggest what the problem is, and maybe even a solution.

Heh.

@OP (who is devil plus one, I guess ...)

Move the curly brace from right under the line:

</sub> <sub>  chronoA.restart();            //..and restart the chrono</sub> <sub>

to the end of your sketch, and it works, right?

I don't see the point though. Chrono.reset() is cool, but it just seems like an extraneous level of abstraction on top of an already abstracted construct, namely millis() counting.

Hi Chris,

Move the curly brace from right under the line:
chronoA.restart(); //..and restart the chrono

You are right! I moved the curly bracket and it started to work.

#include <Chrono.h>

bool toggle1 = true;

// Instantiate Chronos
Chrono chronoA; 
Chrono chronoB;
Chrono chronoC;
Chrono chronoDisplay;

void setup() {
  Serial.begin(57600);
  while (!Serial) {
  }
}

void loop() {
  if (chronoA.hasPassed(5000)) {  //Every 5 seconds..
    toggle1 = !toggle1;           //..toggle..
    chronoA.restart();            //..and restart the chrono
      
    if (toggle1 == false) {   //If the toggle is low..
      chronoB.stop();       //..pause the chrono
    } else if (toggle1 == true) {  //If the toggle is High..
      chronoB.resume();     //..resume the chrono
    } 
  }
  
  if (chronoDisplay.hasPassed(500)) {   //Every 0.5seconds..
    Serial.print("ChronoA: ");          //..output the values to serial
    Serial.println(chronoA.elapsed());
    Serial.print("ChronoB: ");
    Serial.println(chronoB.elapsed());
    Serial.print("ChronoC: ");
    Serial.println(chronoC.elapsed());
    Serial.print("Toggle Position: ");
    Serial.println(toggle1);
    Serial.print("Is chronoB Running: ");
    Serial.println(chronoB.isRunning());
    chronoDisplay.restart();
  }
    
}

So, i guess it didnt like the original version of my sketch calling the .stop() and .resume() command every at iteration of the loop?

Really appreciate your help with that Chris.