Thanks.
/*
Temperature control program
Maintain 80 degrees for 10 minutes
Maintain 70 degrees for 15 minutes
Maintain 60 degrees in 25 minutes
*/
#include <DHT.h>
const byte dhtPin = 2; //Read DHT11 data
#define dhtType DHT11 // Insert DHT11
const byte FAN_PIN = 8;
const byte HEATER_PIN = 9;
const unsigned long PERIOD1 = (10ul * 60ul * 60ul * 1000ul); //in a few units
const unsigned long PERIOD2 = (15ul * 60ul * 60ul * 1000ul); //in millimeters
const unsigned long PERIOD3 = (25ul * 60ul * 60ul * 1000ul);
// millis() returns unsigned long, so we will use it to track
unsigned long StartTime = 0;
DHT dht (dhtPin, dhtType); //Initialize DHT sensor
int Setpoint = 0; // Desired temperature
float t, h;
void setup()
{
Serial.begin(9600);//Set the baud rate to 9600
pinMode (FAN_PIN, OUTPUT);
pinMode (HEATER_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
digitalWrite(HEATER_PIN, LOW);
dht.begin(); //Start DHT
StartTime = millis();
Setpoint = 0;
}
void up()
{
if (Setpoint == 0)
{
// Temperature control has ended
digitalWrite(FAN_PIN, LOW);
digitalWrite(HEATER_PIN, LOW);
return;
}
if (Setpoint >= t + 3) // Above maximum temperature
{
digitalWrite(FAN_PIN, HIGH); // Turn on the fan
digitalWrite(HEATER_PIN, LOW); // Turn off the heater
}
else if (Setpoint <= t - 3) // Below minimum temperature
{
digitalWrite(FAN_PIN, LOW); // Turn off the fan
digitalWrite(HEATER_PIN, HIGH); // Turn on the heater
}
else // Setpoint == t +/- 3
{
// In Range. Tuern off both
digitalWrite(FAN_PIN, LOW);
digitalWrite(HEATER_PIN, LOW);
}
}
void loop()
{
t = dht.readTemperature();//Read the temperature in Celsius
float f = dht.readTemperature(true);//Whether read the temperature in Fahrenheit
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println("Unable to read from DHT sensor!");
return;
}
unsigned long now = millis();
if (now - StartTime <= PERIOD1) //every PERIOD count
{
// First period.
Setpoint = 80;
}
else if (now - StartTime <= PERIOD1 + PERIOD2) //every PERIOD count is once
{
// Second period
Setpoint = 70;
}
else if (now - StartTime >= PERIOD1 + PERIOD2 + PERIOD3) //every PERIOD count
{
// Third period
Setpoint = 60;
}
else
Setpoint = 0; // All done.
up(); // Adjust temperature
}