Program Lagging

I’ve been working on a small home control station with,

Four relays controlled by a generic IR remote
DH22 sensor for measuring temperature and humidity
non-invasive current sensor for measuring the current consumption at an outlet
2.4" Nextion TFT Smart display

After I merged the DH22’s code with the IR relay control code, there seems to be a substantial amount of lag in the IR control.
I have to press the button on the remote a bunch of times to get the relay to toggle ON/OFF.
The lag steadily disappears once I comment out the DH22 function from the main loop, the presence of the display’s read/write code doesn’t seem to be a cause.

I googled enough and I Just couldn’t figure a solution for the snag.

//Nextion Display
#include <SoftwareSerial.h>
#include <Nextion.h>
SoftwareSerial nextion(2, 3);
Nextion myNextion(nextion, 9600);
String message;

//IR Sensor
#include <IRremote.h>             
int IR_Recv = 9;                       
int relayPins [] = {4, 5, 6, 7};   
int on = 0;
int i;
IRrecv irrecv(IR_Recv);
decode_results results;

//DH22 Sensor [Temp & Humidity]
#include "DHT.h"
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup () {
  Serial.begin (9600);
  myNextion.init();
  dht.begin();
  irrecv.enableIRIn ();
  pinMode (relayPins, OUTPUT);
}

void loop() {
  IRsense ();
 [color=red] DHT_22 ();[/color]
  nextion_display ();
}

void IRsense () {
  if (irrecv.decode(&results)) {
    long int deCode = results.value;
    //Serial.println (deCode);

    switch (results.value) {

      case 16748655:  // Relay 1
        on = !on;
        digitalWrite(relayPins[0], on ? HIGH : LOW);
        break;

      case 16758855:  // Relay 2
        on = !on;
        digitalWrite(relayPins[1], on ? HIGH : LOW);
        break;

      case 16775175:  // Relay 3
        on = !on;
        digitalWrite(relayPins[2], on ? HIGH : LOW);
        break;

      case 16756815:  // Relay 4
        on = !on;
        digitalWrite(relayPins[3], on ? HIGH : LOW);
        break;

      case 16757325:  // Toggle all relays ON/OFF at once
        on = !on;
        for (i = 0; i <= 5; i++)
          digitalWrite(relayPins[i], on ? HIGH : LOW);
        break;
    }
    irrecv.resume();
  }
}

void DHT_22 () {
  float h = dht.readHumidity();     // Humidity in %
  float t = dht.readTemperature();  // Temperature in Celcius
  myNextion.setComponentText("t0", String(t));
  myNextion.setComponentText("t1", String(h));
}

void nextion_display () {
  message = myNextion.listen(); 
  if (message != "") { 
  Serial.println(message); 
  }
}

Check the spec of the DHT22’s conversion time

So I found this DH11/DH22 timing diagram.

If this is the cause, any possible work around for it?

No, check the conversion time

You could use a non-blocking library for the DHT22:

https://github.com/olewolf/DHT_nonblocking

AWOL: No, check the conversion time

Okay, What conversion time exactly? Sorry, I'm a bit of a noob at these.

The datasheet says the sensor takes 800ms to go from sleep mode to high-speed mode.

aarg: You could use a non-blocking library for the DHT22:

https://github.com/olewolf/DHT_nonblocking

Thanks, I shall check that out!

I'm wondering what kind of home system can possibly require the temp and humidity to be read 1000s of times a second in loop().

Notwithstanding that the non-blocking DHT library would be an elegant solution, you could easily just use a counter and only call the blocking DHT function every x times through loop (x could be really big) or go by time using millis() and check the temperature every few seconds. Hell, even every few minutes would be adequate for a home display. (Unless you have some really secret chemical stuff going on that needs major control....)

But yeah it would be nice to fix it properly.

ps: color tags don't work inside code tags. That's the point of code tags in fact, to have no formatting other than the monospaced typestyle ;)

manor_royal: I'm wondering what kind of home system can possibly require the temp and humidity to be read 1000s of times a second in loop().

Notwithstanding that the non-blocking DHT library would be an elegant solution, you could easily just use a counter and only call the blocking DHT function every x times through loop (x could be really big) or go by time using millis() and check the temperature every few seconds. Hell, even every few minutes would be adequate for a home display. (Unless you have some really secret chemical stuff going on that needs major control....)

But yeah it would be nice to fix it properly.

ps: color tags don't work inside code tags. That's the point of code tags in fact, to have no formatting other than the monospaced typestyle ;)

Yeah, I don't really need to read the sensor that frequently, In fact, I was trying to figure how to reduce the frequency without adding a delay() because that was adding up to the loop time and more lag! The nonblocking library helped with that.

Anyway, I individually set the relay pins to output using pinMode() and that seems to have solved the problem with the code lag. Both DHT libraries work fine now. Sort of weird how that was conflicting with the DHT function in the main loop.

you can do this:

unsigned long CurrentMillis = 0;
unsigned long PrevMillis = 0;
const int Interval = 1000;

void loop()
 {
  CurrentMillis = millis(); // snapshots time, prevents errors.

  if((CurrentMillis - PrevMillis) > Interval)  // works even when millis() overflows
   {
     // do something every "Interval" milliseconds ie poll a sensor
     PrevMillis = CurrentMillis;
   }
 }