Will the system be doing anything during those breaks?
If not, then I know at least one of the following three should work: Note: I do not know if the compiler supports recursive functions (for (2)). Also, delay() apparently is a hog, so almost nothing else will go on.
- You can do something similar to the for loop using delay() as I first pointed out. Or use delay() somehow.
- Eliminate the loop, but still use delay() inside a recursive function. This is the basic setup.
//----First way. Uses external counter.----
int ticker = 0; //this is your counter for counting up to 10 times.
void tick()
{
delay(60000); //wait for 1 minute
ticker++; // just incrementing the count after the wait
if (ticker > 9)
{
// What to do once the 10 times are complete.
}
else
{
tick(); // Calls the function again, effectively acting like a loop. Will still cancel on ticker>9, because the condition is there with each call.
}
}
//----Second way. Same idea, but different..----
int ticker = 0; //this is your counter for counting up to 10 times.
void tick()
{
delay(60000); //wait for 1 minute
ticker++; // just incrementing the count after the wait
if (ticker <= 9) // different condition. Basically just saying that if the counter isn't at 9 yet, keep ticking.
{
tick();
}
}
//----Third way. Uses recursion with passed parameters.----
void tick(unsigned int ticker)
{
delay(60000); //wait for 1 minute
if (ticker <= 9)
{
ticker++;
tick(ticker); //recursively passes the increasing value.
}
}
//----Final way. Same as 3, but more compacted----
void tick(unsigned int ticker)
{
delay(60000); //wait for 1 minute
if (ticker <= 9)
tick(ticker++); //recursively passes the increasing value.
}
- Use millis(). Record what millis() is when you start, then use delay() or something while looping to to check if currentMillis (that is, call millis()) >= startMillis+miniInterval (where miniInterval is the time between each event in milliseconds)
If other things are going on during the time interval, OR if you are trying to save power, then a hardware implementation will work better. As said, the basic idea is to oscillate on an interrupt pin. You'd have it set up similar to the following: Note: Use http://arduino.cc/en/Reference/AttachInterrupt for reference on interrupts.
unsigned long miniCounter = 0;
unsigned long threshold = 960000000; // will allow miniCounter to go up to a full minute
// the above comes from (crystal speed in Hz) * (desired number of seconds)
// I assumed 16MHz (16000000Hz) and 60 seconds.
unsigned int wholeConter = 0;
void tick()
{
miniCounter++;
if (miniCounter >= threshold)
{
miniCounter = 0;
wholeCounter++;
}
}
void setup()
{
attachInterrupt(0, tick, CHANGE);
/*----Parameters
* 0 The interrupt number. Not the pin number. Use Google on the Arduino Interrupt reference to see which pin corresponds to which interrupt. 0 is pin2.
* tick Name of the function to run when the interrupt pin experience the third parameter. Must have no parameters. Must not return a value (must be declared as void)
* CHANGE
Tells to run the function (tick) when the value on the interrupt (0, or pin2) changes. That is, goes from LOW to HIGH, or HIGH to LOW.
*/----
}
That provides just the basic layout for what you could do.
Now, about those other timers in the 328. Those might be for PWM (Pulse-width modulation, if you're not familar with it. You might want to look it up if you're not, but don't get scared of it. analogWrite() makes PWM extremely easy).
If they are used for PWM only, then I don't think you would have access to them.
Though that gives me an idea. analogWrite() to an interrupt pin. Should oscillate fairly nice at 50% duty cycle! (gives equal time for HIGH and LOW output, or "true" and "false" for the CHANGE interrupt to happen). Of course though, I would not think it a great idea to hook a PWM pin directly to another Arduino pin...
As a little note, don't worry about using millis(). It will run for 49.71 days according to my calculations (assuming it returns an unsigned long int). This matches the Arduino reference, which states that it overflows after 50 days. Link here: millis() - Arduino Reference
Might want to bookmark the Reference page if you haven't. It's nice to have around if you've forgotton some basic functions, or if you need parameters/functionality clarified. That would be at Arduino - Home
I hope this helps
keep in mind I'm not exactly an Arduino programmer though.
Just saw your new replies. Looks perfect on a quick look. I'll check back if I see anything worth noting... but otherwise it looks good to use! Nice find.