├── README.md ├── examples └── CountimerTest │ └── CountimerTest.ino ├── keywords.txt ├── library.properties └── src ├── Countimer.cpp └── Countimer.h /README.md: -------------------------------------------------------------------------------- 1 | # Countimer 2 | 3 | This is simple timer and counter Arduino library. 4 | Offers three work modes: 5 | 6 | * Count-up timer with call specified method when count is complete. 7 | * Count-down timer with call specified method when count is complete. 8 | * Calling any method at a specified time interval. 9 | 10 | 11 | It allows you to start/pause, stop or restart timer. 12 | The following are public methods for actions: 13 | 14 | * void start() 15 | * void stop() 16 | * void pause() 17 | * void restart() 18 | 19 | 20 | Other methods: 21 | 22 | * byte getCurrentHours() 23 | * byte getCurrentMinutes() 24 | * byte getCurrentSeconds() 25 | * void setInterval() 26 | * String getCurrentTime() 27 | * bool isCounterCompleted() 28 | * bool isCounterRunning() 29 | * bool isStopped() 30 | 31 | 32 | 33 | And here's some sample code! 34 | 35 | ```c 36 | #include "Countimer.h" 37 | 38 | Countimer timer; 39 | 40 | void setup() { 41 | Serial.begin(9600); 42 | 43 | // Set up count down timer with 10s and call method onComplete() when timer is complete. 44 | // 00h:00m:10s 45 | timer.setCounter(0, 0, 10, timer.COUNT_DOWN, onComplete); 46 | 47 | // Print current time every 1s on serial port by calling method refreshClock(). 48 | timer.setInterval(refreshClock, 1000); 49 | } 50 | 51 | void refreshClock() { 52 | Serial.print("Current count time is: "); 53 | Serial.println(timer.getCurrentTime()); 54 | } 55 | 56 | void onComplete() { 57 | Serial.println("Complete!!!"); 58 | } 59 | 60 | void loop() { 61 | // Run timer 62 | timer.run(); 63 | 64 | // Now timer is running and listening for actions. 65 | // If you want to start the timer, you have to call start() method. 66 | if(!timer.isCounterCompleted()) { 67 | timer.start(); 68 | } 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /examples/CountimerTest/CountimerTest.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Open serial monitor, press one of the keys below and click 'Send': 3 | * 'S' - to start all timers 4 | * 'P' - to pause all timers 5 | * 'R' - to restart all timers 6 | * 'T' - to stop all timers 7 | */ 8 | #include "Countimer.h" 9 | 10 | Countimer tUp; 11 | Countimer tDown; 12 | Countimer tNone; 13 | 14 | void setup() 15 | { 16 | Serial.begin(9600); 17 | 18 | // Count-up timer with 10s 19 | tUp.setCounter(0, 0, 10, tUp.COUNT_UP, tUpComplete); 20 | // Call print_time1() method every 1s. 21 | tUp.setInterval(print_time1, 1000); 22 | 23 | // Count-down timer with 21s 24 | tDown.setCounter(0, 0, 21, tDown.COUNT_DOWN, tDownComplete); 25 | // Call print_time2() method every 1s. 26 | tDown.setInterval(print_time2, 1000); 27 | 28 | // No counter 29 | // Just call print_none() method every 2s. 30 | tNone.setInterval(print_none, 2000); 31 | 32 | Serial.println("Press one of the keys below and click 'Send':"); 33 | Serial.println("'S' - to start all timers"); 34 | Serial.println("'P' - to pause all timers"); 35 | Serial.println("'R' - to restart all timers"); 36 | Serial.println("'T' - to stop all timers"); 37 | } 38 | 39 | void loop() 40 | { 41 | tUp.run(); 42 | tDown.run(); 43 | tNone.run(); 44 | 45 | if (Serial.available() > 0) 46 | { 47 | char c = toupper(Serial.read()); 48 | 49 | switch (c) 50 | { 51 | case 'T': 52 | tUp.stop(); 53 | tDown.stop(); 54 | tNone.stop(); 55 | break; 56 | case 'R': 57 | tUp.restart(); 58 | tDown.restart(); 59 | tNone.restart(); 60 | break; 61 | case 'S': 62 | tUp.start(); 63 | tDown.start(); 64 | tNone.start(); 65 | break; 66 | case 'P': 67 | tUp.pause(); 68 | tDown.pause(); 69 | tNone.pause(); 70 | break; 71 | default: 72 | break; 73 | } 74 | } 75 | } 76 | 77 | void print_time1() 78 | { 79 | Serial.print("tUp: "); 80 | Serial.println(tUp.getCurrentTime()); 81 | } 82 | 83 | void print_time2() 84 | { 85 | Serial.print("tDown: "); 86 | Serial.println(tDown.getCurrentTime()); 87 | } 88 | 89 | void print_none() 90 | { 91 | Serial.print("tNone: millis(): "); 92 | Serial.println(millis()); 93 | } 94 | 95 | void tUpComplete() 96 | { 97 | digitalWrite(13, HIGH); 98 | } 99 | 100 | void tDownComplete() 101 | { 102 | digitalWrite(13, LOW); 103 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | Countimer KEYWORD1 2 | timer_callback KEYWORD2 3 | setCounter KEYWORD2 4 | getCurrentHours KEYWORD2 5 | getCurrentMinutes KEYWORD2 6 | getCurrentSeconds KEYWORD2 7 | getCurrentTime KEYWORD2 8 | isCounterCompleted KEYWORD2 9 | isCounterRunning KEYWORD2 10 | isStopped KEYWORD2 11 | run KEYWORD2 12 | start KEYWORD2 13 | stop KEYWORD2 14 | pause KEYWORD2 15 | restart KEYWORD2 16 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Countimer 2 | version=1.0.0 3 | author=inflop 4 | maintainer=inflop 5 | sentence=A simple library for creating timers and counters. 6 | paragraph=Now with end event! 7 | category=Timing 8 | url=https://github.com/inflop/Countimer 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/Countimer.cpp: -------------------------------------------------------------------------------- 1 | #include "Countimer.h" 2 | #include "Arduino.h" 3 | 4 | Countimer::Countimer() 5 | { 6 | _previousMillis = 0; 7 | _currentCountTime = 0; 8 | _countTime = 0; 9 | _isCounterCompleted = false; 10 | _isStopped = true; 11 | _countType = COUNT_NONE; 12 | _startCountTime = 0; 13 | } 14 | 15 | Countimer::~Countimer() 16 | { 17 | } 18 | 19 | void Countimer::setCounter(uint16_t hours, uint8_t minutes, uint8_t seconds, CountType countType, timer_callback onComplete) 20 | { 21 | _onComplete = onComplete; 22 | _countType = countType; 23 | setCounter(hours, minutes, seconds); 24 | } 25 | 26 | void Countimer::setCounter(uint16_t hours, uint8_t minutes, uint8_t seconds) 27 | { 28 | if (hours > COUNTIMER_MAX_HOURS) { 29 | hours = COUNTIMER_MAX_HOURS; 30 | } 31 | 32 | if (minutes > COUNTIMER_MAX_MINUTES_SECONDS) { 33 | minutes = COUNTIMER_MAX_MINUTES_SECONDS; 34 | } 35 | 36 | if (seconds > COUNTIMER_MAX_MINUTES_SECONDS) { 37 | seconds = COUNTIMER_MAX_MINUTES_SECONDS; 38 | } 39 | 40 | _currentCountTime = ((hours * 3600L) + (minutes * 60L) + seconds) * 1000L; 41 | _countTime = _currentCountTime; 42 | 43 | if (_countType == COUNT_UP) 44 | { 45 | // if is count up mode, we have to start from 00:00:00; 46 | _currentCountTime = 0; 47 | } 48 | 49 | _startCountTime = _currentCountTime; 50 | 51 | } 52 | 53 | void Countimer::setInterval(timer_callback callback, uint32_t interval) 54 | { 55 | _interval = interval; 56 | _callback = callback; 57 | } 58 | 59 | uint16_t Countimer::getCurrentHours() 60 | { 61 | return _currentCountTime / 1000 / 3600; 62 | } 63 | 64 | uint8_t Countimer::getCurrentMinutes() 65 | { 66 | return _currentCountTime / 1000 % 3600 / 60; 67 | } 68 | 69 | uint8_t Countimer::getCurrentSeconds() 70 | { 71 | return _currentCountTime / 1000 % 3600 % 60 % 60; 72 | } 73 | 74 | char* Countimer::getCurrentTime() 75 | { 76 | sprintf(_formatted_time, "%02d:%02d:%02d", getCurrentHours(), getCurrentMinutes(), getCurrentSeconds()); 77 | return _formatted_time; 78 | } 79 | 80 | bool Countimer::isCounterCompleted() 81 | { 82 | return _isCounterCompleted; 83 | } 84 | 85 | bool Countimer::isStopped() 86 | { 87 | return _isStopped; 88 | } 89 | 90 | bool Countimer::isCounterRunning() 91 | { 92 | return !_isStopped; 93 | } 94 | 95 | void Countimer::start() 96 | { 97 | _isStopped = false; 98 | if(_isCounterCompleted) 99 | _isCounterCompleted = false; 100 | } 101 | 102 | void Countimer::pause() 103 | { 104 | _isStopped = true; 105 | } 106 | 107 | void Countimer::stop() 108 | { 109 | _isStopped = true; 110 | _isCounterCompleted = true; 111 | _currentCountTime = _countTime; 112 | 113 | if(_countType == COUNT_UP) 114 | { 115 | _currentCountTime = 0; 116 | } 117 | } 118 | 119 | void Countimer::restart() 120 | { 121 | _currentCountTime = _startCountTime; 122 | _isCounterCompleted = false; 123 | _isStopped = false; 124 | 125 | start(); 126 | } 127 | 128 | void Countimer::run() 129 | { 130 | // timer is running only if is not completed or not stopped. 131 | if (_isCounterCompleted || _isStopped) 132 | return; 133 | 134 | if (millis() - _previousMillis >= _interval) { 135 | 136 | if (_countType == COUNT_DOWN) 137 | { 138 | countDown(); 139 | } 140 | else if (_countType == COUNT_UP) 141 | { 142 | countUp(); 143 | } 144 | else 145 | { 146 | callback(); 147 | } 148 | _previousMillis = millis(); 149 | } 150 | } 151 | 152 | void Countimer::countDown() 153 | { 154 | if (_currentCountTime > 0) 155 | { 156 | callback(); 157 | _currentCountTime -= _interval; 158 | } 159 | else 160 | { 161 | stop(); 162 | complete(); 163 | } 164 | } 165 | 166 | void Countimer::countUp() 167 | { 168 | if (_currentCountTime < _countTime) 169 | { 170 | callback(); 171 | _currentCountTime += _interval; 172 | } 173 | else 174 | { 175 | stop(); 176 | complete(); 177 | } 178 | } 179 | 180 | void Countimer::callback() 181 | { 182 | if(_callback != NULL) 183 | _callback(); 184 | } 185 | 186 | void Countimer::complete() 187 | { 188 | if(_onComplete != NULL) 189 | _onComplete(); 190 | } 191 | -------------------------------------------------------------------------------- /src/Countimer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef COUNTIMER_H 4 | #define COUNTIMER_H 5 | 6 | #if defined(ARDUINO) && ARDUINO >= 100 7 | #include 8 | #else 9 | #include 10 | #endif 11 | 12 | #define COUNTIMER_MAX_HOURS 999 13 | #define COUNTIMER_MAX_MINUTES_SECONDS 59 14 | 15 | typedef void(*timer_callback)(void); 16 | 17 | class Countimer 18 | { 19 | public: 20 | Countimer(); 21 | ~Countimer(); 22 | 23 | enum CountType 24 | { 25 | COUNT_NONE = 0, 26 | COUNT_UP = 1, 27 | COUNT_DOWN = 2 28 | }; 29 | 30 | // Set up counter time(hours, minutes, seconds), count mode and function to execute if count is completed. 31 | void setCounter(uint16_t hours, uint8_t minutes, uint8_t seconds, CountType countType, timer_callback onComplete); 32 | 33 | // Set up counter time(hours, minutes, seconds) for existing timer. 34 | void setCounter(uint16_t hours, uint8_t minutes, uint8_t seconds); 35 | 36 | // Returns timer's current hours. 37 | uint16_t getCurrentHours(); 38 | 39 | // Returns timer's current minutes. 40 | uint8_t getCurrentMinutes(); 41 | 42 | // Returns timer's current seconds. 43 | uint8_t getCurrentSeconds(); 44 | 45 | void setInterval(timer_callback callback, uint32_t interval); 46 | 47 | // Returns current timer as formatted string HH:MM:SS 48 | char* getCurrentTime(); 49 | 50 | // Returns true if counter is completed, otherwise returns false. 51 | bool isCounterCompleted(); 52 | 53 | // Returns true if counter is still running, otherwise returns false. 54 | bool isCounterRunning(); 55 | 56 | // Returns true if timer is stopped, otherwise returns false. 57 | bool isStopped(); 58 | 59 | // Run timer. This is main method. 60 | // If you want to start timer after run, you have to invoke start() method. 61 | void run(); 62 | 63 | // Starting timer. 64 | void start(); 65 | 66 | // Stopping timer. 67 | void stop(); 68 | 69 | // Pausing timer. 70 | void pause(); 71 | 72 | // Restart timer. 73 | void restart(); 74 | 75 | private: 76 | // Counting down timer. 77 | void countDown(); 78 | 79 | void callback(); 80 | void complete(); 81 | 82 | // Counting up timer. 83 | void countUp(); 84 | 85 | uint32_t _interval = 1; 86 | uint32_t _previousMillis; 87 | 88 | // Stores current counter value in milliseconds. 89 | uint32_t _currentCountTime; 90 | uint32_t _startCountTime; 91 | 92 | // Stores cached user's time. 93 | uint32_t _countTime; 94 | 95 | // Function to execute. 96 | timer_callback _callback; 97 | 98 | // Function to execute when timer is complete. 99 | timer_callback _onComplete; 100 | bool _isCounterCompleted; 101 | bool _isStopped; 102 | char _formatted_time[10]; 103 | CountType _countType; 104 | }; 105 | 106 | #endif 107 | --------------------------------------------------------------------------------