Help with the flow of this program

Hey guy! I'm new to the world of programming and arduinos. I made an automatic relay switch that turns on my space heater when the temperature is at certain level. It also displays the temp and humidity on a 16x2 LCD screen. I've managed to patch together some code and it works, but i'm wondering how I could improve a few things. Mainly the fact that when I unplug the sensor it changes the text on screen and then when I plug it back in the text is all jumbled because I can't just clear the screen to reset it or else I will lose what i've set into the setup().

Example:

#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Defines LCD variable
     
#define DHTPIN 2     // DHT22 is connected to D2 pin
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define relaytwo 7 //Relay pin IN2 is connected to D7 
#define relayone 5 //Relay pin IN1 is connected to D5

//Control Points
#define TEMPHIGH 76 // Turn off if higher than 79
#define TEMPLOW 73 // Turn on if lower than 74
     
DHT dht(DHTPIN, DHTTYPE);
     
void setup() {
  pinMode(relaytwo, OUTPUT);
  digitalWrite(relaytwo, true);
  pinMode(relayone, OUTPUT);
  digitalWrite(relayone, true);
  lcd.begin(16,2);
  dht.begin();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.setCursor(0,1);
  lcd.print("Humidity: "); 
}
     
void loop() {
  // Wait a few seconds between measurements.
  delay(2000);     
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); //true means farenheit
      
  // Check if h or t is-not-a-number, if its not then print error
  if (isnan(h) || isnan(t)) { 
    digitalWrite(relayone, HIGH); // Sets relay to off if no input detected
    lcd.setCursor(0,0);
    lcd.print("Failed to read");
    lcd.setCursor(0,1);
    lcd.print("from DHT sensor!");
    return;
  }
//Move cursor to end of word Temp: and print reading to 2 decimals
  lcd.setCursor(6,0);
  lcd.print(t,2);
//Move cursor to end of word Humidity: and print to 2 decimals
  lcd.setCursor(10,1);
  lcd.print(h,2);

  if(t > TEMPHIGH) {
      digitalWrite(relayone, HIGH);
  }
  else 
  {
    if(t < TEMPLOW)
     digitalWrite(relayone, LOW); 
  }
}

As you can see in the loop it checks if the h or t variable is not a number. If it's not a number then it displays an error. The problem is when it becomes a number again (plugging the sensor back in) it doesn't reset the screen back to the way it was. So it puts this on screen "Failed to72read from40sensor"

If I were to just clear the screen then it would skip the part of the setup function where it prints Temp: and Humidity:.

I'm just not sure what to do with the logic in order to get it to clear the error from the screen and go back to displaying "Temp:72 Humidity:40" if I were to unplug the sensor and plug it back in.

I hope this made sense.

Thank you!

Mainly the fact that when I unplug the sensor it changes the text on screen and then when I plug it back in the text is all jumbled

Really, really simple solution. STOP REMOVING THE SENSOR BEFORE YOU DESTROY YOUR ARDUINO.

PaulS:
Really, really simple solution. STOP REMOVING THE SENSOR BEFORE YOU DESTROY YOUR ARDUINO.

It's a cheap nano I'm not worried about "destroying" it because I have a handful of them laying around. (Which honestly I doubt would happen anyways) Also that didn't answer my question. Thanks though.

Take all the LCD code out of setup() and put it in a separate function. Make setup() call that function.

Make the main loop also call that function once a minute or so.

And when you get it all sorted, let us know when you burn the house down with that 'automatic' heater..

lastchancename:
And when you get it all sorted, let us know when you burn the house down with that 'automatic' heater..

Actually it's been going great, running strong for about a month now. Please explain why you think its going to burn down my house.

Sorry - a bad day!
It was an unsubtle hint to stay on top of the 'automatic' part.
I'm sure you have included interlocks to stop the heater turning on if there any risk factors.

