Hello, i've been trying to complete this project for a little while, i'm new to arduino. I have an actuator which is controlled via temp sensor DHT11. Currently it the actuator extends when temp is above 23C and retracts when temp is below 21C, i'm aware those two temps are too close, its just for testing purposes. The issue is i need the actuator to stop after 5 seconds and stay idle at that point unitl temp is above 23 C and extends again. The actuator is controlled by two relays. I have looked into Millis and other methods, please can someone help. Code is below.
#include "DHT.h" //install this library from Library Manager and
//install the additional library it requests while doing so
#define DHTPIN A0 //the digital pin # on which sensor OUT is connected to
#define DHTTYPE DHT11 //sensor type (there is a DHT22 available as well)
DHT dht(DHTPIN, DHTTYPE); //DHT library function
//pin assignment
int relay_1 = 7; //relay 1 pin to activate coil
int relay_2 = 8; //relay 2 pin to activate coil
int relay_3 = 9; //relay 3 pin to activate coil
int relay_4 = 10; //relay 4 pin to activate coil
//Actuator door open and close temperature values in celsius - can be changed to suit application needs
int open_door_temp = 23;
int close_door_temp = 21;
int turn_mat_on_temp = 25; //power off heated matts
int turn_mat_off_temp = 35;
void setup() {
Serial.begin(115200);
Serial.println(F("DHTxx test!"));
dht.begin();
pinMode(relay_1, OUTPUT); //defining relay 1 pin as output
pinMode(relay_2, OUTPUT); //defining relay 2 pin as output
pinMode(relay_3, OUTPUT); //defining relay 3 pin as output
pinMode(relay_4, OUTPUT); //defining relay 4 pin as output
digitalWrite(relay_1, LOW); //deactivating relay 1
digitalWrite(relay_2, LOW); //deactivating relay 2
digitalWrite(relay_3, LOW); //deactivating relay 3
digitalWrite(relay_4, LOW); //deactivating relay 4
}
void loop() {
float h = dht.readHumidity(); //reads humidity
float t = dht.readTemperature(); //reads temperature
float f = dht.readTemperature(true); //reads temperature
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(f, h); //converts the readings to celcius
float hic = dht.computeHeatIndex(t, h); //converts the readings to fahrenheit
//float hic = dht.readTemperature();
// //Serial.print is useful if you are using serial monitor on PC and is not required for this code to work
// Serial.print(F(" Humidity: "));
// Serial.print(h);
// Serial.print(F("% Temperature: "));
// Serial.print(t);
//Serial.print(F("°C "));
// Serial.print(f);
// Serial.print(F("°F Heat index: "));
// Serial.print(hic);
// Serial.print(F("°C "));
// Serial.print(hif);
// Serial.println(F("°F"));
if (hic >= open_door_temp) { //if the temperature value is higher than 27C, extend actuator
digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, LOW);
Serial.println(F("open"));
Serial.println(hic);
}
else if (hic <= close_door_temp) { //if the temperature value is lower than 24C, retract actuator
digitalWrite(relay_1, LOW);
// delay(5000);
digitalWrite(relay_2, HIGH);
// delay(5000);
// digitalWrite(relay_1, HIGH);
// digitalWrite(relay_2, HIGH);
// if(relay_2State == HIGH)
//{
// if(millis()=relayCameOn > 5000)
// {
// digitalWrite(relay_1, LOW);
// digitalWrite(relay_2, LOW) ;
//relay_2State = LOW;
}
Serial.println(F("close"));
Serial.println(hic);
if (hic >= turn_mat_off_temp) { //if the temperature value is higher than 35C, turn off relay
digitalWrite(relay_3, LOW);
digitalWrite(relay_4, LOW);
Serial.println(F("off"));
Serial.println(hic);
}
else if (hic <= turn_mat_on_temp) { //if the temperature value is lower than 25C, turn on relay
digitalWrite(relay_3, HIGH);
digitalWrite(relay_4, HIGH);
Serial.println(F("on"));
Serial.println(hic);
}
delay(500); //can be changed based on needs
}