Timer related problem

I’m newbie in Arduino platform. I have done a sketch on Arduino Mega which will take external interrupt on RISING edge on PIN2 and as an indicator do high the PIN13. When PIN2 gets logic level LOW, It scan for 15 seconds. If PIN2 is low for 15 seconds it will do PIN13 LOW. That’s why I used timer 1. Interrupt works fine. While PIN2 gets LOW, PIN13 gets LOW before 15 seconds. BUT I think timer is okay. What’s the problem actually?

TT.ino (1.36 KB)

What’s the problem actually?

Lots of them, actually. You should attach the interrupt handler ONCE and leave it attached.

pin and pin1 are dumb names. Why not give them meaningful names, like ledPin and intPin?

You should be using CHANGE as the interrupt type, so you can detect when it goes LOW, too.
There is no reason to be using a timer. Record, using millis() or micros(), when the interrupt happens, specifically when it goes HIGH and when it goes LOW. Periodically (read that as on every pass through loop()), see if the pin is LOW and has been LOW for the required time.

Thanx Pauls for valuable reply. I have tried to modify the sketch according to u. But still problem remains. Interrupt works fine. While logic level is low, millis() doesn’t wait for 15 sec to low the ledPin13. It immediately lowed the ledPin.

TT.ino (1.08 KB)

 if(millis()-startTime == 15000)

Bad idea expecting exact matches. You should be using >=.

Your interrupt handler gets called, now, when the pin goes HIGH OR LOW. You don't distinguish, in the handler, which change happened.

Only first interrupt work properly, then happens same problem.