Chronometer/stopwatch library that counts the time passed since started.
24 | 25 |Copy the Chrono folder to your Arduino or Wiring libraries folder or download it with the Arduino library manager.
33 |
34 |
36 | // INCLUDE CHRONO LIBRARY
37 | // Documentation : https://github.com/SofaPirate/Chrono-Arduino-Wiring/blob/master/README.md
38 | // Download : https://github.com/SofaPirate/Chrono-Arduino-Wiring/archive/master.zip
39 | #include <Chrono.h>
40 |
41 | // Instanciate a Chrono object.
42 | Chrono myChrono;
43 |
44 | void setup() {
45 |
46 | }
47 |
48 | void loop() {
49 | // Check whether the chronometer has reached 1000 time units.
50 | if (myChrono.hasPassed(1000)) {
51 | myChrono.restart(); // Restart the chronometer.
52 |
53 | // Do something here...
54 | }
55 | }
56 |
57 | // INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
61 | #include <Chrono.h>
62 |
63 |
64 |
68 | // CREATE A CHRONO INSTANCE :
69 | Chrono myChrono;
70 |
71 |
72 | elapsed() | 76 |unsigned long | 77 |Returns the elapsed time | 78 |
restart() | 81 |void | 82 |Sets the elapsed time to 0 and starts the chronometer | 83 |
hasPassed( timeout ) | 86 |bool | 87 |Returns true if the chronometer passed the timeout | 88 |
94 | /*
95 | This code will toggle pin 13 on and off every second (1000 ms).
96 | This should be the debug LED's pin. So the debug LED should
97 | blink every second.
98 | */
99 |
100 | // INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
101 | #include
102 |
103 | // Set the led's pin
104 | int ledPin = 13;
105 |
106 | //Create a variable to hold the led's state
107 | int ledState = HIGH;
108 |
109 | // Instanciate a Chrono object.
110 | Chrono ledChrono;
111 |
112 | void setup()
113 | {
114 | pinMode(ledPin,OUTPUT);
115 | digitalWrite(ledPin,ledState);
116 | }
117 |
118 | void loop()
119 | {
120 |
121 | if (ledChrono.hasPassed(1000) ) { // returns true if it passed 1000 ms since it was started
122 | ledChrono.restart(); // restart the crono so that it triggers again later
123 |
124 | // toggle the stored state
125 | if (ledState==HIGH) ledState=LOW;
126 | else ledState=HIGH;
127 |
128 | // write the state to the pin
129 | digitalWrite(ledPin,ledState);
130 | }
131 | }
132 |
133 |
134 |
137 | // This code will toggle output 13 every 250 ms
138 | // and will toggle output 9 every 125 ms
139 |
140 |
141 | // INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
142 | #include
143 |
144 | // Create variables for the LED pins
145 | int ledPinA = 13;
146 | int ledPinB = 9;
147 |
148 | // Create variables to hold the LED states
149 | int ledStateA = HIGH;
150 | int ledStateB = HIGH;
151 |
152 | // Instantiate two Chronos
153 | Chrono chronoA;
154 | Chrono chronoB;
155 |
156 | void setup()
157 | {
158 | pinMode(ledPinA,OUTPUT);
159 | digitalWrite(ledPinA,ledStateA);
160 |
161 | pinMode(ledPinB,OUTPUT);
162 | digitalWrite(ledPinB,ledStateB);
163 |
164 | }
165 |
166 | void loop()
167 | {
168 | // Use Chrono as a metronome with an interval of 250 ms:
169 | if ( chronoA.hasPassed(250) ) { // returns true if it passed 250 ms since it was started
170 | chronoA.restart(); // restart the crono so that it triggers again later
171 | ledStateA = !ledStateA; // !: toggle the state from 0 to 1 or from 1 to 0
172 | digitalWrite(ledPinA,ledStateA);
173 | }
174 |
175 | // Use Chrono as a metronome with an interval of 125 ms:
176 | if ( chronoB.hasPassed(125) ) { // returns true if it passed 125 ms since it was started
177 | chronoB.restart(); // restart the crono so that it triggers again later
178 | ledStateB = !ledStateB; // !: toggle the state from 0 to 1 or from 1 to 0
179 | digitalWrite(ledPinB,ledStateB);
180 | }
181 |
182 |
183 | }
184 |
185 |
186 |
187 | Chrono myChronoMicros(Chrono::MICROS);
193 |
194 | Chrono myChronoSeconds(Chrono::SECONDS);
195 |
196 |
197 | unsigned long mySpecialTimeFunction();
200 |
201 | Chrono myChronoMicros(mySpecialTimeFunction);
202 |
203 |
204 | restart( offset ) | 208 |void | 209 |You can alternatively start(restart) the chronometer with a time offset | 210 |
stop() | 213 |void | 214 |Stops/pauses the chronometer | 215 |
resume( ) | 218 |void | 219 |Resumes the chronometer | 220 |
hasPassed( timeout,restartIfPassed ) | 223 |bool | 224 |If the chronometer passed the timeout, returns true and restarts the chronometer | 225 |
add( time ) | 228 |void | 229 |Immediately adds some time to the chronometer | 230 |
isRunning( ) | 233 |bool | 234 |Returns true if the chronometer is currently running | 235 |
delay( time ) | 238 |void | 239 |Waits for some time (in the time unit of the chronometer) | 240 |