Delay for on off time in minutes?
maximum delay in arduino an how to make it
How do I make a motor spin every 10 minutes? - Arduino Stack Exchange
My Arduino stops after 1 minute wait - Stack Overflow
Videos
For a simple interval 5 mins on, 15 mins off, repeat until powered off, can I just use the delay function?
Just checking to make sure it won’t hang during the timing.
Thanks
The simplest option is to use delay() to implement the waits:
void loop() {
turn_motor_on();
delay(10000); // 10 seconds
turn_motor_off();
delay(600000); // 10 minutes
}
Since these long numbers may be difficult to read, you can make them more readable by defining some units:
const unsigned long second = 1000;
const unsigned long minute = 60000;
Then you can for example delay(10 * minute);
Beware there is a trap if you try to do arithmetics with integer literals. The following:
delay(10 * 60 * 1000); // BUG!
will not work on most Arduinos, as the multiplications will be
performed on the int data type, which can only hold numbers up to
32,767. It is safer to use either explicitly typed constants (unsigned long is the appropriate type for delays) or integer literals typed with
the suffix UL (which stands for “unsigned long”).
Note that the Arduino will do no useful work while delaying. If you want to do any kind of work during the delay, see the “Blink Without Delay” Arduino example.
Yes, of course it's possible. The loop() function might look something like:
loop(){
turn_motor_on();
wait_seconds(10);
turn_motor_off();
wait_minutes(10);
}
Now all you have to do is write your turn_motor_on() and off functions and also wait_seconds() and wait_minutes(). I'll leave the motor controls to you since you have not provided any detail on how the motor is hooked up.
You could use the Arduino delay() function:
void wait_seconds(int num_seconds){
for(int i=0; i<num_seconds; i++){
delay(1000);
}
}
void wait_minutes(int num_minutes){
for(int i=0; i<num_minutes; i++){
delay_seconds(60);
}
}
Of course there are many other ways to do this but this method is simple and easy to understand and will get you started easily.
It seems that you need to explicitly set numeric literals to long (L) and they use them. Otherwise it does not work. If anyone can explain if there is any kind of automatic conversion it will be awesome but until then simply use:
unsigned long seconds = 1000L; // !!! SEE THE CAPITAL "L" USED!!!
unsigned long minutes = seconds * 60;
unsigned long hours = minutes * 60;
and then simply use delay(millisec) as usual:
delay(5 * minutes);
It worked for me.
in your line:
unsigned long pausetime = 1000 * 60; // 1 minute
the Arduino will look at 1000 (integer) and 60 (integer) and so will work out an answer that it will try to slot into... an integer! This means the biggest answer it can give to pausetime is 32,767. Anything bigger than this will wrap round, so 60,000 minus two lots of 32,768 comes out at -5536.
To see it in action add Serial.print(1000 * 60); to the setup and watch in your Tools>Serial Monitor:
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.print(1000 * 60);
}
To force the Arduino to use your constants as unsigned longs add ul or UL to the end of the number.
Yes, absolutely, it should work. But your code will be easier to read if you use some constants, like:
const unsigned long SECOND = 1000;
const unsigned long HOUR = 3600*SECOND;
delay(7*HOUR);
Yes you can write delay(25200000UL) and it will delay for 7 hours.
How it works
delay(x) will delay for x number of milliseconds.
1 second = 1000 milliseconds
1 minute = 60 seconds
1 hour = 60 minutes
7 hours = 1000 * 60 * 60 * 7 = 25,200,000
This number is quite large but is well within the scope of an unsigned long: 32 bits = (2^32)-1 = 4,294,967,295. Doing the sums, this will last for 49 days and 17 hours.
Delaying for >49 days
If for some reason you needed to go longer than 49 days, you can do so by placing the delay inside a for loop.
const unsigned long ONE_DAY = 24UL*3600*1000;
const unsigned int numOfDays = 51;
int i;
int j;
for( i=0; i<numOfDays; i++ )
{
delay(ONE_DAY);
}
The real time clock method is the most accurate way but otherwise use millis
unsigned long startMillis = millis();
while (millis() - startMillis < LONG_DELAY_MS);
This will delay up to approx. 4294967295ms (2^32-1) or 49 days, after which the timer will catch up to the value of startMillis
delay() has its uses, but for long delays it's no good. It simply tells the microcontroller to do nothing for x clock cycles. During that time, your Arduino can't do anything else.
Your best bet would be to use a thing called a Real Time Clock (RTC). These chips are specifically made to keep track of time, and you can connect them to your Arduino with ease. Here's an example of how you could do that.