Skip to content

Timer: Cleanup and fix millis() rollover bug #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions Timer/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,7 @@

#include "Timer.h"

Timer::Timer(unsigned long int ms){
Create(ms, NULL, false);
}

Timer::Timer(unsigned long int ms, CallBackType callback){
Create(ms, callback, false);
}

Timer::Timer(unsigned long int ms, CallBackType callback, bool isSingle){
Create(ms, callback, isSingle);
}

void Timer::Create(unsigned long int ms, CallBackType callback, bool isSingle){
setInterval(ms);
setEnabled(false);
setSingleShot(isSingle);
Expand All @@ -40,7 +28,7 @@ void Timer::Create(unsigned long int ms, CallBackType callback, bool isSingle){
}

void Timer::setInterval(unsigned long int ms){
msInterval = (ms > 0) ? ms : 0;
msInterval = ms;
}

void Timer::setEnabled(bool Enabled){
Expand Down Expand Up @@ -84,9 +72,7 @@ void Timer::Update(){
bool Timer::Tick(){
if(!blEnabled)
return false;
if(LastTime > millis()*2)//millis restarted
LastTime = 0;
if ((unsigned long int)(millis() - LastTime) >= msInterval) {
if (millis() - LastTime >= msInterval) {
LastTime = millis();
if(isSingleShot())
setEnabled(false);
Expand All @@ -101,14 +87,14 @@ unsigned long int Timer::getInterval(){
}

unsigned long int Timer::getCurrentTime(){
return (unsigned long int)(millis() - LastTime);
return millis() - LastTime;
}

unsigned long int Timer::getRemaining() {
if (blEnabled) {
return msInterval - (unsigned long int)(millis() - LastTime);
return msInterval - (millis() - LastTime);
} else {
return msInterval - (unsigned long int)(millis() - (millis() - DiffTime));
return msInterval - DiffTime;
}
}

Expand Down
5 changes: 1 addition & 4 deletions Timer/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ typedef void (*CallBackType)();

class Timer{
private:
void Create(unsigned long int ms, CallBackType callback, bool isSingle);
unsigned long int msInterval;
bool blEnabled;
bool blSingleShot;
Expand All @@ -37,9 +36,7 @@ class Timer{
unsigned long DiffTime;//used when I pause the Timer and need to resume

public:
Timer(unsigned long int ms);
Timer(unsigned long int ms, CallBackType callback);
Timer(unsigned long int ms, CallBackType callback, bool isSingle);
Timer(unsigned long int ms, CallBackType callback = NULL, bool isSingle = false);
~Timer();

void setInterval(unsigned long int ms);
Expand Down