question about timers and interrupts

Hi guys,

I've been reading about this subjects and for what i understand there are only 3 timers available (and probably we cant use them all)

What I would like to know is if it's possible to use a timer as an interrupt several times. The point is to have 7 pir sensors (one in each pin) and when one is active a timer would start for that sensor (and restart every time), and all sensors could be active at the same time.

First time I thought about this I was planning to use a RTC (calculating time diferences), but I didn't know about internal timers and interrupts.

I managed to figure out how to use 7 interrupts, but I cant interface that with the timers for each sensor.

Any help would be appreciated.

It isn't clear what you need the timers for. It looks like you want to keep track of how long a sensor is high (or low). If that is the case, simply use millis() to record when the state of a pin changes. Compare that time to "now" (again using millis()) each time you want to know how long the pin has been high or low.

No interrupts needed.

Assuming you just need to measure the time between two interrupts for a sensor, just call the function micros() at the beginning of the interrupt and the calculate the time with:

unsigned long currenttime = micros();
if (Sensor x starting of signal processing) {
    sensorstart[x] = currenttime;
}
else if (Sensor x end of signal processing) {
    sensortime[x] = currenttime - sensorstart[x];
}

This will work with the default timer as long as a resolution of 4 µs is good enough. If you need things a lot less precise, replace micros() with millis().

That should do work for most applications well enough.

Korman

Sorry, I didn't explain my self right.

The goal is to have PIR sensors for every room in my house so every time I walk in that room an LED strip would light on. But I want to control the time the LED stays on, and after a specific time (different for each PIR) the light would turn off automatically. (Like in the bathroom maybe I configure for a 5 minutes shutdown time, and the hall only 1 minute.) I have 7 areas I want to use PIR. So I guess I would need 7 timers to control each room individually, or could I do it with just one? It would be very nice to have an interrupt for each area wouldn't it?

My idea, if possible would be something like:

  • Create 7 different interrupts for each sensor pin to start/restart the timer (the timeout is different in each sensor)
  • When a specific timeout occurs, I would now it as an “event” to shut down the light in a specific area.

I think now it's a bit clear what I want.

Thank you both for your input.

So I guess I would need 7 timers to control each room individually, or could I do it with just one? It would be very nice to have an interrupt for each area wouldn't it?

Suppose you needed to run around after your kid, turning lights off after he turned them on. The interval between his turning the light on and your turning it off can be different for each light.

Would you need to wear 7 watches to turn the 7 lights off at the right time, or could you just use a clipboard to write down when he turned each light on, and use one watch to determine if it was time to turn a light off?

Yes, I understand your point. That was my first idea actually.

But how can I trigger an event like function when I calculate the timeout in my function?

But how can I trigger an event like function when I calculate the timeout in my function?

Exactly the same way that you trigger an event when the PIR sensor says to turn the light on.

We really need a button that inserts the link to Tutorial/BlinkWithoutDelay.

Look at that example and imagine doing it with 7 LEDs, 7 variables and perhaps even 7 different intervals.

Korman