Counter/Timer Help

I'm still new to this Arduino thing, so please bare with me.

My goals:
I need to the arduino to cycle the output from a pin (lets say 13) for around 60 seconds, every hour.

I originally said "No problem, I'll just use the delay function." Well, it turns out there must be a max amount of time you can delay a function.

So, can I instead start a variable (lets call the variable "counter") and set it = 1, then inside the loop add one to the existing number with a delay of one second. Then when I reach a certain value, like 60, execute a command? After an hour, I would like to reset the variable also.

The main problem I'm running into with this is the language.

I'm aware of the timing functions as well, but I'm not very competent in this programming quite yet to understand them.

Or just use millis(). See BlinkWithoutDelay. But keep in mind, the Arduino is not a very accurate timekeeper!

You say it isn't very accurate, by how much would you say? Lets say in 12 hours, how many seconds would be off compared to a stop watch?

Also, I will look into millis()

I would not advise the use of delay() and when you say

Well, it turns out there must be a max amount of time you can delay a function.

you are right, but the maximum time is a little over 49 days so I do not know why you had a problem with a delay of one hour.

As a matter of interest how did you express the number of milliseconds used as a parameter for the delay() ?

Depends on the oscillator used, voltage, temperature etc. But if seconds (or tens of seconds) matter you don't want to use the internal timing of the Arduino for this. Better is to use a RTC. You don't even need to set the time correct if you don't want to. Just use the 1Hz output of it, his will be accurate :slight_smile:

And the delay can handle 1 hour. but you need to use the unsigned long suffix for the constant. Like: delay(60000UL);. But yeay, the Arduino becomes a pretty useless device now...

I was only trying to delay for a minute when I was running into issues. I tried to express it in terms of delay(1000x). I believe in this case I did delay (100060), in which it ran infinitely. I"m thinking it doesn't appreciate my terminology. There is also the possibility I messed up somewhere else.

My problem with millis() is that from what I understand, you cannot reset it. I'm wanting to loop an output for roughly a minute every hour for 12-24 hours. Instead of writing a line of code over ~24 times for every hour of the day, I'd rather just have a function for one hour, then loop it. It will be a lot easier when I have to edit the time I'm cycling the output.

My problem with millis() is that from what I understand, you cannot reset it.

You don't have to. Save the start time from millis() in an unsigned long variable then every time through loop() check whether millis() - start time >= required interval (in milliseconds). If so, do what you need to, if not keep going round loop()

In your case you will have 3 required intervals.

One of 1 hour (60 * 60 * 1000UL) 60 minutes each of 60 seconds each of 1000 milliseconds. The UL will force the calculation to result in an unsigned long result which is where you went wrong last time

One of 1 minute (60 * 1000UL) 60 seconds each of 1000 milliseconds

and

One of 12 hours (12 * 60 * 60 * 1000UL)

This might help
Several things at the same time