 How do I make arduino read for 15 mins then turns off or idle for another 45 mins, then loops back again and again?

thank you so much guys

15 mins = 900 sec = 900,000 milliseconds.
45 mins = 2700 sec = 2,700,000 milliseconds

If you use the millis() function (recommended) you can run the program for 50 days.
pseudo code (won’t actually work, just an idea):

``````void loop() {
long time = millis() + 900000; // time var. is 900,000 milliseconds from now
while(millis() < time) {
}
time = millis() + 2700000; // same as before
while(millis() < time) {} //wait, idle, do nothing
}
``````

Good Luck!

I think it will work better if you declare the timer elements as
unsigned long data types.
That way you get the full 32 databits to work with and can go 49 days. (2^32 millisecond)

Subtraction will work better also. When you’ve crossed the rollover boundary, then subtracting the new low value from the prior value will give a valid result:
0000 0010 - FFFF FFE0 for example will yield 0000 0030

Put the parts before void setup() and declare pins in void setup() as needed:

``````unsigned long current_time = 0;
unsigned long prior_time = 0;
unsigned long elapsed_time = 0;
unsigned long duration1 = 900000;
unsigned long duration2 = 2700000;

current_time = millis();  // capture the current time
elapsed_time = current_time - prior_time;  // figure out how long has gone by
while (elapsed_time < duration1) {  // see if the duration has passed
//other actions
prior_time = current_time;  // store the current time
current_time = millis(); capture the new current time
elapsed_time = current_time - prior_time;  // see how much time passed for the while test

}  // back to while comparison

// on to 2nd period, same comments apply
// reset the time elements
current_time = millis();
prior_time = current_time
elapsed_time = current_time - prior_time;  // so, 0 to start
while (elapsed_time < duration2) {
// hang out in this loop
prior_time = current_time;
current_time = millis(); capture the new current time
elapsed_time = current_time - prior_time;

}
``````
``````unsigned long duration2 = 2700000;
``````

Even though the value is being stored in an unsigned long, the compiler will still treat it as an int, because it is not explicitly typed correctly.

``````unsigned long duration2 = 2700000[color=orange]UL[/color];
``````

The value here IS explicitly typed correctly.

I didn’t know that. I’m gonna have to revisit my scoring machines & see if I did that, or if I just compared to a number in the if () statement. I betcha I just have something like duration = 100; (checking for uS gone by). That’d be easy to fix - gonna have to take my laptop to the fencing club & do some downloading if that’s what I did. Thanks Paul!

I betcha I just have something like duration = 100;

As long as the value IS an int, there is no problem. 2700000 is NOT an int.

Thank you very much for your help guys. I appreciate it. :)

PaulS: ``` unsigned long duration2 = 2700000; ```

Even though the value is being stored in an unsigned long, the compiler will still treat it as an int, because it is not explicitly typed correctly.

``````unsigned long duration2 = 2700000[color=orange]UL[/color];
``````

The value here IS explicitly typed correctly.

I've never had to explicitly cast a simple assignment. The following test code works perfectly fine for me:

``````unsigned long temp = 2700000;

void setup(){

Serial.begin(115200);
}
void loop(){

Serial.println(temp, DEC);
temp++
delay(2000);

}
``````

Hi! I have a problem making my prior_time constant cause it updates every loop. pls help me make it constant, thanks.

here is the code snippet

while (elapsed_time < 9000) {
prior_time = current_time;
current_time = millis();
elapsed_time = current_time - prior_time;

}

Looks to me like you want to switch that logic around 180 degrees. Do something when the elapsed time is greater than 9000. Otherwise, do nothing.

``````  current_time = millis();
elapsed_time = current_time - prior_time;
if(elapsed_time >= 9000)
{