IR transmitter delay interference with DHT temp reading

Hello everyone, new programmer here, I hv a probleme with IRsend command interfering with dht temp reading,
I want to set a delay to IR transmitter to turn off/on AC unit, and since the unit have a buzzer, everytime it turns off or on it makes this annoying sound, so i decided to set the IRsend command once every 10minutes, allowing the temperature to increase or decrease.
The problem is that this delay interfere with th DHT temp reading, so the reading restart only once the 10 min delay is over.
I'm sure one of you guys know how to set delay to IRtransmitter without interfering with DHT readings.
PS: I dont want to have to remove the buzzer from the unit.
I dont want the the IR to keep sending commands, it may damage the AC unit programmer, plus it will keep draigning the batterie.
Here is the code:

#include <IRLibSendBase.h>    //We need the base code
#include <IRLib_HashRaw.h>    //Only use raw sender
#include <dht.h>

#define dht_apin A0 // Analog Pin sensor is connected to
 
dht DHT;

IRsendRaw mySender;

void setup() {
  Serial.begin(115200);
  delay(2000); 
  while (!Serial); //delay for Leonardo
  Serial.println(F("DHT11 Humidity & temperature Sensor\n\n"));
}
/* Cut and paste the output from "rawRecv.ino" below here. It will 
 * consist of a #define RAW_DATA_LEN statement and an array definition
 * beginning with "uint16_t rawData[RAW_DATA_LEN]= {…" and concludes
 * with "…,1000};"
 */
#define RAW_DATA_LEN 230
uint16_t rawDataOff[RAW_DATA_LEN]={
  3086, 290, 30, 1354, 546, 1006, 546, 1010, 
 546, 318, 546, 274, 590, 318, 546, 1010, 
  546, 318, 546, 322, 546, 1010, 546, 1006, 
  550, 262, 598, 1010, 546, 314, 550, 314, 
  550, 1006, 546, 1014, 546, 318, 546, 1006, 
  550, 1006, 546, 226, 638, 318, 546, 1010, 
  546, 318, 546, 322, 546, 1010, 546, 318, 
  546, 318, 550, 318, 546, 318, 546, 318, 
  546, 318, 546, 326, 546, 318, 546, 318, 
  550, 318, 546, 318, 546, 318, 546, 318, 
  546, 318, 550, 326, 546, 318, 546, 318, 
  546, 318, 546, 318, 546, 318, 546, 1010, 
  546, 318, 546, 322, 550, 1006, 546, 1006, 
  550, 318, 546, 318, 522, 342, 522, 342, 
  522, 342, 526, 346, 546, 1006, 522, 1034, 
  522, 342, 518, 1034, 522, 346, 518, 346, 
  518, 346, 518, 354, 518, 346, 518, 1034, 
  522, 342, 522, 346, 518, 346, 518, 346, 
  518, 346, 522, 350, 522, 346, 518, 346, 
  518, 346, 518, 346, 518, 350, 514, 370, 
  498, 366, 498, 374, 498, 370, 494, 374, 
  490, 374, 490, 374, 490, 378, 486, 378, 
  486, 378, 486, 386, 486, 378, 486, 378, 
  486, 382, 486, 378, 486, 378, 486, 402, 
  462, 382, 486, 406, 466, 402, 462, 402, 
  462, 402, 486, 378, 486, 382, 486, 378, 
  478, 386, 486, 382, 490, 1066, 486, 378, 
  486, 1070, 486, 378, 486, 378, 486, 378, 
  486, 1066, 490, 378, 490, 1000};
  
