Display on lcd hangs (does not update) when i include a delay function to drive fan using a relay

Hi all. First of all I am a 'no-programmer'. I copy sections of code from different projects and assemble them together to make my things work. Most times the job is done.
My current project involves using an Arduino UNO R3 and a BME280 sensor to read temperature and humidity readings and control a relay each to operate when a certain set-point is reached (I am using a 4 channel relay module). I also included a third relay to toggle at predetermined intervals (one minute - ON, one hour - OFF) to operate a circulating fan. The project is for use in a small green house that i built.
PROBLEM STATEMENT: The temperature and humidity control relays work just fine until i add 'delay'. Then the readings on the lcd hang up and dont update, they get stuck at a particular reading. If i comment out the fan control relay code, everything goes back to normal.
I am including the complete sketch.
HELP REQUIRED: I need to know what is going wrong and how to correct it. please help me with lines of code and where to include them. By the way, although the lcd display hangs up, the temperature and humidity control keeps working in the background.
APPRECIATION: I want to acknowledge the time and effort so many of you put in to help guys like me. Without you, life would be miserable. So, THANKS A LOT.

SKETCH:

#include <LiquidCrystal.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

//#define SEALEVELPRESSURE_HPA (1013.25)

int upButtonPin = 6; // Define the Up Button Pin to set temp.
int downButtonPin = 13; // Define the Down Button Pin to set temp.

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  //LCD digital pins connection

Adafruit_BME280 bme; // I2C

int relayPin1 = 7; // Define the Relay Pin to operate heater
int relayPin2 = 8; // Define the Relay Pin to operate humidifier
int relayPin3 = 9; // Define the Relay Pin for timed fan operation

float temperatureThreshold = 35; // Define Temperature Threshold
float humidityThreshold = 80; // Define Humidity Threshold
//unsigned long onTime = 60000L;   // Define On Time of circulating fan in Milliseconds
//unsigned long offTime = 60000L; // Define Off Time of circulating fan in Milliseconds

void setup() {
  
  lcd.begin(16, 2);  // initialize the LCD
  
  pinMode(upButtonPin, INPUT_PULLUP); // Set Up Button Pin as Input to set temp.
  pinMode(downButtonPin, INPUT_PULLUP); // Set Down Button Pin as Input to set temp.
  pinMode(relayPin1, OUTPUT); // Set Relay Pin as Output
  digitalWrite(relayPin1, HIGH); // Set Relay Pin to High
  pinMode(relayPin2, OUTPUT); // Set Relay Pin as Output
  digitalWrite(relayPin2, HIGH); // Set Relay Pin to High
  pinMode(relayPin3, OUTPUT);
  digitalWrite(relayPin3, HIGH);
    
  Serial.begin(9600);
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
  
}

void loop() {
  
  if (digitalRead(upButtonPin) == LOW) { // If Up Button is Pressed
    temperatureThreshold++; // Increase Temperature Set Point
    delay(100); // Wait for Debouncing
  }
  
  if (digitalRead(downButtonPin) == LOW) { // If Down Button is Pressed
    temperatureThreshold--; // Decrease Temperature Set Point
    delay(100); // Wait for Debouncing
  }
  
  float temperature = bme.readTemperature();
  float humidity = bme.readHumidity();
  
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("Temp.  "); //new line added
	lcd.print(bme.readTemperature(),0); //new line added
	lcd.print((char)223);
  lcd.print("C" " (30)");  //new line added
  Serial.print("Temperature = ");
  Serial.print(temperature,0);
  Serial.println("*C");
  
  lcd.setCursor(0, 1);
  lcd.print("Humid  "); //new line added
  lcd.print(bme.readHumidity(),0); //new line added
  lcd.print(" %" " (80)");  //new line added
  Serial.print("Humidity = ");
  Serial.print(humidity);
  Serial.println(" %");
   
  if (temperature > temperatureThreshold) {
    digitalWrite(relayPin1, HIGH); // Turn on Relay
    Serial.println("Temp Relay OFF");
  } else {
    digitalWrite(relayPin1, LOW); // Turn off Relay
    Serial.println("Temp Relay ON");
  }
 
  if (humidity < humidityThreshold) {
    digitalWrite(relayPin2, HIGH); // Turn on Relay
    Serial.println("Humid Relay OFF");
  } else {
    digitalWrite(relayPin2, LOW); // Turn off Relay
    Serial.println("Humid Relay ON");
  }
  
  
  digitalWrite(relayPin3, HIGH); // Turn off Relay to operate circulating fan
  delay(60000UL); // Wait for On Time
  digitalWrite(relayPin3, LOW); // Turn on Relay to operate circulating fan
  delay(60000UL); // Wait for Off Time

delay(2000);
}

Do you know what delay() actually does under the covers ?
Have you heard about millis() timing ?

1 Like

Thanks for the response. I know that when delay() is used it adds halts the program for the specified period of time in millisecs. Havnt gone into too much depth regarding the function. When I add the UL after the delay time, it gives a longer delay, even upto hours. I know what you are driving at, If i add the delay function, it will stop the program from executing till the prescribed time is over and please correct me if I am wrong? That could be one of the reasons the updates are not happenning?

Can you suggest an alternate to 'delay()' that wont stop the program?

[quote="lastchancename, post:2, topic:1178267"]
millis() timing ?
[/quote]Look into millis() timing ?
Read about the tutorials for blink without delay()

On suggestion of 'lastchancename' I rewrote the code to implement the millis() function and the problem was resolved. Thanks a lot lastchancename. Although, I had read about the 'delay()' function stopping the readings from sensors etc., it had slipped from my mind.
This is the sample code from where i copied and modified my code:

int relayPin = 8; // Define the Relay Pin
unsigned long previousMillis = 0; // Initialize Previous Millis
unsigned long onTime = 60000; // Define On Time in Milliseconds
unsigned long offTime = 3600000; // Define Off Time in Milliseconds
int relayState = LOW; // Initialize Relay State

void setup() {
  pinMode(relayPin, OUTPUT); // Set Relay Pin as Output
}

void loop() {
  unsigned long currentMillis = millis(); // Get Current Millis
  
  if (currentMillis - previousMillis >= onTime && relayState == LOW) { // If On Time is Over and Relay is Off
    digitalWrite(relayPin, HIGH); // Turn on Relay
    previousMillis = currentMillis; // Update Previous Millis
    relayState = HIGH; // Update Relay State
  } else if (currentMillis - previousMillis >= offTime && relayState == HIGH) { // If Off Time is Over and Relay is On
    digitalWrite(relayPin, LOW); // Turn off Relay
    previousMillis = currentMillis; // Update Previous Millis
    relayState = LOW; // Update Relay State
  }
}

Thanks for all the help.
Topic can be closed.

You do that by marking the solution:

image

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