Need help on making a timer type of thing and I am so lost

Hi everyone,
I am trying to write a segment into my code that reads the status of a simple counting variable i.
Basically every time another part of the code runs it adds 1 to i.
I want the timer thing to say, if i is > 2 at any point in the last 48 hours it should set a different variable "x" to add 1.
I have poked around some websites looking at all the different time based classes but nothing is popping out.
Any help is greatly appreciated.

We need some information from you.

Show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Make it a function that is used instead of a direct increment and check the value inside that function. Rename i to find all places where the function has to be called.

In OOP one would make i a property with an increment method.

bool conditionFound = false;
unsigned long timeFound;
...
if (i > 2) {
  conditionFound = true;
  timeFound = millis();
}
...
if (conditionFound && millis() - timeFound <= 48UL*60*60*1000) { //corrected!
  x++;
  conditionFound = false;
}

I have changed the above several times. I am still not sure i understand exactly what you need.

once "i > 2", wouldn't the condition being monitored for always be true.

consider the follow snipped that monitors for a "change" in value > 2

void
monitor (void)
{
    static unsigned long msecLst;
           unsigned long msec     = millis ();

    if ( (msec - msecLst) > Period)  {
        msecLst = msec;

        if ( (cnt - cntLst) > 2)  {
            events++;
            Serial.println (events);
        }
        cntLst = cnt;
    }
}

I think you mean:
if (conditionFound && millis() - timeFound <= 48UL*60*60*1000) {

Oops! Thanks John, will correct that!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.