Sketch stop to be evaluated

Dear all,

I am working on a project to control the climatic condition into a small greenhouse (at the moment temperature and light). I have a Arduino UNO board with a 4 relays shield. Basically I set a target temperature that is monitored by a DHT sensor, and I have a heater connected on a relays and a fan on a PWM pin to cool down. A light source is connected on a second relay.
The board is connected on external power source (9V AC/DC adaptor).

I can compile my sketch and everything works, but only for a limited amount of time. After some time (can be 30 min or 4 h) it looks like the whole thing just freezes. I.e the temperature is not evaluated anymore and the heater, fan and light just stay in the same state until I switch things off. When I switch it back on, everything start again smoothly.

I do not get what lead to this in my sketch. Or is this a hardware issue? I place my sketch below with the hope that someone can spot the error that I am not able to see.

Thanks in advance in case someone take the time to have a look

Best regards

/define libraries
#include<DHT.h>
#include <PID_v1.h>
#include <Wire.h>
#include "RTClib.h"

//Initialize Temp-RH sensor
#define DHTPIN A0  //what pin we're connected to
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);  //Initialize dht sensor for normal 16mhrz Arduino


//Declare variables for PID controling heat source
double SetTempH=22.9, temperature, Output1;
PID myPID1(&temperature, &Output1, &SetTempH, 2, 2000, 2, DIRECT);

//Declare variables for PID controling fan
double SetTempL=23.1, Output2;
PID myPID2(&temperature, &Output2, &SetTempL, 2, 100, 2, DIRECT);

//Declare varialbe for Real Time Clock
RTC_DS1307 RTC;

//Declare constantes
long DataInterval=2000; //interval for sending data to spreadsheet 
const int RelayOne=7;
const int RelayTwo=6;
const int RelayThree=5;
const int RelayFor=4; 
double WindowSize=120000; //time window for relay working
const int LightTime=13; //time frame (in hours) during which light is on
int fanPin=3;


//Declare variables
float rh;
long PreviousMillisSensor=0;
int RelayOneStatus=0;
int RelayTwoStatus=0;
unsigned long WindowStartTime;



void setup() {
  Serial.begin(9600);

  //initialize DHT sensor
  dht.begin();

  //initialize RTC
  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__));
  }

  //declare  pin as output
  pinMode(RelayOne,OUTPUT);
  pinMode(RelayTwo,OUTPUT);
  pinMode(RelayThree,OUTPUT);
  pinMode(RelayFor,OUTPUT);
  pinMode(fanPin,OUTPUT);

  WindowStartTime=millis();


  //tell the PID to range between 0 and the full window size
  myPID1.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID1.SetMode(AUTOMATIC); 
  myPID2.SetMode(AUTOMATIC);  
 
}

void loop() {
  
  //read data 
  rh=dht.readHumidity();
  temperature=dht.readTemperature();
  DateTime now = RTC.now();
 

  //PID computing
  myPID1.Compute();
  myPID2.Compute();
  
  // PID control of heating system
    unsigned long maintenant = millis();
  if (maintenant - WindowStartTime > WindowSize){
   //time to shift the Relay Window
    WindowStartTime += WindowSize;
  }
  if (Output1 > maintenant - WindowStartTime){
    digitalWrite(RelayOne, HIGH);
  }
  else{
    digitalWrite(RelayOne, LOW);
  }

  //PID Controle of fan
  analogWrite(fanPin,255-Output2);

  //Control of the light (switch on at 7 o'clock)
  if ((now.hour()>=7)&&(now.hour()<7+LightTime)){
      digitalWrite(RelayTwo, HIGH);
  }
  else{
    digitalWrite(RelayTwo,LOW);
  }
  

      Serial.print(temperature);
      Serial.print(",");
      Serial.print(rh);
      Serial.print(",");
      Serial.print(Output1);
      Serial.print(",");
      Serial.print(Output2);
      Serial.print(",");
      Serial.print(now.hour());
      Serial.print(":");
      Serial.print(now.minute());
      Serial.print(":");
      Serial.print(now.second());
      Serial.println(":");
    

 }

Nothing jumps out at me.

Are you sure it's not noise on the power rails from those relays? Eventually you get unlucky and one happens to cause enough of a glitch on the power rails that the board gets into a bad state? That is my suspicion....

What is the last thing printed on the computer? If that is consistently the same, then that might point to the problem.

Paul

Don't know if there's any there there but, could

if (maintenant - WindowStartTime > WindowSize)
UL - UL > double

cause any issues?

Are the relay coils close to the processor ?

Hi Thanks a lot for all your suggestions.

I do not know about the noise from the relay but I can tell that they are on top of the Uno board (it's a shield).

The last thing that the computer shows in the serial monitor is the line with the information I ask him but at the time it stopped. So it is not consistent because it kinda give the photography of the situation when it stopped. Even the time between upload and this unwanted stop is never the same.

So I will try the next steps.

  • I remove the shield and connect to two separate relays that I got yesterday
  • double check the consitency of my variables format

I cross finger and will keep you update

Martin