RTC timer 1/2 hour behind

Hello, i can’t figure out why my RTC timer keeps showing up 1/2 hour behind my computers time… help please!!! :frowning:

//DS18B20 (Pin 2)

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

//LCD (Pin A4 & A5)

#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

//RTC (Pin A4 & A5)

#include <Wire.h>
#include “RTClib.h”
RTC_DS1307 RTC;
char daysOfTheWeek[7][12] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

//Heater (Pin 4)

const int heatRight = 4;

void setup()

{

//DS18B20 (Pin 2)

Serial.begin(9600);
sensors.begin();

//LCD (Pin A4 & A5)

lcd.begin (20,4);

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home ();

//RTC (Pin A4 & A5)

Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println(“RTC is NOT running!”);
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(DATE, TIME));
}

pinMode(heatRight, OUTPUT);

}
void loop()

{

int temp = sensors.getTempCByIndex(0);

//DS18B20 (Pin 2)

sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.print(“Temperature:”);
Serial.println(sensors.getTempCByIndex(0)); // 0 refers to the first IC on the wire
Serial.println(sensors.getTempCByIndex(1));
delay(200);

//LCD (Pin A4 & A5)

DateTime now = RTC.now();
lcd.setCursor ( 1, 0 );
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" “);
lcd.print(now.day(), DEC);
lcd.print(”,");
lcd.print(now.month(), DEC);
lcd.print(",");
lcd.print(now.year(), DEC);
lcd.setCursor ( 6, 1 );
lcd.print(now.hour(), DEC);
lcd.print(’:’);
lcd.print(now.minute(), DEC);
lcd.print(’:’);
lcd.print(now.second(), DEC);
lcd.setCursor ( 0, 2 );
lcd.print(“Temp. Left: “);
lcd.print(sensors.getTempCByIndex(0));
lcd.print(” C”);
lcd.setCursor ( 0, 3 );
lcd.print(“Temp. Right: “);
lcd.print(sensors.getTempCByIndex(1));
lcd.print(” C”);

// Backlight on/off every 3 seconds
// lcd.setCursor (14,3); // go col 14 of line 3
// lcd.print(n++,DEC);
// lcd.setBacklight(LOW); // Backlight off
// delay(3000);
//lcd.setBacklight(HIGH); // Backlight on
// delay(3000);

//RTC (Pin A4 & A5)

Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(’,’);
Serial.print(now.day(), DEC);
Serial.print(’/’);
Serial.print(now.month(), DEC);
Serial.print(’/’);
Serial.print(now.year(), DEC);
Serial.print(’/’);
Serial.print(’ ‘);
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.print(now.second(), DEC);
Serial.println();
delay(50);

//Heater (Pin 4)

if (sensors.getTempCByIndex(0) < 27.00) {
digitalWrite(heatRight, LOW);
}else if(sensors.getTempCByIndex(0) > 27.00){
digitalWrite(heatRight, HIGH);
}
}

//Lights (Pin 7,8,12,13)

Please start by editing your post

Type [code] before the code
Type [/code] after the code

Next learn to properly indent your code; the IDE menu tools -> autoformat makes that easy.

if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

This resets the time to the time you compiled the code EVERY TIME you reset the Arduino.

Have you by chance reset the Arduino 30 minutes after you compiled the code?

Do you have something plugged into digital pin 0 or 1? I had the same problem and disconnecting components from those pins made my RTC sync correctly to computer time

sterretje:
Please start by editing your post
Next learn to properly indent your code; the IDE menu tools -> autoformat

And while you are at it, you might get rid of the excessive blank lines

Try uploading the program then comment out the following line:

RTC.adjust(DateTime(DATE, TIME));

Then reload the program, provided you keep power on the RTC (normally has a battery back up) the RTC should hold it's time.