Serial Monitor Stops Printing Data After Some Time: Using Arduino to Control Solenoid Valves

Hello,

I am using an Arduino Mega to build a relative humidity setup. I am doing this by turning solenoid valves on and off using relays connected to the Arduino. I have no trouble with switching the relays on and off. I also have my program displaying the humidity and temperature using a DHT 22 humidity sensor using the serial monitor. I have written a small program to test this.

What I am having trouble with is that the data comes in for a few hours, but after that the system just stops displaying any data (I am using Serial.println() to display data in the serial monitor). Essentially, the serial monitor just freezes and the LCD display I have connected also freezes displaying the same humidity and temperature levels. I cant seem to figure out what is causing this problem. I am thinking that for some reason, the system just 'gets stuck' in that relative humidity/ temperature setting so my solenoid valves stop responding to the different if else conditions after some time.

I would greatly appreciate any help with this matter!

Thank you.

This is my code FYI:

#include <LiquidCrystal.h>
#include <DHT.h>

#define DHTPIN 5    
#define DHTTYPE DHT22  


const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
const int RELAY1_ENABLE = 3;
const int RELAY2_ENABLE = 4;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
DHT dht(DHTPIN, DHTTYPE);

float hum;  
float temp; 

void setup() {
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(RELAY1_ENABLE,OUTPUT);
  pinMode(RELAY2_ENABLE,OUTPUT);
  digitalWrite (RELAY1_ENABLE, HIGH); 
  digitalWrite (RELAY2_ENABLE, HIGH); 
  Serial.begin(9600);
  dht.begin();  

}

void loop() {
    
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp= dht.readTemperature();  


if (hum < 88 ){

if (!Serial){
  Serial.end();
  delay(100);
  Serial.begin(9600);
}

digitalWrite (RELAY2_ENABLE, LOW); // Relay 1 swtiches on when pin is set to low
digitalWrite (RELAY1_ENABLE, HIGH); // Relay 2 swtiches off when pin is set to high


lcd.begin(16, 2);
lcd.print("Bubbling");

lcd.setCursor(0,1);
lcd.print("T:");
lcd.print(temp);
lcd.print(" ");


lcd.print("RH:");
lcd.print(hum);

Serial.println(temp);
Serial.println(hum);


delay(15000);



}


else if (hum>88){

if (!Serial){
  Serial.end();
  delay(100);
  Serial.begin(9600);
}

 
digitalWrite (RELAY1_ENABLE, LOW); // Relay 2 swtiches on when pin is set to low
digitalWrite (RELAY2_ENABLE, HIGH); // Relay 1 swtiches off when pin is set to high


lcd.begin(16, 2);
lcd.print("Drying");

lcd.setCursor(0,1);
lcd.print("T:");
lcd.print(temp);
lcd.print(" ");


lcd.print("RH:");
lcd.print(hum);

Serial.println(temp);
Serial.println(hum);

delay(15000);

}

}

Okey.

Your code is likely good so I focus on the hardware.
I would like You to post real schematics, not any children's Fritzings. Power source, voltage, pin names etc....
A photo of the setup might tell something.

― ? ―

What is your expectation when hum is between (inclusive) 88.0 and 88.9999?

1 Like

First this is to get rid of these things. You begin the serial in setup() and that is enough for ever for your program.
Paul

ahh.. I don't know how I missed that... :no_mouth: I think this juts might be it! Thank you!

Got it. I was just trying to make sure I was not losing any serial connection between the Arduino and my connected computer, but I believe the problem just seems to be my code where there is no condition while the hum is between 88 and 88.99.

Thank you for your help!

Thank you for the response. But as Coding_Badly pointed out, there seems to be an undefined state when the hum value reads between 88.0 and 88.9... I will fix this and see how it works.

Using == is often not the best. <=, >=, are more safe especially when floats are involved.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.