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);
}