#define RAW_DATA_LEN 228
uint16_t rawDataOn[RAW_DATA_LEN]={
 3086, 1702, 522, 1034, 518, 1034, 518, 362, 
  498, 366, 498, 362, 498, 1038, 518, 366, 
  498, 350, 518, 1034, 518, 1034, 518, 342, 
  514, 1038, 518, 350, 502, 358, 494, 1062, 
  494, 1066, 518, 342, 518, 1034, 518, 1038, 
  494, 370, 494, 366, 490, 1066, 490, 374, 
  494, 378, 490, 1058, 494, 370, 490, 374, 
  490, 370, 494, 370, 494, 370, 490, 374, 
  490, 378, 490, 374, 490, 374, 490, 370, 
  490, 374, 494, 370, 490, 374, 490, 370, 
  490, 382, 490, 378, 490, 370, 490, 1086, 
  470, 374, 490, 374, 490, 1086, 466, 378, 
  486, 406, 466, 1082, 466, 1086, 466, 398, 
  462, 402, 462, 402, 462, 402, 462, 398, 
  462, 410, 462, 1090, 462, 1094, 462, 402, 
  458, 1094, 462, 402, 462, 406, 458, 398, 
  462, 410, 470, 394, 462, 1094, 478, 382, 
  486, 378, 486, 378, 482, 382, 474, 386, 
  486, 386, 486, 378, 486, 382, 486, 378, 
  486, 374, 486, 382, 482, 378, 486, 378, 
  486, 382, 486, 378, 486, 378, 486, 374, 
  486, 382, 482, 378, 486, 378, 462, 402, 
  470, 402, 458, 406, 462, 402, 462, 402, 
  462, 402, 458, 406, 462, 398, 462, 406, 
  458, 410, 458, 402, 462, 402, 462, 422, 
  438, 430, 438, 422, 438, 426, 438, 426, 
  438, 430, 438, 1118, 438, 426, 438, 422, 
  438, 1118, 438, 426, 438, 426, 434, 1122, 
  434, 434, 434, 1000};



/*
 * Cut-and-paste into the area above.
 */
   
void loop() {

  DHT.read11(dht_apin);
  int temp = DHT.temperature;
  {Serial.print(F("temperature = "));
  Serial.print(temp); 
  Serial.println(F(" C"));
      delay(2000);
         }


 
 
   if (temp > 27) {
    mySender.send(rawDataOn,RAW_DATA_LEN,36);//Pass the buffer,length, optionally frequency
    Serial.println(F("AC Switched On"));
    if  (rawDataOn,RAW_DATA_LEN,36) {
   delay (10000);
    }
    }

  
  
  else if (temp < 24) {
    mySender.send(rawDataOff,RAW_DATA_LEN,36);//Pass the buffer,length, optionally frequency
    Serial.println(F("AC Switched Off"));
    if  (rawDataOn,RAW_DATA_LEN,36) {
   delay (10000);
    }
    }
}

Read the Blink Without Delay example in the IDE (File->examples->02.digital->BlinkWithoutDelay) to learn how to track elapsed time without blocking your other code from running.

The delay() function basically stops your program for that amount of time so nothing else can happen.

Thanks, I learned alot from the example, but it doesn't seem to solve my problem.
Again, it delayed the IR transmition,but stopped the DHT readings

You only want to send your IR codes if you are switching states (on -> off or off->on) so you need to keep track of that

#include <IRLibSendBase.h>    //We need the base code
#include <IRLib_HashRaw.h>    //Only use raw sender
#include <dht.h>

#define dht_apin A0 // Analog Pin sensor is connected to

dht DHT;

IRsendRaw mySender;

