Updating temperature live without triggering set points

Hi All

Not sure if this is in the right section but assume so as its a programming issue.

A simple sketch to send an IR code to turn off an Air Conditioner when the set temperature gets low enough, and likewise as the room warms up again, once the set temperature is reached the IR code is sent again to turn on the Air Conditioner

A simple project to avoid my kids running the AC all night.

If you look at the code it does what its supposed to, but I have included 5 min delays after each send of the IR code to stop the Air Conditioner being triggered multiple times, but of course once the IR code has been sent, the 5 min delay starts and nothing happens till that times is up, including the update of the OLED screen

I would like the OLED screen updated with the live temperature display, even though there is a delay running, is this possible.

Cheers Alan

#include "IRremote.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define ONE_WIRE_BUS 5
#define OLED_RESET 4

Adafruit_SH1106 display(OLED_RESET);
IRsend irsend;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  Serial.begin(9600);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.display();
  display.clearDisplay();
  delay(20);
  sensors.begin();
}

int khz = 38;
unsigned signal_1[] = {4400, 4550, 600, 1600, 450, 1800, 500, 1700, 450, 650, 500, 650, 400, 650, 500, 650, 450, 650, 500, 1750, 450, 1750, 450, 1750, 500, 600, 450, 700, 450, 600, 550, 700, 350, 650, 500, 700, 400, 1750, 450, 650, 500, 650, 400, 650, 500, 600, 500, 650, 450, 650, 500, 1700, 450, 700, 450, 1750, 500, 1700, 500, 1800, 400, 1800, 450, 1750, 450, 1800, 400};

void loop() {
  
  float t = sensors.getTempCByIndex(0);
  sensors.requestTemperatures(); // Send the command to get temperatures
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(18, 5);
  display.print("Temp is");
  display.setCursor(0, 40);  
  display.setTextSize(3);
  display.print(t); display.print(" C");
  display.display();
  Serial.print("Temperature for the device is: ");
  Serial.println(t);
  delay(1000);

  if (t <= 18) {
    irsend.sendRaw(signal_1, sizeof(signal_1) / sizeof(int), khz);
    delay(300000);
  }
  else  {
    delay(10);
  }

  if (t >= 26) {
    irsend.sendRaw(signal_1, sizeof(signal_1) / sizeof(int), khz);
    delay(300000);
  }
  else  {
    delay(10);
  }

  delay(1000);
}

Take a look at the example "BlinkWithoutDelay" in the IDE. It will show you how to do just that. You basically capture the time an event happens with millis() and then each time through loop() you compare the elapsed time to your delay time and react accordingly.

Ozric13:
delay(300000);

This is the reason for @bhl64's post. During the five minute delay the Arduino does nothing else but loop around in the delay function.

This is what is meant by 'blocking' code, it blocks anything else from happening.