How to make rtc in ISR?

#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>

RTC_DS1307 RTC;

File myFile;

void setup(){

Serial.begin(38400);

Serial.print("Initializing SD card...");

if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");

Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(F(DATE), F(TIME)));
}

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 = 3124;// = (1610^6) / (11024) - 1 (must be <65536)
// 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.5kHz (takes two cycles for full wave- toggle high then toggle low)
int blueToothVal=0;
if(Serial.available())//if there is data being recieved
{
blueToothVal=0;
blueToothVal=Serial.read(); //read it
}

if(blueToothVal=='1')
{

digitalWrite(5,HIGH);
void loop();
}
}

void loop()
{

DateTime now = RTC.now();
myFile = SD.open("test1.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test1.txt...");

myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(' ');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.println();
delay(1000);

// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}

why rtc is not work in interrupt, help how to change a code.

if(blueToothVal=='1')
  {

    digitalWrite(5,HIGH);
void loop();
  }
}

What a strange place for a function prototype.
But what a good job it is a prototype, and not a call to that function - that would be odd, to say the least.

(Did you see what I did with code tags to enclose the code?
You could do that.
You should do that)

#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>

RTC_DS1307 RTC;

File myFile;


void setup(){
  
Serial.begin(38400);

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

 Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
 
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;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//OCR1A = 3124;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  // 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.5kHz (takes two cycles for full wave- toggle high then toggle low)
   int blueToothVal=0;
  if(Serial.available())//if there is data being recieved
  {
   blueToothVal=0;
   blueToothVal=Serial.read(); //read it
  }

  if(blueToothVal=='1')
  {

    digitalWrite(5,HIGH);
void loop();
  }
}


void loop()
{
  
  
DateTime now = RTC.now();
   myFile = SD.open("test1.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test1.txt...");
     

     
     myFile.print(now.year(), DEC);
     myFile.print('/');
     myFile.print(now.month(), DEC);
     myFile.print('/');
     myFile.print(now.day(), DEC);
     myFile.print(' ');
     myFile.print(now.hour(), DEC);
     myFile.print(':');
     myFile.print(now.minute(), DEC);
     myFile.print(':');
     myFile.print(now.second(), DEC); 
     myFile.println();
     delay(1000);
    
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}
  if(blueToothVal=='1')
  {

    digitalWrite(5,HIGH);
void loop();
  }

see reply #2

You can't, in general, do serial I/O in an ISR, it can hang forever - only do Serial I/O when interrupts are enabled.

MarkT:
You can't, in general, do serial I/O in an ISR, it can hang forever - only do Serial I/O when interrupts are enabled.

Reading serial data is OK. There is something in the buffer to read, or there isn't. In either case, interrupts are not involved.

Of course, there is nothing in the code to explain why serial data needs to be read on a periodic basis.

Serial data should be read in loop(). Of course, the delay() in loop is going to cause problems, but adding interrupt handling so you can stick your head in the sand is silly.

Look at the blink without delay example. Get rid of the delay() AND the interrupt service routine (and the timer diddling code).