void setup() {
  Serial.begin(115200);
  delay(2000);
  while (!Serial); //delay for Leonardo
  Serial.println(F("DHT11 Humidity & temperature Sensor\n\n"));
}
/* Cut and paste the output from "rawRecv.ino" below here. It will
   consist of a #define RAW_DATA_LEN statement and an array definition
   beginning with "uint16_t rawData[RAW_DATA_LEN]= {…" and concludes
   with "…,1000};"
*/
#define RAW_DATA_LEN 230
uint16_t rawDataOff[RAW_DATA_LEN] = {
  3086, 290, 30, 1354, 546, 1006, 546, 1010,
  546, 318, 546, 274, 590, 318, 546, 1010,
  546, 318, 546, 322, 546, 1010, 546, 1006,
  550, 262, 598, 1010, 546, 314, 550, 314,
  550, 1006, 546, 1014, 546, 318, 546, 1006,
  550, 1006, 546, 226, 638, 318, 546, 1010,
  546, 318, 546, 322, 546, 1010, 546, 318,
  546, 318, 550, 318, 546, 318, 546, 318,
  546, 318, 546, 326, 546, 318, 546, 318,
  550, 318, 546, 318, 546, 318, 546, 318,
  546, 318, 550, 326, 546, 318, 546, 318,
  546, 318, 546, 318, 546, 318, 546, 1010,
  546, 318, 546, 322, 550, 1006, 546, 1006,
  550, 318, 546, 318, 522, 342, 522, 342,
  522, 342, 526, 346, 546, 1006, 522, 1034,
  522, 342, 518, 1034, 522, 346, 518, 346,
  518, 346, 518, 354, 518, 346, 518, 1034,
  522, 342, 522, 346, 518, 346, 518, 346,
  518, 346, 522, 350, 522, 346, 518, 346,
  518, 346, 518, 346, 518, 350, 514, 370,
  498, 366, 498, 374, 498, 370, 494, 374,
  490, 374, 490, 374, 490, 378, 486, 378,
  486, 378, 486, 386, 486, 378, 486, 378,
  486, 382, 486, 378, 486, 378, 486, 402,
  462, 382, 486, 406, 466, 402, 462, 402,
  462, 402, 486, 378, 486, 382, 486, 378,
  478, 386, 486, 382, 490, 1066, 486, 378,
  486, 1070, 486, 378, 486, 378, 486, 378,
  486, 1066, 490, 378, 490, 1000
};

#define RAW_DATA_LEN 228
uint16_t rawDataOn[RAW_DATA_LEN] = {
  3086, 1702, 522, 1034, 518, 1034, 518, 362,
  498, 366, 498, 362, 498, 1038, 518, 366,
  498, 350, 518, 1034, 518, 1034, 518, 342,
  514, 1038, 518, 350, 502, 358, 494, 1062,
  494, 1066, 518, 342, 518, 1034, 518, 1038,
  494, 370, 494, 366, 490, 1066, 490, 374,
  494, 378, 490, 1058, 494, 370, 490, 374,
  490, 370, 494, 370, 494, 370, 490, 374,
  490, 378, 490, 374, 490, 374, 490, 370,
  490, 374, 494, 370, 490, 374, 490, 370,
  490, 382, 490, 378, 490, 370, 490, 1086,
  470, 374, 490, 374, 490, 1086, 466, 378,
  486, 406, 466, 1082, 466, 1086, 466, 398,
  462, 402, 462, 402, 462, 402, 462, 398,
  462, 410, 462, 1090, 462, 1094, 462, 402,
  458, 1094, 462, 402, 462, 406, 458, 398,
  462, 410, 470, 394, 462, 1094, 478, 382,
  486, 378, 486, 378, 482, 382, 474, 386,
  486, 386, 486, 378, 486, 382, 486, 378,
  486, 374, 486, 382, 482, 378, 486, 378,
  486, 382, 486, 378, 486, 378, 486, 374,
  486, 382, 482, 378, 486, 378, 462, 402,
  470, 402, 458, 406, 462, 402, 462, 402,
  462, 402, 458, 406, 462, 398, 462, 406,
  458, 410, 458, 402, 462, 402, 462, 422,
  438, 430, 438, 422, 438, 426, 438, 426,
  438, 430, 438, 1118, 438, 426, 438, 422,
  438, 1118, 438, 426, 438, 426, 434, 1122,
  434, 434, 434, 1000
};



bool isOn = false;
unsigned long lastTime;
const unsigned long period = 2000;

