Arduino Multiple Timer Interrupts

1,605 views
Skip to first unread message

ldic...@mindspring.com

unread,
Jul 13, 2011, 11:37:03 AM7/13/11
to nycresistormi...@googlegroups.com
Hi Everyone -

Can anyone point me to info on how to use multiple countdown timer interrupts on the Arduino. I am using MsTimer2 library currently, but don't see anything that describes multiple timers. Even though I am using MsTime2, I don't mind switching to another method or library.

I want to do a count down to check a condition, if the condition is true, I want to trigger a relay for 30 seconds. I am a little confused because I can find info on multiple interrupts for hardware interrupts but nothing on timers.

Thanks.

Louis

josh jordan

unread,
Jul 13, 2011, 11:47:49 AM7/13/11
to nycresistormi...@googlegroups.com
You can make 2 interrupts out of 1 interrupt.

Set the time interval to a common denominator, if one is 100ms and the
other is 1 minute, set the interval for 100ms. Have a static variable
for each thing to be timed, for the 1 minute time init this to 600.
Whenever the timer goes off, do the 100ms function, and subtract 1
from the static variable. If the static variable reaches 0, do the 1
minute function and reset it to 600.

-Josh Jordan

> --
> You received this message because you are subscribed to the Google Groups "NYCResistor:Microcontrollers" group.
> To post to this group, send email to nycresistormi...@googlegroups.com.
> To unsubscribe from this group, send email to nycresistormicrocon...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nycresistormicrocontrollers?hl=en.
>
>

ldic...@mindspring.com

unread,
Jul 13, 2011, 4:39:35 PM7/13/11
to nycresistormi...@googlegroups.com
Thanks for the info! I pasted my code below - does it look right? I want to do a check every 60 seconds (first timer) and have the resulting event happen for 30 seconds (second timer).

Thanks.

Louis

#include <MsTimer2.h>

int timerCounter1 = 6; //wait 60 sec
int timerCounter2 = 3; //wait 30 sec

void checkTime()
{

if(timerCounter1 > 0)
{
timerCounter--;
}
else
{
doThirtySeconds();
timerCounter1 = 6;
}
}

void setup()
{
MsTimer2::set(10000, checkTime); // 10s period
MsTimer2::start();
}

void loop()
{

}

void doThirtySeconds()
{
if(timerCounter2 > 0)
{
digitalWrite(10,HIGH);
timerCounter2--;
}
else
{
digitalWrite(10,LOW);
timerCounter2 = 3;

Jace van Auken

unread,
Jul 15, 2011, 3:04:44 PM7/15/11
to nycresistormi...@googlegroups.com
Hey Louis,

Not sure if you figured out your problem.  I've been working with Timer interrupts this past week so they're fresh in my head.  It looks like you implemented MsTimer2 just fine, but I'm not sure what your timing scheme is.

Are looking for the relay to be off for 60 seconds and then on for 30 seconds in a repetitive loop?  Right now you have the doThirtySeconds() function nested inside the checkTime function in such a way the relay will be on 180 seconds.  This can be easily fixed by moving 'timerCounter1 = 6' into the else portion of the condition in doThirtySeconds.  You can also achieve this timing setup with a single variable and one function if it's what you're going for.

If you're going for another timer scheme, I'm not sure what that is.  If you can be more specific about when you want each event to happen that would help.

JvA

reddi pavan

unread,
Jul 16, 2011, 11:24:30 AM7/16/11
to nycresistormi...@googlegroups.com
u can use 555timer connected to a microcontroller. by giving a clock
pulse to a 555 timer u can adjust the time as your wish.this is my
suggestion. it'll be cheaper also.............
Reply all
Reply to author
Forward
0 new messages