Arduino Freezes, gets stuck in a loop and stops sending/receiving data.

Hi guys, I’m working on a simple project, I just want to control heaters using the SHT31dD Adafruit sensors, and display this data to a LCD screen, and depending on the user input, the thresholds needs to adjust.

Everything seemingly works fine, but after a while, the Arduino freezes and my data stops transmitting. I can also see that the serial monitor stops displaying its output and my heaters go haywire.

I’ve initially thought that it was a buffer overflow, but I determined the correct value and all is working fine.

I suspect the problem may lay with my variable initialization and my usage of the dtostrf function.

Any help would be greatly appreciated.

I’m new to using the forum.

#include "DEV_Config.h"
#include "LCD_Driver.h"
#include "LCD_GUI.h"
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <avr/wht.h>

float t1 = 0;
float t2 = 0;
float temp = 0;
float h1 = 0;
float h2 = 0;
float humidity = 0;

Adafruit_SHT31 sht31a = Adafruit_SHT31();
Adafruit_SHT31 sht31b = Adafruit_SHT31();

const int up = 17;                                                        /*Knoppies*/
const int down = 18;
const int left = 16;
const int right = 19;
const int settings = 14;
const int enter = 15;

const int heater_left = 23;                                               /*Fans en Heaters*/
const int heater_right = 27;
const int extractor_left = 31;
const int left_heater_fan = 35;
const int right_heater_fan = 39;
const int extractor_right = 43;

static int SetPoint = 20;

void setup()
{
  wdt_enable(WDTO_1s);
  sht31a.begin(0x44);
  sht31b.begin(0x45);

  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  pinMode(settings, INPUT);
  pinMode(enter, INPUT);

  pinMode(heater_left, OUTPUT);
  pinMode(heater_right, OUTPUT);
  pinMode(extractor_left, OUTPUT);
  pinMode(left_heater_fan, OUTPUT);
  pinMode(right_heater_fan, OUTPUT);
  pinMode(extractor_right, OUTPUT);

  digitalWrite(up, HIGH);
  digitalWrite(down, HIGH);
  digitalWrite(left, HIGH);
  digitalWrite(right, HIGH);
  digitalWrite(settings, HIGH);
  digitalWrite(enter, HIGH);

  digitalWrite(heater_right, LOW);
  digitalWrite(heater_left, LOW);
  digitalWrite(extractor_left, LOW);
  digitalWrite(left_heater_fan, LOW);
  digitalWrite(right_heater_fan, LOW);
  digitalWrite(extractor_right, LOW);

  System_Init();
  LCD_SCAN_DIR Lcd_ScanDir = SCAN_DIR_DFT;
  LCD_Init( Lcd_ScanDir, 100);
  LCD_Clear(BLACK);

  GUI_DisString_EN(75, 35, "Precision Incubators", &Font24, LCD_BACKGROUND, WHITE);
  GUI_DisString_EN(50, 75, "Innovative Heating Control", &Font20, LCD_BACKGROUND, WHITE);

  GUI_DisString_EN(25, 130, "Current Temperature:", &Font20, LCD_BACKGROUND, WHITE);
  GUI_DisString_EN(440, 130, "*C", &Font20, LCD_BACKGROUND, WHITE);

  GUI_DisString_EN(25, 165, "Humidity:", &Font20, LCD_BACKGROUND, WHITE);
  GUI_DisString_EN(440, 165, "%", &Font20, LCD_BACKGROUND, WHITE);

  GUI_DisString_EN(125, 215, "Setpoint:", &Font20, LCD_BACKGROUND, WHITE);
  GUI_DisString_EN(360, 215, "*C", &Font20, LCD_BACKGROUND, WHITE);
  GUI_DisNum(310, 215, SetPoint, &Font20, LCD_BACKGROUND, WHITE); /*Displays Setpoint*/

}

void loop()
{

  delay(20);
  {
    if (digitalRead(up) == LOW)
    {
      if (SetPoint < 40)
      {
        SetPoint++;
        LCD_SetArealColor(310, 215, 360, 240, BLACK);
        GUI_DisNum(310, 215, SetPoint, &Font20, LCD_BACKGROUND, WHITE);
      }
    }

    if (digitalRead(down) == LOW)
    {
      if (SetPoint > 20)
      {
        SetPoint--;
        LCD_SetArealColor(310, 215, 360, 240, BLACK);
        GUI_DisNum(310, 215, SetPoint, &Font20, LCD_BACKGROUND, WHITE);
      }

    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    char string_t1[8];
    t1 = sht31a.readTemperature();
    dtostrf(t1, 5, 2, string_t1);

    char string_h1[8];
    h1 = sht31a.readHumidity();                                             /*Calculating Humidity*/
    dtostrf(h1, 5, 2, string_h1);                                                 /*Humidity Left*/

    char string_t2[8];
    t2 = sht31b.readTemperature();                                          /*Calculating Temperature*/
    dtostrf(t2, 5, 2, string_t2);                                                 /*Temp Right*/

    char string_h2[8];
    h2 = sht31b.readHumidity();                                             /*Calculating Humidity*/
    dtostrf(h2, 5, 2, string_h2);                                                 /*Humidity Right*/

    char string_temp[8];
    temp = (t1 + t2) / 2;
    dtostrf(temp, 5, 2, string_temp);                                             /*Average Temp*/

    char string_humidity[8];
    humidity = (h1 + h2) / 2;
    dtostrf(humidity, 5, 2, string_humidity);                                     /*Average Humidity*/

    GUI_DisString_EN(320, 130, string_temp, &Font20, LCD_BACKGROUND, WHITE);      /*Displays final readings*/
    GUI_DisString_EN(320, 165, string_humidity, &Font20, LCD_BACKGROUND, WHITE);

    if (temp > SetPoint)
    {
      digitalWrite(heater_right, LOW);
      digitalWrite(heater_left, LOW);
      digitalWrite(extractor_left, HIGH);
      digitalWrite(left_heater_fan, LOW);
      digitalWrite(right_heater_fan, LOW);
      digitalWrite(extractor_right, HIGH);
    }
    else
    {
      digitalWrite(heater_right, HIGH);
      digitalWrite(heater_left, HIGH);
      digitalWrite(extractor_left, HIGH);
      digitalWrite(left_heater_fan, HIGH);
      digitalWrite(right_heater_fan, HIGH);
      digitalWrite(extractor_right, HIGH);
    }
    delay(1000);
    LCD_SetArealColor(320, 130, 400, 190, BLACK); /*Refreshes Screen*/
  }
}

I've deleted your other cross post @leoeldan.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks for your cooperation.