Arduino Mega 2560

Timers can be used for example to measure elapsed time or PWM signal generation. This timer is clocked by the internal clock source. The Timer in this case is a special countdown timer. It uses an 8 bit timer, which is Timer 2 in both ATmega328P and ATmega2560 chips. Every tick increases a 64 bit global variable created in OzTimer.h. You can declare a lot of timers, because these are virtual timers based on the preciseness of Timer 2. You can set the initial value of the countdown timers to any year, day, hour, minute, second, millisecond and microsecond. It will start countdown immediately. The virtual timers will tick simultaneously. You can stop them all anytime. After any virtual timer finishes countdown it can run a predefined function that is added to the programcode.

Required hardware

  • Arduino Mega 2560

Source code to install on controller


Before you upload this code to your Arduino, please format the EEPROM...
#include <OzTimer.h>
#include <OzIDManager.h>

OzIDManager* manager;
OzTimer* timer;

// save indexes for virtual timers
uint8_t i1, i2, i3, i4;

// Function called
// when last timer expires
void sayFinished()
{
  Serial.println("Timers finished.");
}

void setup()
{
  Serial.begin(115200);

  manager = new OzIDManager();
  manager->_sendACK = false;
  manager->_checksum = false;

  OzCommunication::setIDManager(manager);

  timer = new OzTimer();
  
  // Timer subscription with callback function
  // 5000 millisecs
  i1 = timer->addTimerMillisec(5000, "test_1");

  // 6000 millisecs
  i2 = timer->addTimerMillisec(6000, "test_2");

  // 65000 millisecs
  i3 = timer->addTimerString("1:5:0:0", "test_3");

  // Runs sayFinished() after 72000 millisecs
  i4 = timer->addTimerString("1:10:1000:1000000", "test_4", sayFinished);
  
  int x = 1;
  manager->sendLinkSetup();
  manager->PrintWelcomeLine(timer, x++, "MyTimers");
}

void loop()
{
    OzCommunication::communicate();
    OzTimer::loop();
}