Millis used for timing instead of delay

Hi there,

I've made a query about this topic, but I'm looking for more insight as I am still fining it very hard to understand. So basically I'm working on a project powered by an Arduino nano. The project requires a temperature controlled heater. But due to the nature of the project i have multiple sketches in the one program for various features and functions. I want to use millis because they are 'non-blocking' so that everything runs uninterrupted. It's just this last sketch i can't get my head around. Basically what the code does is it reads a DHT11 sensor for temperature every 2 seconds, and also display the reading on the Serial Monitor. If the temperature is below 45 degrees it will power up the heater and if it goes over 45C the heater will turn off. Here is the code.

#include <SimpleDHT.h>

//Declaring digital pin no 2 as the dht11 data pin

int pinDHT11 = A2;
int DHTpower = A3;
int Heater = 9;
SimpleDHT11 dht11;

void setup() {
  pinMode(Heater, OUTPUT);
  pinMode(DHTpower, OUTPUT);
  digitalWrite(DHTpower, LOW);
  digitalWrite(Heater, LOW);
  Serial.begin(9600);   
}

void loop() {
  delay(1000);
  RTcheck();                        //check Temperature Level
  delay(500);                     //wait 1/2sec
  }


void RTcheck() {                    //Check Temperature Level Function
  digitalWrite(DHTpower, HIGH);     //On Temperature Sensor
  delay(2000);
  Serial.println("============ Check Temperature ===============");
  delay(1000);
  Serial.println("DHT11 readings...");
   
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;

  //This bit will tell our Arduino what to do if there is some sort of an error at getting readings from our sensor
  if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("No reading , err="); Serial.println(err);delay(1000);
    return;
  }
  Serial.print("Readings: ");
  Serial.print((int)temperature); Serial.print(" C, ");
  Serial.print((int)humidity); Serial.println(" %");
  delay(500);
  if((int)temperature > 45){
    digitalWrite(DHTpower, LOW);
    delay(500);
    Serial.println("Heater OFF");
    delay(500);
    digitalWrite(Heater, LOW);
  }else{
    if ((int)temperature < 45){
      Serial.println("Temperature > 45C");
      digitalWrite(DHTpower, LOW);
      delay(500);
      Serial.println("Heater ON");
      delay(500);
      digitalWrite(Heater, HIGH);
    }else{
      Serial.println("45C < Temperature < 45C");
      digitalWrite(DHTpower, LOW);
      delay(500);
      Serial.println("Heater ON @ low heat");
      delay(500);
      analogWrite(Heater, 150);

    }
  }
 }

It was originally written with delays and i would just like some guidance as to getting the same result with millis. Would really appreciate any advice, thanks.

What do You want to happend after the last quoted "if"? It's the same code as for the first if.

  if((int)temperature > 45){
    digitalWrite(DHTpower, LOW);
    delay(500);
    Serial.println("Heater OFF");
    delay(500);
    digitalWrite(Heater, LOW);
  }else{
    if ((int)temperature < 45){

So the principle is store a starting time stamp.
Get the current time stamp.
Does the current time stamp equal or exceeds the starting time stamp?
If it does, do the things and store a new time stamp.
If it does not then do the other things

// of course its not tested.
unsigned int StartingTimeStamp = millis();
unsigned in TimeToDelay = 2000;

void loop() {
  // delay(1000);
  if ( (millis() - StartingTimeStamp) >= TimeDelay )
{
RTcheck();                        //check Temperature Level
StartingTimeStamp = millis();
}
  
// delay(500);                     //wait 1/2sec
  }

Not all that hard of a concept and the math is not hard.

Maybe this will help:

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Set your off temperature lower, like 40°. So off when temp >45, on when temp <40. Otherwise you risk lots and lots of on/off cycles.