Timer interrupt working

Hi friends,
I am learning timer interrupt,i want to switch on an led for one minute and turn off,but in this code i am not able to switch off the led at 1minute,led is turning off before one minute that is around 45 second.Please tell me the working of interrupt.

According to code every one second the timer is entering in to the interrupt service routine right.
Please help me

int fklc=0,int fk=60;//fk=60 for one minute
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
cli();//stop interrupts

//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (1610^6) / (11024) - 1 (must be <65536)
// OCR1A = 7812;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);

sei();//allow interrupts
//cli();
}//end setup

ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5Hz (takes two cycles for full wave- toggle high then toggle low)
char blueToothVal;

if(Serial.available())//if there is data being recieved
{
blueToothVal=Serial.read(); //read it
}

if(blueToothVal==‘a’)//if value from bluetooth serial is n
{

digitalWrite(13,HIGH); //switch on LED
Serial.println(F(“Relay3 is on”)); //print LED is on

}
fklc=fklc+1;
if(fklc>=fk)
{
if(digitalRead(13))
{
digitalWrite(13,LOW);
Serial.println(F(“Relay3 is OFF”));
fklc=0;

}
}
}

void loop(){

}

Doing Serial IO in an ISR is too dumb for words. Stop that.

Posting code that actually compiles would be a start.

Just use your ISR to change the value of a global variable.

Then the code in loop() can check that variable and do stuff (such as printing) if the value has changed. For example

ISR(TIMER1_COMPA_vect){
   timerHasTripped = true;
}

…R

Firstly you should not do anything in an ISR that takes time and cannot do anything that waits
for another interrupt (unless you re-enable global interrupts). Serial takes time and waits for
interrupts...

Secondly your logic for reading bluetooth is broken - you conditionally read a byte into
blueToothVal, then unconditionally assume its the newly read value when it will almost
always be the last byte read however long ago.

thanks for your reply
if i not do anything in ISR than what is the use of ISR then what task has to be written in ISR.

if i not do anything in ISR than what is the use of ISR

Serial data arrival triggers interrupts. Each bit is copied in an interrupt. When all 8 have arrived, the byte is stored in the appropriate array. Quick and useful.

You can do lots of things in an ISR. But, the things you can do must be fast and must not require that interrupts be enabled.

I am learning timer interrupt,i want to switch on an led for one minute and turn off,but in this code i am not able to switch off the led at 1minute,led is turning off before one minute that is around 45 second.

NOTHING in that statement says anything about sending and receiving serial data. So, why are you?

Switching an LED on in an ISR is fine. Switching an LED off in an ISR is fine.

thanks pauls
then what i written code in ISR is right naa,ISR is used to perfom a task for one second in my code,it's for one second or half second?.

ISR is used to perfom a task for one second in my code

NO! The ISR is called when the timer ticks to the right time. ALL that the ISR does is turn the LED on or off. NOTHING else. No sitting around for one second, waiting to turn the LED back off.

ok Pauls then the serial receive part has to be written in void loop and only turn on and turn off part is written in ISR right.

thanks

rahulsubbu:
ok Pauls then the serial receive part has to be written in void loop and only turn on and turn off part is written in ISR right.

thanks

Yes.

Normally an interrupt is used when an external condition has to be handled fast or you need to
generate output with accurate timing. That usually means < 1ms.

To do something once a second interrupts are not needed - your loop() should be running frequently
enough to test for when each second goes by.

If you need to do something once a second with 10 microsecond precision, then you definitely
need to use an ISR to do it. And if you want to play fair with other ISRs you should try and
keep your ISR’s execution time down to 10 microseconds too! Or may be 100us if nothing
else is that fussy.

The timer0 interrupts run about every 1ms to maintain millis(), micros() and, delay(). The accuracy
of these functions also depends on your ISR not hogging the processor for too long…