Sketch Freezes after a few days running

Hi,

Apologies if this is a dumb question though I have am running a Nano with the below sketch to monitor temperature using a DS18B20 sensor and control two relays to either switch on a heat pad or switch on a fridge to control my beer fermentation temp.

After a couple days the Arduino freezes and I assume it is running out of memory, though I am not sure why.

Can some one please assist and tell me where my code is incorrect.

Temp and status of a pump input/output is also displayed.

Note: Ive removed the bitmap code for the images I display to keep the post under 9000 characters.

Cheers guys,

//======================================================================================
//    THIS SKETCH CONTROLS 3 RELAYS FOR HEATING, COOLING AND PUMPING OF WORTH AND BEER 
//=====================================================================================


//===============================================
// INCLUDES ALL REQUIRED HUBS AND LIBRARIES BELOW
//===============================================
#include <Arduino.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <OneWire.h>
#include <DallasTemperature.h>


// Data wire is plugged into pin 10 on the Arduino
#define ONE_WIRE_BUS 10
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

//======================================================================================
//     BELOW IS USED TO DEFINE THE SET POINT AND THE BUTTON USED TO CHANGE THESE POINT
//======================================================================================

int SetPoint = 20; // Initial temperature to start with

//=====================================================================================
//    BELOW USED FOR PUMP SWITCHING
//=====================================================================================

//Pump Switch status inputs and Relay switching
int Pump = "-";
int pumpSwitch = 3;
int pumpRelay = 2; //Connects to Pump Relay
int val = 0; // push value from pin 3
int relayOn = 0; // Relay Status
int pushed = 0; // Push Status

#define Up_Key 6
#define Dn_Key 7

//=========================================================================
//    CONFIGURE OUTPUTS FOR 240V SWITCHING RELAYS
//========================================================================

int HEAT = 5; // pin 5 to run relay for heating
int COOL = 4; // pin 4 to run relay for cooling


//==========================================================================
//  THE BELOW WORKS FOR A SH1106 OLED DISPLAY ON THE U8G2 LIBRARY
//  THIS SETS UP THE OLED TO OPERATE CORRECTLY
//==========================================================================

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);


//=========================================================================
//    THE XBM FILE GOES BELOW - MUST KNOW DEMENSIONS!
//========================================================================

#define newhops_width 63
#define newhops_height 64
static const unsigned char newhops_bits[] PROGMEM = {
 


//=========================================================================
//    THE XBM FILE GOES BELOW - MUST KNOW DEMENSIONS!
//========================================================================

#define happyhop_width 128
#define happyhop_height 64
static const unsigned char happyhop_bits[]PROGMEM = {
  

float temperature; //NOT SURE WHAT THESE ARE FOR YET
char temperatureString[6] = "-"; //NOT SURE WHAT THESE ARE FOR YET

void draw1(void) {
  // graphic commands to redraw the complete screen should be placed here
  u8g2.drawXBMP( 0, 0, newhops_width, newhops_height, newhops_bits); // THESE LINES CALL UPON THE SIZING AT THE TOP OF THE BITMAP CODING.
  u8g2.setFont(u8g2_font_crox2tb_tr);
  u8g2.drawStr(27, 10, "BREW STATS");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(42, 25, "TEMP =");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(90, 25, temperatureString);
  u8g2.setFont(u8g2_font_courB08_tf);
  u8g2.drawStr(106, 22, "o");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(113, 25, "C");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(42, 42, "SET =");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(90, 42);
  u8g2.print(SetPoint);
  u8g2.setFont(u8g2_font_courB08_tf);
  u8g2.drawStr(106, 39, "o");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(113, 42, "C");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(42, 59, "PUMP =");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.drawStr(90, 58, Pump);
}

void draw2(void) {
  u8g2.drawXBMP( 0, 0, happyhop_width, happyhop_height, happyhop_bits);
}

void setup(void) {

  //configure pump operation input with use of pull up resistor and output to relay
  pinMode(pumpSwitch, INPUT_PULLUP);
  pinMode(pumpRelay, OUTPUT); 

  //cofigures outputs to relays
  pinMode(HEAT, OUTPUT);
  pinMode(COOL, OUTPUT);

  //configures the below pins as inputs and enables the internal pull-up resistor
  pinMode(7, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);

  // Define use of Internal LED
  pinMode(LED_BUILTIN, OUTPUT);

  //=========================================
  //  SETS UP THE BUTTON INPUTS
  //=========================================
  pinMode(Up_Key, INPUT);
  pinMode(Dn_Key, INPUT);

  //Pull up for setpoint keys
  digitalWrite(Up_Key, HIGH);
  digitalWrite(Dn_Key, HIGH);


  sensors.begin(); //Start Sensors

  //=========================================
  //    STARTS THE OLED AND DISPLAY
  //=========================================
  u8g2.begin();
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(47, 20, "IT'S");
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(35, 38, "BREW");
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(36, 56, "TIME!");
  } while ( u8g2.nextPage() );
  delay(3000);
  u8g2.firstPage();
  do {
    draw2();
  } while ( u8g2.nextPage() );
  delay(4000);
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(42, 20, "I'M");
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(10, 38, "HOPPING");
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(35, 56, "MAD");
  } while ( u8g2.nextPage() );
  delay(3000);
}

void loop(void) {



  //=========================================
  //     THIS STARTS THE PICTURE LOOP
  //=========================================
  u8g2.firstPage();
  do {
    draw1();
  } while ( u8g2.nextPage() );

  //=========================================
  //     UPDATES THE TEMPERATURES
  //=========================================

  // Read Temperature
  temperature = sensors.getTempCByIndex(0);
  // Update the sensor readings
  sensors.requestTemperatures();
  dtostrf(temperature, 2, 0, temperatureString);

  //read the pushbutton values into a variable
  int sensorVal1 = digitalRead(7);
  int sensorVal2 = digitalRead(6);
  if (sensorVal1 == LOW) {
    SetPoint++;
  }
  if (sensorVal2 == LOW) {
    SetPoint--;
  }


  // Controls LED Output and switiches relays on or off with a 1 Degree Celcius Hysterisis
  if (temperature < SetPoint + 1)  {
    Serial.println("Relay on!");
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(HEAT, HIGH);
  }

  else {
    digitalWrite(HEAT, LOW);
  }

  if (temperature > SetPoint) {
    Serial.println("Relay OFF");
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(COOL, HIGH);
  }

  else {
    digitalWrite(COOL, LOW);
  }

  Serial.print(temperatureString);
  Serial.println(" Deg C");

  Serial.print(SetPoint);
  Serial.println(" Deg C");

  Serial.print(Pump);
  
  //=========================================
  //     CONTROLS THE PUMP RELAY
  //=========================================

  val = digitalRead(pumpSwitch);

  if(val == HIGH && relayOn == LOW) {

    pushed = 1-pushed;
    delay(100);
  }

  relayOn = val;

    if(pushed == HIGH) {
      digitalWrite(pumpRelay, LOW);
      Pump = "OFF";
    }else{
      digitalWrite(pumpRelay, HIGH);
      Pump = "ON";
    }
    

}

What's the last thing you see in your serial output before the crash?

Fridges (especially older ones) are notorious for spiking the power when the compressor kicks in. You may need some defense i.e. a capacitor :wink:

char temperatureString[6] = "-"; //NOT SURE WHAT THESE ARE FOR YET
...
  dtostrf(temperature, 2, 0, temperatureString);

From the dtostrf() docs:

The caller is responsible for providing sufficient storage in s.

Are you sure the temperature string always fits in the 6 bytes that have been allocated? Really, 5 because the last one is always a terminating character.