Need help with Metro Library not keeping time correctly.

Hello, Thanks for checking my thread. I am having trouble with my code. I am writing a program for a: Recycling Timer for an aeroponics project. That turns on for a second then goes off for 1 hour, repeatedly.

The problem I have is that: it works for the first two “on” moments, at 1s when the program starts and at 1hr. But after that, the timing goes way off. by the time it’s hour 3, it turns on every 30 mins and ends up turning on at 15 mins interval by hour 4… it’s worse when I put in 30 min interval.

I tried so many thing like changing the ints to unsigned long, doesnt help. SO I came to the experts at the arduino forum!

Device: Seeduino v2.12, with Relay Shield from seeduino also.

Please lend me a helping hand. Here is my code:

/*
 This Code will set a repeat cycle timer using the Metro Library.
 */
#include <Metro.h> //Include Metro library
unsigned char relayPin[4] = {4,5,6,7}; // Define the led's pin

extern volatile unsigned long timer0_overflow_count;

int mode[4]={LOW,LOW,LOW,LOW};

// Initiate a metro object and set the interval to 250 milliseconds (0.25 seconds).
Metro ledMetro1 = Metro(250); 
Metro ledMetro2 = Metro(250); 
Metro ledMetro3 = Metro(250); 
Metro ledMetro4 = Metro(250); 
unsigned long Sec = 1000;
unsigned long Min= 60*1000;
unsigned long onTime1=Sec; 
unsigned long offTime1=60*Min;

void setup()
{
  int i;
  for(i = 0; i < 4; i++)
  {
    pinMode(relayPin[i],OUTPUT);
    digitalWrite(relayPin[i],mode[i]);
  }
}


void loop()
{
  //Zone one
   if (ledMetro1.check() == 1) { // check if the metro has passed its interval .
    if (mode[0]==LOW)  { 
      mode[0]=HIGH;
      ledMetro1.interval(onTime1); // if the pin is HIGH, set the interval to onTime1.
    }
    else {
      ledMetro1.interval(offTime1); // if the pin is LOW, set the interval to offTime1.
      mode[0]=LOW;
    }
    digitalWrite(relayPin[0],mode[0]);
    }
}

Moderator edit: CODE TAGS

Toggling 4 pins on and off at regular intervals does not need the Metro class. The millis() function, and and an array to hold the last change time is all that is needed.

I'd highly recommend that you learn how to use millis() to determine if it is time to turn the pins on or off. Trust me, it will be time well spent.

Thanks for your help! PaulS

just in case it comes up again,
I fixed the problem by adding the metro’s reset() function. I am having trouble making functions with the Metro Class since it is not possible to make arrays out of the Metro ledMetro[4]. but it works for now… without fucntions. But in the future I will add up to 8 zones for my High Pressure Aeroponic project so it would be a lot easier with functions.
I may be over complicating things, because as PaulS said, i should just use the millis() class to keep time, it will be easier to create prototype functions that way.

what do you guys think?

/*
 This Code will set a repeat cycle timer using the Metro Class
 */
#include <Metro.h> //Include Metro library
unsigned char relayPin[4] = {4,5,6,7}; // Define the led's pin

int mode[4]={LOW,LOW,LOW,LOW};

Metro ledMetro1 = Metro(1000); 

void setup()
{
  int i;
  for(i = 0; i < 4; i++)
  {
    pinMode(relayPin[i],OUTPUT);
    digitalWrite(relayPin[i],mode[i]);
  }
}


void loop()
{
   if (ledMetro1.check() == 1) { // check if the metro has passed its interval .
    if (mode[0]==LOW)  { 
      mode[0]=HIGH;
      ledMetro1.interval(1000L); // if the pin is HIGH, set the interval to 1 seconds.
    }
    else {
      ledMetro1.interval(900000L); // if the pin is LOW, set the interval to 15 minutes.
      mode[0]=LOW;
    }
    digitalWrite(relayPin[0],mode[0]);
    ledMetro1.reset();
    }
}