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.