Program for terrarium control freezes after a while

Hi,
I was working on a project for terrarium control and I run into some problems.It’s kinda working but after some time heating pad control just stops working and it runs at max power,resulting in temperatures that are way too high.The temperature at the sensor should be at constant 60°C (It may sound too high but sensor is mounted really close to heating element and temperature on outside of heater is perfect when it’s at that temperature).Could anyone help me to fix that problem as soon as possible,also any suggestions would be apriciated.Here is my code

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 10  //pin 10
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <Wire.h>
const int DS1307 = 0x68; // Address of DS1307 see data sheets
const char* days[] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const char* months[] =
{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

// Initializes all values:
byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;

void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);      // Heater pin 
  pinMode(9, OUTPUT);      // Lamp relay pin
  sensors.begin();
  Wire.begin();
  Serial.begin(9600);
  delay(2000); // This delay allows the MCU to read the current date and time.

  Serial.print("The current date and time is: ");
  printTime();
  Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor");
  Serial.println("Would you like to set the date and time now? Y/N");

  while (!Serial.available()) delay(10);
  if (Serial.read() == 'y' || Serial.read() == 'Y')

    // This set of functions allows the user to change the date and time
  {
    Serial.read();
    setTime();
    Serial.print("The current date and time is now: ");
    printTime();
  }


  Serial.println("Thank you.");
}

// Continuous function for converting bytes to decimals and vice versa
void loop() {
  readTime();
  if (hour > 7 && hour < 21) {        //if time is between 7AM and 9PM lights should be on
    readTime();
    digitalWrite(9, HIGH);
  }

  else {
    digitalWrite(9, LOW);              //else light is off
  }
  sensors.requestTemperatures(); // Send the command to get temperature readings
  if (sensors.getTempCByIndex(0) <= 60) {
    sensors.requestTemperatures();
    digitalWrite(8, HIGH);
    Serial.println("Grijanje upaljeno"); // println("Heating on");
    sensors.requestTemperatures();
  }
  else {
    digitalWrite(8, LOW);
    Serial.println("Grijanje ugaseno"); //println("heating off");
    sensors.requestTemperatures();
  }
  Serial.print("Temperature is: ");
  Serial.println(sensors.getTempCByIndex(0));
  delay(750);
}
byte decToBcd(byte val) {
  return ((val / 10 * 16) + (val % 10));
}
byte bcdToDec(byte val) {
  return ((val / 16 * 10) + (val % 16));
}


// This set of codes is allows input of data
void setTime() {
  Serial.print("Please enter the current year, 00-99. - ");
  year = readByte();
  Serial.println(year);
  Serial.print("Please enter the current month, 1-12. - ");
  month = readByte();
  Serial.println(months[month - 1]);
  Serial.print("Please enter the current day of the month, 1-31. - ");
  monthday = readByte();
  Serial.println(monthday);
  Serial.println("Please enter the current day of the week, 1-7.");
  Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat - ");
  weekday = readByte();
  Serial.println(days[weekday - 1]);
  Serial.print("Please enter the current hour in 24hr format, 0-23. - ");
  hour = readByte();
  Serial.println(hour);
  Serial.print("Please enter the current minute, 0-59. - ");
  minute = readByte();
  Serial.println(minute);
  second = 0;
  Serial.println("The data has been entered.");

  // The following codes transmits the data to the RTC
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekday));
  Wire.write(decToBcd(monthday));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(byte(0));
  Wire.endTransmission();
  // Ends transmission of data
}


byte readByte() {
  while (!Serial.available()) delay(10);
  byte reading = 0;
  byte incomingByte = Serial.read();
  while (incomingByte != '\n') {
    if (incomingByte >= '0' && incomingByte <= '9')
      reading = reading * 10 + (incomingByte - '0');
    else;
    incomingByte = Serial.read();
  }
  Serial.flush();
  return reading;
}


void printTime() {
  char buffer[3];
  const char* AMPM = 0;
  readTime();
  Serial.print(days[weekday - 1]);
  Serial.print(" ");
  Serial.print(months[month - 1]);
  Serial.print(" ");
  Serial.print(monthday);
  Serial.print(", 20");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  sprintf(buffer, "%02d", minute);
  Serial.print(buffer);
}


void readTime() {
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 7);
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read());
  weekday = bcdToDec(Wire.read());
  monthday = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}
if (Serial.read() == 'y' || Serial.read() == 'Y')

Uh-oh.
Read once, test twice.

Put a check to make sure you are not accessing invalid address:

  month = readByte();
  Serial.println(months[month - 1]);

Note: Not just this but everywhere are you are accessing arrays.

This is weird here:

const char* AMPM = 0;

I just tried this and not working any better

    if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if ( ( incomingByte == 'y' ) || ( incomingByte == 'Y' )) {

      // This set of functions allows the user to change the date and time
      Serial.read();
      setTime();
      Serial.print("The current date and time is now: ");
      printTime();
    }
    else {
      incomingByte = 0;
    }
  }

Also that part of code for accessing arrays was not written by me and I don't really understand that part really good,any suggestions would be apriciated

That’s a bad thing. If you steal or borrow code, it’s not a free ride, you should understand it before using, or your program will probably do unexpected things (like ‘freeze’j.

We all,re-use other code... that’s how we learn, but the critical word here is learn.

The other thing I noticed was the large number of big string literals - I’d be looking at better use of the F() macro.