[Edit - moderator - please use the 'code' button when submitting code]
Code: Select all
// Lesson 4.5 Adding output, Relays etc. with hysterisis
#include "DHT.h"
#include <LiquidCrystal.h>
#include <Wire.h>
#define DHTPIN 2 // what pin we're connected to
#define tempcontpin8 //temp output to control
#define humcontpin4 //humidity output to control
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
int maxF=0, minF=212;
int maxH=0, minH=100;
LiquidCrystal lcd(8,9,4,5,6,7,
dht(DHTPIN2, DHTTYPE(22,
//here's where the temp and hum setpoints are changed
int (tempset=85, tempreset=83;
int humset=55, humreset=53;
int tmem=0, hmem=0;
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
lcd.begin(20, 4); // Set the display to 20 columns and 4 rows
lcd.clear();
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
float tF= (t* 1.8) +32; //convert to Farenheit
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
Serial.print("Temperature: ");
Serial.print(tF);
Serial.println(" *F");
lcd.setCursor(0,0);
lcd.print("Temp ");
lcd.print(tF);
lcd.write(B11011111); // print degree symbol
lcd.print("F ");
lcd.setCursor(20,0);
lcd.print("Hum ");
lcd.print(h);
if (tF>maxF) {
maxF=tF;
} //check for max temp
if (tF<minF) {
minF=tF;
} //check for min temp
lcd.setCursor(0,1);
lcd.print("H=");
lcd.print(maxF);
lcd.write(B11011111);
lcd.print("F L=");
lcd.print(minF);
lcd.write(B11011111);
lcd.print("F ");
if (h>maxH) {
maxH=h;
} //check for max hum
if (h<minH) {
minH=h;
} //check for min hum
lcd.setCursor(20,1);
lcd.print("max=");
lcd.print(maxH);
lcd.print(" min=");
lcd.print(minH);
//Here's the section for control with hysterisis
if (tempreset<=tF && tF<=tempset && tmem==1) {
digitalWrite (8, HIGH);
}
if (tF>=tempset) {
tmem==1,
digitalWrite (8, HIGH);
}
if (tF<tempreset) {
tmem==0,
digitalWrite (8, LOW);
}
if (humreset<=h && h<=humset && hmem==1) {
digitalWrite (4, HIGH);
}
if (h>humset) {
hmem==1,
digitalWrite (4, HIGH);
}
if (h<humreset) {
hmem==0,
digitalWrite (4, LOW);
}
}
}