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.
Copy the SuperChrono folder to your Arduino libraries.
#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 SuperChrono LIBRARY :
#include <SuperChrono.h>
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);
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);
Stops/pauses the chronometer.
// STOPS/PAUSES THE CHRONOMETER :
mySuperChrono.stop();
Resumes the chronometer.
// STOPS/PAUSES THE CHRONOMETER :
mySuperChrono.resume();
Returns the elasped time.
// GET ELAPSED MILLISECONDS :
unsigned long elapsed = mySuperChrono.elapsed();
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.
}
Adds some time to the chronometer.
// ADDS 500 TIME UNITS TO THE CHRONOMETER :
mySuperChrono.add(500);
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)
Waits for some time (in the time unit of the chronometer).
// WAIT FOR 1000 TIME UNITS :
mySuperChrono.delay(1000);