user40973: I have a sketch with multiple functions in it. The 2 main functions in that sketch are get_data and post_data.
Now these functions are called from loop(). The thing is I want these functions to run at different times. Basically I want the get_data function to run once every 4 minutes and the post_data function to run once every hour. How can I achieve that?
As far as I know, you can't run multiple programs together on an Arduino Mega.
P.S: I'm new to Arduino programming.

user23991: I imagine that you tried to figure out how to do it whit the delay() function (if it's not your case, it is for most Arduino beginners). Well... you can't use delay() in this case (and in most other cases) because it completely blocks the program for a given time (1) (except for interrupts, but i won't treat interrupts here). In general it is better not to use delays, except for very simple programs or in initialisation routines (i.e. in stuff you usually put in setup()).

Since your delays are quite long I imagine that you don't need much precision (i.e. you don't need to be more precise than a millisecond). So you could do like that:
void setup() {}

// Times at which we last called the functions
// (These are global variables, which are in general best avoided)
unsigned long lastGetData = 0;
unsigned long lastPostData = 0;

void loop() {

    //                           min.  sec.  milliseconds
    if(millis() - lastGetData >= 4   * 60  * 1000) {

        lastGetData = millis();
        get_Data();
    }

    //                            min.  sec.  milliseconds
    if(millis() - lastPostData >= 60  * 60  * 1000) {

        lastPostData = millis();
        post_Data();
    }
}

void get_Data() {
    // ...
}

void post_Data() {
    // ...
}

Notes:

millis() returns the time since the startup of the Arduino in millisecond. To get more precision you could also use micros() which returns the same but in microseconds (then you would need to multiply the number in the if condition by 1000).
you can add as much functions as you want whit different execution delays. But you must keep every function as short as possible, i.e. you must not use delay()s, because while executing a function if the time to execute another function comes that function will only be called after the end of the current one.
I used the => sign, and not ==, so that even if the loop doesn't evaluates one if condition in the very milliseconds in which it becomes true it can positively evaluate it some milliseconds later (e.g. after the end of another running function).


40973: <p>I have a sketch with multiple functions in it. The 2 main functions in that sketch are <code>get_data</code> and <code>post_data</code>.</p>

<p>Now these functions are called from <code>loop()</code>. The thing is I want these functions to run at different times. Basically I want the <code>get_data</code> function to run once every 4 minutes and the <code>post_data</code> function to run once every hour. How can I achieve that?</p>

<p>As far as I know, you can't run multiple programs together on an Arduino Mega.</p>

<p>P.S: I'm new to Arduino programming.</p>
 23991: <p>I imagine that you tried to figure out how to do it whit the <code>delay()</code> function (if it's not your case, it is for most Arduino beginners). Well... you can't use <code>delay()</code> in this case (and in most other cases) because it completely blocks the program for a given time (1) (except for interrupts, but i won't treat interrupts here). In general it is better not to use delays, except for very simple programs or in initialisation routines (i.e. in stuff you usually put in <code>setup()</code>).</p>

<hr>

<p>Since your delays are quite long I imagine that you don't need much precision (i.e. you don't need to be more precise than a millisecond). So you could do like that:</p>

<pre class="lang-c++ prettyprint-override"><code>void setup() {}

// Times at which we last called the functions
// (These are global variables, which are in general best avoided)
unsigned long lastGetData = 0;
unsigned long lastPostData = 0;

void loop() {

    //                           min.  sec.  milliseconds
    if(millis() - lastGetData &gt;= 4   * 60  * 1000) {

        lastGetData = millis();
        get_Data();
    }

    //                            min.  sec.  milliseconds
    if(millis() - lastPostData &gt;= 60  * 60  * 1000) {

        lastPostData = millis();
        post_Data();
    }
}

void get_Data() {
    // ...
}

void post_Data() {
    // ...
}
</code></pre>

<p><strong>Notes:</strong></p>

<ul>
<li>millis() returns the time since the startup of the Arduino in millisecond. To get more precision you could also use micros() which returns the same but in microseconds (then you would need to multiply the number in the if condition by 1000).</li>
<li>you can add as much functions as you want whit different execution delays. But you must keep every function as short as possible, i.e. you must not use <code>delay()</code>s, because while executing a function if the time to execute another function comes that function will only be called after the end of the current one.</li>
<li>I used the <code>=&gt;</code> sign, and not <code>==</code>, so that even if the loop doesn't evaluates one if condition in the very milliseconds in which it becomes true it can positively evaluate it some milliseconds later (e.g. after the end of another running function).</li>
</ul>
