Skip to content

v-dvorak/arduino-timer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Timer

Simple library for timing in milliseconds, works with Atmega328P, Arduino UNO, may work for other AVRs. Uses TIMER0 and interrupts, can be easily modified to run on any timer, for that see documentation linked below. Timer can run without an overflow for almost 50 days.

Features

Init

void Init(void);

Initialisation function, must be called before anything else.

Get

unsigned long Get(void);

Read current time.

Reset

void Reset(void);

Resets milliseconds counter back to zero.

Example usage

#include "timer.h"
#define DELAY 200 // in ms

Timer timer;
unsigned long lastReading;

int main(void)
{
    // setup timer
    timer.Init();
    unsigned long lastReading = 0;
    unsigned long currentTime = 0;
    // enable interrupts
	sei();

	// main loop
	while (1)
	{
        // get time at the start of a loop
        currentTime = timer.Get();

        // if enough time has passed
        if (currentTime - lastReading > DELAY) {
            /* 
            do something (e.g. blink LED)
            */
           // reset timing
           lastReading = currentTime;
        }
    }
}

Sources

About

Simple timer library for Atmega328P / Arduino UNO

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages