Millis() instead of Delay

Hello, i am trying to build an automated greenhouse but have become stumped by responsiveness.

Currently my code cycles through Temperature, Humidity, Moisture, and Light values on an LCD display and the values of the sensors DHT11, LDR and a soil moisture Probe.

This works as intended however the delay(2000); after each LCD print out slows down the program and only when the program cycles around do the sensors trigger the corresponding outputs.

My question is how can i substitute Millis(); for delay as i believe this is what i need to do?

p.s i have been trying to sort this for the last 3 days.

My code is attached any help would be greatly appreciated.

#include <dht.h> // include the dht.h sensor libaries
#include <LiquidCrystal.h> //inculde LCD library

dht DHT;

#define DHT11_PIN 7 // define sensor input pin to pin 7

int Tempthreshold = 24; // set threshold i.e if above or below
int Humidthreshold = 50; // set threshold i.e if above or below
int Soilthreshold = 50; // set moisture content threshold
int lightthreshold = 400; // set threshold for LDR i.e if above or below

int LDRsensorPin = A0; // set pin for LDR
int SoilsensorPin = A1; // assign A1 for soil sensor

int ledPin = 8; // select the pin for the LED
int ledPin1 = 9; //
int fanPin = 13; // connect this pin for the LED

int val;
int Mapped;

int LDRvalue;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin, OUTPUT); // set LED pin and an output
pinMode(fanPin, OUTPUT); // set fanPin 13 as an output
Serial.begin(9600); // begin serial read out

lcd.begin(16, 2); // set up the LCD’s number of columns and rows:

lcd.clear(); // clear LCD
lcd.setCursor (0, 0); // set position for next line
lcd.print(“WELCOME TO”); // Print to the LCD.
lcd.setCursor (0, 1); // set position for next line
lcd.print(“GREENDUINO”); // Print to the LCD.
delay(5000); // wait
lcd.clear(); // clear lcd

// ^^^ Welcoming message ^^^

}
void loop()
{
int LDRvalue = analogRead(LDRsensorPin);

int val = analogRead(A1);
Mapped = map (val, 200 , 700, 100, 0); // map soilmoisture between 0 - 100 instead of 0 - 1023

int chk = DHT.read11(DHT11_PIN); // check sensor

if (DHT.temperature > Tempthreshold) { // if the analog value is high enough, turn on the LED:
digitalWrite(fanPin, HIGH); // turn LED on

} else {
digitalWrite(fanPin, LOW); // turn LED off
}

if (DHT.humidity > Humidthreshold) { // if the analog value is high enough, turn on the LED:
digitalWrite(fanPin, HIGH); // turn LED on
} else {
digitalWrite(fanPin, LOW); // turn LED off
}

if (LDRvalue < lightthreshold) {
digitalWrite(ledPin, LOW); // turn LED off
} else {
digitalWrite(ledPin, HIGH); // turn LED on
}

if (Mapped > Soilthreshold) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}

Serial.print("Temperature = "); // print “temprature” in serial monitor
Serial.println(DHT.temperature, 0); // print value of sensor
lcd.clear(); // clears lcd
lcd.setCursor (0, 0); // set position on lcd
lcd.print("Temp = "); // print “temprature” on lcd
lcd.print(DHT.temperature, 0); // print temprature value to lcd
lcd.print((char)223); //print symbol to lcd
lcd.print(“C”); // print “C” to lcd
delay(2000);

Serial.print("Humidity = "); // print “Humidity” in serial monitor
Serial.println(DHT.humidity, 0); // print value of sensor
lcd.clear();
lcd.setCursor (0, 0); // set position on lcd
lcd.print(“Humidity = “); // print “Humidity” on lcd
lcd.print(DHT.humidity, 0); // print humidity value lcd
lcd.print (”%”); // print “%” to lcd
delay(2000);

Serial.print(“SoilMoistureContent =”);
Serial.println(Mapped);
lcd.clear(); // clear lcd
lcd.setCursor (0, 0); // set position on lcd
lcd.print(“Moisture = “); // print Moisture
lcd.print(Mapped); // display soil moisture content value
lcd.print(”%”);
delay(2000);

Serial.print("LDR = ");
Serial.println(LDRvalue);
lcd.clear(); // clear lcd
lcd.setCursor (0, 0); // set position on lcd
lcd.print(“LDR = “); // print Moisture
lcd.print(LDRvalue); // display soil moisture content value
lcd.print(”%”);
delay(2000);

}

GREENHOUSE.ino (3.67 KB)

Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

See if you can follow this:

Put the code to update the LCD into a separate function and only call the function once every 2 seconds.

Have a look at how millis() is used to manage timing without blocking in Several things at a time.

...R