/SuperChrono

Advanced Chrono library for Arduino/Wiring

Primary LanguageC++GNU General Public License v3.0GPL-3.0

SuperChrono

SuperChrono library for Arduino or Wiring by Sofian Audry

Advanced chronometer/stopwatch class that counts the time passed since started. For a chronometer with more basic features but a lower memory trace please use the Chrono library.

Installation

Copy the SuperChrono folder to your Arduino libraries.

Basic Example

#include <SuperChrono.h>

// Instanciate a SuperChrono object.
SuperChrono mySuperChrono;

void setup() {
  // Start the chronometer on setup.
  mySuperChrono.start();
}

void loop() {
  // Check whether the chronometer has reached 1000 ms
  if (mySuperChrono.hasPassed(1000)) {
    // Do something here...
    // Restart the SuperChrono.
    mySuperChrono.start();
  }
}

Include the library

// INCLUDE SuperChrono LIBRARY :
#include <SuperChrono.h> 

Create an instance

Once a SuperChrono is created, it starts counting.

// CREATE A SuperChrono INSTANCE :
SuperChrono mySuperChrono;

You can create a SuperChrono that counts in microseconds or seconds:

SuperChrono mySuperChronoMicros(SuperChrono::MICROS);
SuperChrono mySuperChronoSeconds(SuperChrono::SECONDS);

Alternatively you can create a SuperChrono with a custom time function:

unsigned long mySpecialTimeFunction();

SuperChrono mySuperChronoMicros(mySpecialTimeFunction);

start(offset)

Starts/restarts the chronometer.

// START/RESTART THE CHRONOMETER :
mySuperChrono.start();

// START/RESTART THE CHRONOMETER WITH ALREADY 1000 TIME UNITS AS AN OFFSET :
mySuperChrono.start(1000);

stop()

Stops/pauses the chronometer.

// STOPS/PAUSES THE CHRONOMETER :
mySuperChrono.stop();

resume()

Resumes the chronometer.

// STOPS/PAUSES THE CHRONOMETER :
mySuperChrono.resume();

elapsed()

Returns the elasped time.

// GET ELAPSED MILLISECONDS :
unsigned long elapsed = mySuperChrono.elapsed();

hasPassed(timeout)

Returns true if the chronometer passed the timeout.

if ( mySuperChrono.hasPassed(500) ) {
	// DO SOMETHING IF 500 TIME UNITS HAVE PASSED.
}

Combined with start() you can have a metronome :

if ( mySuperChrono.hasPassed(200) ) {
  mySuperChrono.start();
	// DO SOMETHING EVERY 200 TIME UNITS.
}

add(time)

Adds some time to the chronometer.

// ADDS 500 TIME UNITS TO THE CHRONOMETER :
mySuperChrono.add(500);

isRunning()

Returns true if the chronometer is currently running.

mySuperChrono.start();
Serial.println( mySuperChrono.isRunning() ); // will print "1" (true)
mySuperChrono.stop();
Serial.println( mySuperChrono.isRunning() ); // will print "0" (false)
mySuperChrono.resume();
Serial.println( mySuperChrono.isRunning() ); // will print "1" (true)

delay(time)

Waits for some time (in the time unit of the chronometer).

// WAIT FOR 1000 TIME UNITS :
mySuperChrono.delay(1000);