It's a cheap nano I'm not worried about "destroying" it because I have a handful of them laying around. (Which honestly I doubt would happen anyways) Also that didn't answer my question. Thanks though.

This is a very aggressive attitude that might destroy one's whole profession; even, he is self employed.

One must be trained/motivated to look after his safety first and then safety to the equipment he is using!!!

feistyelk:
Mainly the fact that when I unplug the sensor it changes the text on screen and then when I plug it back in the text is all jumbled because I can't just clear the screen to reset it or else I will lose what i've set into the setup().

Why do you unplug the sensor while the controller is running?

That is how it sees the world! ! ! :o :o :o

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you post a picture of your project?
What does relay2 do?

Tom... :slight_smile:

Hi,
When you start your code the library sets up the DHT pin and STARTS comms with it.

If you unplug it you loose comms, when you plug it back in, comms cannot be restored because you have to restart the DHT library so it can go through the establish comms proceedure.

Tom.... :slight_smile:

TomGeorge:
Why do you unplug the sensor while the controller is running?

That is how it sees the world! ! ! :o :o :o

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you post a picture of your project?
What does relay2 do?

Tom... :slight_smile:

Relay2 is not currently set up.

I've only unplugged it to test to make sure the error works correctly and that it turns off the relay if the sensor becomes unplugged. But then it made me realize it's not erasing the screen. I just want to know how I could make the code work correctly so that it clears the screen then goes back to what it was doing. I think I may have figured it out by using a while command. Such as
"While h or t isdigit() do the if statements" and if that becomes false then it will display the error. I'm not sure. I never learned to program. That's why I was on here looking for some help of how I could improve it.

I just want a way to make it work so if the cat rips the cord out and I plug it back in I wouldn't have to reset it in order to clear the screen. Basically I just want to understand programming better. It's not like I'm going to be sitting there unplugging it and plugging it back in. I just want the code to be improved so I can improve my understanding of programming is what it really comes down to.

I'll include a picture of the circuit in the attachments.

TomGeorge:
Hi,
When you start your code the library sets up the DHT pin and STARTS comms with it.

If you unplug it you loose comms, when you plug it back in, comms cannot be restored because you have to restart the DHT library so it can go through the establish comms proceedure.

Tom.... :slight_smile:

I never noticed that the comms can't be restored? When I plug it back in it seems to work correctly. I could be wrong.. Usually I just reset it if it comes unplugged. I'll have to see. Is that what "DHT dht(DHTPIN, DHTTYPE);" is? Like I said I'm new to programming in general and I've just managed to patch together this code from different places and examples.

Thanks Tom.

Hi,
Thanks for the schematic.
You could put a 0.1uF capacitor across the Vcc and GND connections of the socket that you have the DHT plugged into.
This will provide some bypassing if any noise appears on the leads of the DHT.
Another way of checking if the plug has been pulled, is to use a 4 pin plug/socket, with the extra pin shorted to gnd in the plug.
The Nano then monitors that pin to make sure it stays low.
INPUT_PULLUP will pull the pin high if the plug is pulled out.
Thanks.. Tom.... :slight_smile:

Just a comment,
You don’t really need that R1/10K resistor if you use INPUT_PULLUP

TomGeorge:
Hi,
Thanks for the schematic.
You could put a 0.1uF capacitor across the Vcc and GND connections of the socket that you have the DHT plugged into.
This will provide some bypassing if any noise appears on the leads of the DHT.
Another way of checking if the plug has been pulled, is to use a 4 pin plug/socket, with the extra pin shorted to gnd in the plug.
The Nano then monitors that pin to make sure it stays low.
INPUT_PULLUP will pull the pin high if the plug is pulled out.
Thanks.. Tom.... :slight_smile:

Do you mean like this?(Schematic attachment) Does this just help balance the voltage in case of a random disconnect and reconnect?
Is there any advantage in the 4 pin setup, Or is it just another way of doing it?