void loop() {

  DHT.read11(dht_apin);
  int temp = DHT.temperature;

  if ( millis() - lastTime >= period ) {
    lastTime += period;
    Serial.print(F("temperature = "));
    Serial.print(temp);
    Serial.println(F(" C"));
  }

  if (temp > 27 && isOn == false ) {
    mySender.send(rawDataOn, RAW_DATA_LEN, 36); //Pass the buffer,length, optionally frequency
    Serial.println(F("AC Switched On"));
    isOn = true;
  }
  if (temp < 24 && isOn == true ) {
    mySender.send(rawDataOff, RAW_DATA_LEN, 36); //Pass the buffer,length, optionally frequency
    Serial.println(F("AC Switched Off"));
    isOn = true;
  }
}

You don't really need to track elapsed time at all, other than to slow down how often you report the temperature to the serial monitor

Doesn't the compiler complain when you re-define the value of RAW_DATA_LEN? And won't it use the second value in both places in the code so the last two values in rawDataOff are not sent?

I didn't compile it since I don't have that library. I was also focused on the timing/delay aspects of the problem. It does look like that is another problem, but easily fixed.

#include <IRLibSendBase.h>    //We need the base code
#include <IRLib_HashRaw.h>    //Only use raw sender
#include <dht.h>

#define dht_apin A0 // Analog Pin sensor is connected to

dht DHT;

IRsendRaw mySender;

void setup() {
  Serial.begin(115200);
  delay(2000);
  while (!Serial); //delay for Leonardo
  Serial.println(F("DHT11 Humidity & temperature Sensor\n\n"));
}
/* Cut and paste the output from "rawRecv.ino" below here. It will
   consist of a #define RAW_DATA_LEN statement and an array definition
   beginning with "uint16_t rawData[RAW_DATA_LEN]= {…" and concludes
   with "…,1000};"
*/
//#define RAW_DATA_LEN 230

uint16_t rawDataOff[] = {
  3086, 290, 30, 1354, 546, 1006, 546, 1010,
  546, 318, 546, 274, 590, 318, 546, 1010,
  546, 318, 546, 322, 546, 1010, 546, 1006,
  550, 262, 598, 1010, 546, 314, 550, 314,
  550, 1006, 546, 1014, 546, 318, 546, 1006,
  550, 1006, 546, 226, 638, 318, 546, 1010,
  546, 318, 546, 322, 546, 1010, 546, 318,
  546, 318, 550, 318, 546, 318, 546, 318,
  546, 318, 546, 326, 546, 318, 546, 318,
  550, 318, 546, 318, 546, 318, 546, 318,
  546, 318, 550, 326, 546, 318, 546, 318,
  546, 318, 546, 318, 546, 318, 546, 1010,
  546, 318, 546, 322, 550, 1006, 546, 1006,
  550, 318, 546, 318, 522, 342, 522, 342,
  522, 342, 526, 346, 546, 1006, 522, 1034,
  522, 342, 518, 1034, 522, 346, 518, 346,
  518, 346, 518, 354, 518, 346, 518, 1034,
  522, 342, 522, 346, 518, 346, 518, 346,
  518, 346, 522, 350, 522, 346, 518, 346,
  518, 346, 518, 346, 518, 350, 514, 370,
  498, 366, 498, 374, 498, 370, 494, 374,
  490, 374, 490, 374, 490, 378, 486, 378,
  486, 378, 486, 386, 486, 378, 486, 378,
  486, 382, 486, 378, 486, 378, 486, 402,
  462, 382, 486, 406, 466, 402, 462, 402,
  462, 402, 486, 378, 486, 382, 486, 378,
  478, 386, 486, 382, 490, 1066, 486, 378,
  486, 1070, 486, 378, 486, 378, 486, 378,
  486, 1066, 490, 378, 490, 1000
};
const uint16_t DATA_LEN_OFF = sizeof(rawDataOff) / sizeof(rawDataOff[0]);

