├── .gitignore ├── LICENSE ├── README.md ├── Timer.cpp ├── Timer.h ├── examples └── Timer │ └── Timer.ino ├── keywords.txt ├── library.json └── library.properties /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Stefan Staub 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Timer Library v1.1.2 2 | 3 | The **Arduino Timer Library** allows you to measure the time between started and stop command. The time can measured in milli or micro seconds. Micro seconds have only a resolution of 4µs! 4 | 5 | ## Installation 6 | Install directly from the library manager of the Arduino or PlatformIO IDE or do it manually: 7 | 8 | 1. Download from the Release site 9 | 2. Unzip 10 | 3. Move the folder to your Arduino Library folder 11 | 12 | ## How to use 13 | 14 | First, include the Timer library to your project: 15 | 16 | ``` 17 | #include "Timer.h" 18 | ``` 19 | 20 | Now, you can create a new object(s): 21 | 22 | ``` 23 | Timer timer; 24 | // for micro second resolution: 25 | Timer timer(MICROS); 26 | timer.start(); // start the timer 27 | timer.pause(); // pause the timer 28 | timer.resume(); // resume the timer 29 | timer.stop(); // stops the timer 30 | timer.read(); // gives back the elapsed time in milli or micro seconds 31 | ``` 32 | 33 | ## Example 34 | 35 | Complete example: Here we created one timer, you can run it and get the result in the Serial monitor. 36 | 37 | ``` 38 | #include "Timer.h" 39 | 40 | Timer timer; 41 | 42 | void setup() { 43 | Serial.begin(9600); 44 | timer.start(); 45 | if(timer.state() == RUNNING) Serial.println("timer running"); 46 | delay(1000); 47 | timer.stop(); 48 | if(timer.state() == STOPPED) Serial.println("timer stopped"); 49 | Serial.print("time elapsed ms: "); 50 | Serial.println(timer.read()); 51 | } 52 | 53 | void loop() { 54 | return; 55 | } 56 | 57 | ``` 58 | 59 | ## Documentation 60 | 61 | ### Constructors / Destructor 62 | 63 | **Timer(resolution_t resolution = MILLIS)**
64 | Creates a Timer object 65 | - parameter resolution sets the internal resolution of the Timer, it can MICROS, or MILLIS 66 | 67 | **~Ticker()**
68 | Destructor for Ticker object 69 | 70 | ### Functions 71 | 72 | **void start()**
73 | Start the Timer. If it is paused, it will restarted the Timer. 74 | 75 | **void pause()**
76 | Pause the Timer. 77 | 78 | **void resume()**
79 | Resume the Timer after a pause. 80 | 81 | **void stop()**
82 | Stops the Timer. 83 | 84 | **uint32_t read()**
85 | Returns the time after start, you can read the elapsed time also while running. 86 | 87 | **status_t state()**
88 | Get the Timer state (RUNNING, PAUSED, STOPPED). 89 | -------------------------------------------------------------------------------- /Timer.cpp: -------------------------------------------------------------------------------- 1 | /* Ticker library code is placed under the MIT license 2 | * Copyright (c) 2020 Stefan Staub 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "Timer.h" 26 | 27 | Timer::Timer(resolution_t resolution) { 28 | this->resolution = resolution; 29 | } 30 | 31 | Timer::~Timer() {} 32 | 33 | void Timer::start() { 34 | elapsed = 0; 35 | if(resolution == MILLIS) started = millis(); 36 | if(resolution == MICROS) started = micros(); 37 | status = RUNNING; 38 | } 39 | 40 | void Timer::pause() { 41 | if (status == RUNNING) { 42 | if(resolution == MILLIS) elapsed = elapsed + millis() - started; 43 | if(resolution == MICROS) elapsed = elapsed + micros() - started; 44 | status = PAUSED; 45 | } 46 | } 47 | 48 | void Timer::resume() { 49 | if (status == PAUSED) { 50 | if(resolution == MILLIS) started = millis(); 51 | if(resolution == MICROS) started = micros(); 52 | status = RUNNING; 53 | } 54 | } 55 | 56 | void Timer::stop() { 57 | if (status == RUNNING) { 58 | if(resolution == MILLIS) elapsed = millis() - started + elapsed; 59 | if(resolution == MICROS) elapsed = micros() - started + elapsed; 60 | } 61 | status = STOPPED; 62 | } 63 | 64 | uint32_t Timer::read() { 65 | if (status == RUNNING) { 66 | if(resolution == MILLIS) return millis() - started + elapsed; 67 | if(resolution == MICROS) return micros() - started + elapsed; 68 | } 69 | return elapsed; 70 | } 71 | 72 | status_t Timer::state() { 73 | return status; 74 | } -------------------------------------------------------------------------------- /Timer.h: -------------------------------------------------------------------------------- 1 | /* Ticker library code is placed under the MIT license 2 | * Copyright (c) 2020 Stefan Staub 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #ifndef TIMER_H 26 | #define TIMER_H 27 | 28 | #include "Arduino.h" 29 | 30 | /** Ticker internal resolution 31 | * 32 | * @param MICROS default, the resolution is in micro seconds, max is 70 minutes, the real resolution is 4 microseconds at 16MHz CPU cycle 33 | * @param MILLIS set the resolution to millis, for longer cycles over 70 minutes 34 | * 35 | */ 36 | enum resolution_t {MICROS, MILLIS}; 37 | 38 | /** Ticker status 39 | * 40 | * @param STOPPED default, ticker is stopped 41 | * @param RUNNING ticker is running 42 | * @param PAUSED ticker is paused 43 | * 44 | */ 45 | enum status_t {STOPPED, RUNNING, PAUSED}; 46 | 47 | class Timer { 48 | 49 | public: 50 | 51 | /** create a Ticker object 52 | * 53 | * @param callback the name of the function to call 54 | * @param timer interval length in ms or us 55 | * @param repeat default 0 -> endless, repeat > 0 -> number of repeats 56 | * @param resolution default MICROS for tickers under 70min, use MILLIS for tickers over 70 min 57 | * 58 | */ 59 | Timer(resolution_t resolution = MILLIS); 60 | 61 | /** destructor for the Ticker object 62 | * 63 | */ 64 | ~Timer(); 65 | 66 | /** started the ticker 67 | * 68 | */ 69 | void start(); 70 | 71 | /** 72 | * @brief pause the timer 73 | * 74 | */ 75 | void pause(); 76 | 77 | /** 78 | * @brief resume the timer after pause 79 | * 80 | */ 81 | void resume(); 82 | 83 | /** stops the ticker 84 | * 85 | */ 86 | void stop(); 87 | 88 | /** 89 | * @brief give the elapsed time back 90 | * 91 | * @return uint32_t eleapsed time in micro or milli seconds 92 | */ 93 | uint32_t read(); 94 | 95 | /** 96 | * @brief get the state of the timer 97 | * 98 | * @return status_t 99 | */ 100 | status_t state(); 101 | 102 | private: 103 | uint32_t started; 104 | uint32_t elapsed; 105 | resolution_t resolution; 106 | status_t status; 107 | }; 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /examples/Timer/Timer.ino: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | Timer timer; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | timer.start(); 8 | if(timer.state() == RUNNING) Serial.println("timer running"); 9 | delay(1000); 10 | timer.stop(); 11 | if(timer.state() == STOPPED) Serial.println("timer stopped"); 12 | Serial.print("time elapsed ms: "); 13 | Serial.println(timer.read()); 14 | } 15 | 16 | void loop() { 17 | return; 18 | } 19 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | Timer KEYWORD1 2 | start KEYWORD2 3 | pause KEYWORD2 4 | resume KEYWORD2 5 | stop KEYWORD2 6 | read KEYWORD2 7 | state KEYWORD2 -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Timer", 3 | "keywords": "Timer", 4 | "description": "A small library for measuring the time between started and stop command.", 5 | "repository": 6 | { 7 | "type": "git", 8 | "url": "https://github.com/sstaub/Timer" 9 | }, 10 | "version": "1.2.1", 11 | "frameworks": "arduino", 12 | "platforms": "*" 13 | } 14 | 15 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Timer 2 | version=1.2.1 3 | author=Stefan Staub 4 | maintainer=Stefan Staub 5 | sentence=A library for creating start / stop Timers 6 | paragraph=Small library for measuring elapsed time between start and stop command 7 | category=Timing 8 | url=https://github.com/sstaub/Timer 9 | architectures=* 10 | --------------------------------------------------------------------------------