how do i run a function every 2 mins in the loop

i want a function to run in the loop but i don't want it to run every time it loops. i want it to run every 2 or 3 mins how can this be done?

sorry if it obvious to some people but i'm new to all this

Research "Blink Without Delay"

Something like this:

unsigned long lastMillis;
void setup()
{
}
 void loop()
{ 
//is the time up for this task?
  if (millis() - lastMillis >= 2*60*1000UL) 
  {
   lastMillis = millis();  //get ready for the next iteration
   myFunction();
  }

// other code
}

First question: Is this the only process you want to be running, or do you want more than one process running (multi tasking) ?

Do you want the easy way, or the harder way?

If you only have one function to run, and want the easy way, then something like:

void setup() { }

void loop() { myFunction(); delay(1000*120); // 2 minute delay }

yes there is a touchscreen touch detect and a check to see if an RF BUtton is pressed going on as well in the loop

Then you need the multi tasking version. Do you understand the system that LarryD provided. That should work for your needs.

yes i do thanks alot for this

The best way to schedule events without losing time accuracy (may not matter here, but it can) is this:

unsigned long target_time = 0L ;

#define PERIOD 2*60*1000L ;

void loop ()
{
  if (millis () - target_time >= PERIOD)
  {
    target_time += PERIOD ;   // change scheduled time exactly, no slippage will happen
    /// do stuff
  }
  // other stuff
}

This will matter a lot for fast repetition where the CPU time to handle the tests and other actions of loop() are significant compared to the PERIOD. This code will also "catch up" if something called from loop () delays longer than PERIOD.

#define PERIOD 2*60*1000L ;

oops ;)

Have a look at several things at a time. It is an extended example of BWoD

...R

@Robin2 "Have a look at several things at a time. It is an extended example of BWoD"

I looked at that link, but did not find how fast it runs (how many times the loop runs per second?)

Can the loop run 140,000 times a second on a nano V3 ? If not, I will share my code that runs just a bit over that, with three functions.

AWOL: ```

define PERIOD 2*60*1000L ;




oops ;)

Bah, I wasn't awake!

#define PERIOD (2*60*1000L)

define PERIOD (2*60*1000L)

what is the advantage over:

define PERIOD (2*60*1000UL)

or const unsigned long PERIOD = 2*60*1000UL;

"or
const unsigned long PERIOD = 2601000UL;"

I think it is better to use the const, since the define may produce problems if another variable has that keyword in it. Such as another variable called lastPERIOD .

@jack wp Good point.

unsigned long as that is what millis() and micros() are return.

[quote author=jack wp link=msg=2327594 date=1437674432] @Robin2 "Have a look at several things at a time. It is an extended example of BWoD"

I looked at that link, but did not find how fast it runs (how many times the loop runs per second?)

Can the loop run 140,000 times a second on a nano V3 ? If not, I will share my code that runs just a bit over that, with three functions.[/quote]

What's this got to do with the OP's problem ?

I have no idea how many times my code repeats. In any case that will depend on what goes on in each of the functions that get called - just the same as with any code you write.

Please feel free to compare my code with yours and let us know the results.

...R

[quote author=jack wp link=msg=2327631 date=1437676168] "or const unsigned long PERIOD = 2*60*1000UL;"

I think it is better to use the const, since the define may produce problems if another variable has that keyword in it. Such as another variable called lastPERIOD . [/quote] Nope. If you define a preprocessor macro called PERIOD, the preprocessor will do nothing with a variable called lastPERIOD. Try it for yourself.

@christop Maybe I was confused by reading from the reference page that states. "This can have some unwanted side effects though, if for example, a constant name that had been #defined is included in some other constant or variable name. In that case the text would be replaced by the #defined number (or text). In general, the const keyword is preferred for defining constants and should be used instead of #define."

https://www.arduino.cc/en/Reference/Define

Maybe I was confused by reading from the reference page that

is wrong.