//#define RAW_DATA_LEN 228
uint16_t rawDataOn[] = {
  3086, 1702, 522, 1034, 518, 1034, 518, 362,
  498, 366, 498, 362, 498, 1038, 518, 366,
  498, 350, 518, 1034, 518, 1034, 518, 342,
  514, 1038, 518, 350, 502, 358, 494, 1062,
  494, 1066, 518, 342, 518, 1034, 518, 1038,
  494, 370, 494, 366, 490, 1066, 490, 374,
  494, 378, 490, 1058, 494, 370, 490, 374,
  490, 370, 494, 370, 494, 370, 490, 374,
  490, 378, 490, 374, 490, 374, 490, 370,
  490, 374, 494, 370, 490, 374, 490, 370,
  490, 382, 490, 378, 490, 370, 490, 1086,
  470, 374, 490, 374, 490, 1086, 466, 378,
  486, 406, 466, 1082, 466, 1086, 466, 398,
  462, 402, 462, 402, 462, 402, 462, 398,
  462, 410, 462, 1090, 462, 1094, 462, 402,
  458, 1094, 462, 402, 462, 406, 458, 398,
  462, 410, 470, 394, 462, 1094, 478, 382,
  486, 378, 486, 378, 482, 382, 474, 386,
  486, 386, 486, 378, 486, 382, 486, 378,
  486, 374, 486, 382, 482, 378, 486, 378,
  486, 382, 486, 378, 486, 378, 486, 374,
  486, 382, 482, 378, 486, 378, 462, 402,
  470, 402, 458, 406, 462, 402, 462, 402,
  462, 402, 458, 406, 462, 398, 462, 406,
  458, 410, 458, 402, 462, 402, 462, 422,
  438, 430, 438, 422, 438, 426, 438, 426,
  438, 430, 438, 1118, 438, 426, 438, 422,
  438, 1118, 438, 426, 438, 426, 434, 1122,
  434, 434, 434, 1000
};
const uint16_t DATA_LEN_ON = sizeof(rawDataOn) / sizeof(rawDataOn[0]);

bool isOn = false;
unsigned long lastTime;
const unsigned long period = 2000;

void loop() {

  DHT.read11(dht_apin);
  int temp = DHT.temperature;

  if ( millis() - lastTime >= period ) {
    lastTime += period;
    Serial.print(F("temperature = "));
    Serial.print(temp);
    Serial.println(F(" C"));
  }

  if (temp > 27 && isOn == false ) {
    mySender.send(rawDataOn, DATA_LEN_ON, 36); //Pass the buffer,length, optionally frequency
    Serial.println(F("AC Switched On"));
    isOn = true;
  }
  if (temp < 24 && isOn == true ) {
    mySender.send(rawDataOff, DATA_LEN_OFF, 36); //Pass the buffer,length, optionally frequency
    Serial.println(F("AC Switched Off"));
    isOn = true;
  }
}

int readTemperature() {
  // read the temperature periodically
  return temp;
}

Maaaan!!! I love you, you're a genius,,, I've been working on this project for almost a week, and i finally, you got it to work. I just started to learn about millis(), and here you gave me a lot to learn. thanks again man.

PS: we have to include: "int temp = DHT.temperature;" before "int readTemperature()", or manipulate the brackets some how.

No, Not at all,,, The memory though is about 60%, and im only using two IR commands, and DHT for reading temp

this made the IRLed go crazy, lol,,,,maaan, are you trying to burn my AC :rofl: :rofl: :rofl:..... it Hell Season here in Africa...Thanks again. Hats off

IR_SEND_FINAL_delay.ino (4.2 KB)

if (t < 29 && isOn == true ) {
mySender.send(rawDataOff, DATA_LEN_OFF, 36); //Pass the buffer,length, optionally frequency
Serial.println(F("AC Switched Off"));
isOn = false;
}