using Arduino Uno with RTC and temprature sensor

hi can you help me ? please

#include <SD.h>
#include <RTClib.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
RTC_DS3231 rtcPCF;
File myFile;
#define ONE_WIRE_BUS 2
#define SDFILE_PIN_CS 10

OneWire oneWire(ONE_WIRE_BUS);
 
DallasTemperature sensors(&oneWire);
float temp;

void setup() {
  Serial.begin(115200);
  while (!Serial) //{; // wait for serial port to connect. Needed for native USB port only }
  sensors.begin();
   Wire.begin();
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  
 }

void loop() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0); 
  DateTime now= rtcPCF.now();
  myFile = SD.open("temp.txt", FILE_WRITE);
  if(myFile)
  {
    

    Serial.print(temp);
    myFile.print(temp);
    myFile.print(',');
    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();
   myFile.close();
   Serial.print(',');
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  delay(5000);
  }
    
    else {
    // if the file didn't open, print an error:
    Serial.println("error opening temp.txt");
    }
    
 }

after every 3 or 4 reading I am getting next reading at interval of 6 seconds. please can you suggest a possible solution

You have a delay(5000) that suspends execution for 5 seconds in the if(myFile) code…

I don't think that's the problem, Railroader.

I think the OP is expecting to see the timestamp on each reading, in the file written to the SD card, to be 5 seconds apart, but occasionally one reading is 6 seconds.

@rahul4627, taking the temperature, formatting and writing the readings to the SD card will take a fraction of a second. Because your delay(5000) is always exactly 5 seconds, every few readings, the time will have skipped an extra second. If you want to avoid this, you must use millis() instead of delay() so that you can achieve exactly 5s intervals.

Also, there are ways to write to the SD card and serial monitor that will make your code much shorter and less repetitive. Let me know if you are interested in that.

@PaulRB
I agree totaly. Interval is 6 instead of 5 seconds and the reason is obvious, like You say.

if one wanted to store at exactly 5.000 seconds,
wouldn't the timing need to be by the clock, not by millis() ?
or some combination using the clock to re-adjust ?

do the reading of temperature, in the idle time in that 5 seconds.

but if you use
if (millis() - lastTime >=5000 )

you will always be at 5.000 or longer.
the or longer bit will add up over time.
over days/weeks , even the milliseconds would add up to a second.

with 86,400 seconds in a day, and 5 second write intervals, that is 17,280 writes in a day.

unsigned long start_time = 0;
setup
{
}

loop
{

if( millis() >= start_time)
{
make a reading;
start_time += 5000;
}