//HUMIDITY CONTROLLER 1.1
//written for NANO
//controls and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.
//added: "system off" function - allows both outlets to be turned off
// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT//controls DHT_sens(10and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, DHT22);connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.
#define//added: DHT22_PIN"system A2off" function - allows both outlets to be turned off
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration variable
int setpoint = 50; //feedback setpoint.
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of time the display is to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){
//check calibration buttons
calup = digitalRead(calupbtn);
caldown = digitalRead(caldownbtn);
if(calup == HIGH and caldown == HIGH){ //--------SYSTEM TOGGLE
syslock ++;
Serial.println(syslock);
if(syslock == 20){ //if both buttons held down for this many loops
systog++;
if(systog % 2 == 1){ //--------SYSTEM OFF
syson = false;
digitalWrite(fan, HIGH);
digitalWrite(humidifier, HIGH);
digitalWrite(lcdlight, HIGH);
Serial.println("SYSTEM TURNED OFF");
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(2000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
if(systog % 2 == 0){ //--------SYSTEM ON
syson = true;
Serial.println("SYSTEM TURNED ON");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM ON");
delay(2000);
lcd.clear();
displtimeon = millis();
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(millis() > lastcheck + interval and syson == true){ //read the DHT humidity
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.print("DHT read = ");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.println(" ");
}
//turn on the led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht DHT;
#define DHT11_PIN A2
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration variable
int setpoint = 50; //feedback setpoint.
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of time the display is to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){
//check calibration buttons
calup = digitalRead(calupbtn);
caldown = digitalRead(caldownbtn);
if(calup == HIGH and caldown == HIGH){ //--------SYSTEM TOGGLE
syslock ++;
Serial.println(syslock);
if(syslock == 20){ //if both buttons held down for this many loops
systog++;
if(systog % 2 == 1){ //--------SYSTEM OFF
syson = false;
digitalWrite(fan, HIGH);
digitalWrite(humidifier, HIGH);
digitalWrite(lcdlight, HIGH);
calstate = true;
displtimeon = millis(); //set display timer
Serial.println("cal btn ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" "SYSTEM SYSTEMTURNED OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
if(calstate == true and syson == true){ //--------DISPLAY ROUTINE
Serial.println("printing display");
//display variables on LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//keeps calibration locked until display cycles 5 times after initially turned on
//prevents adjustments on initial button press.
}
//--------CALIBRATION ADJUSTMENTS
if(calup == HIGH and caldown == LOW and calstate == true and syson == true){
if(setpoint < 90 and calunlock > 5){
setpoint = setpoint + 5; //increase setpoint
Serial.println("adj setpoint up");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
if(setpoint > 10 and calunlock > 5){
setpoint = setpoint - 5; //decrease setpoint
Serial.println("adj setpoint dn");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(millis() > displtimeon + displtime){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight, LOW); //turn off the screen
calstate = false;
Serial.println("displ + backlights off");
lcd.clear();
calunlock =lcd.print(" 0; //lockSYSTEM calibrationOFF");
} lcd.setCursor(0,1);
lcd.print(" hold both btns");
if(millis delay(2000);
< lastcheck lcd.setCursor(0,1){;
lastchecklcd.print(" = millis(to turn on "); //reset timers in a millisecond rollover displtimeon = millis();
}
if(systog % 2 == 0){ //--------SETPOINT ERRORSYSTEM PROCEDUREON
if(setpoint > 91 or setpoint < 9){ //in case setpoint is ever outsyson of= boundstrue;
Serial.println("O/B"SYSTEM ERROR"TURNED ON");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursor(0,1);
" SYSTEM lcd.print("RESETTING"ON");
delay(1000);
for(int count = 9; count >= 0; count = count - 1){
lcd.setCursor(15,12000);
lcd.print(count); //count down from 10
delayclear(1000);
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
//turn on humidifier relay if below setpoint --------RELAY CONTROL
//RELAY MODULE IS ACTIVE LOW
if(DHT.humidity <= setpoint - 3 and syson == true){ //if humidity is 3% lower than setpoint
Serial.println("humidifier ON, fan OFF");
digitalWrite(humidifier, LOW); //turn on humidifier
digitalWrite(fan, HIGH);
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(DHT.humiditymillis() >=> setpointlastcheck + 3interval and syson == true){ //ifread the DHT humidity is 3% above setpointint chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.printlnprint("humidifier"DHT OFF,read fan= ON"");
Serial.print(DHT.humidity);
digitalWriteSerial.print(humidifier,"%");
HIGH Serial.println(" ");
}
//turn on fanthe led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
digitalWrite(fanlcdlight, LOWHIGH);
calstate = }true;
displtimeon = elsemillis(); //set display timer
Serial.println("cal btn ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
if(calstate == true and syson == true){ //--------DISPLAY ROUTINE
Serial.println("all"printing off"display"); //ifdisplay variables on LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
is within 3% oflcd.setCursor(15,0);
setpoint lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
digitalWritelcd.setCursor(humidifier13,1);
HIGH lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//turnkeeps bothcalibration offlocked until display cycles 5 times after initially turned on
//prevents adjustments on initial digitalWritebutton press.
}
//--------CALIBRATION ADJUSTMENTS
if(fan,calup == HIGH and caldown == LOW and calstate == true and syson == true){
if(setpoint < 90 and calunlock > 5){
setpoint = setpoint + 5; //increase setpoint
Serial.println("adj setpoint up");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
if(setpoint > 10 and calunlock > 5){
setpoint = setpoint - 5; //decrease setpoint
Serial.println("adj setpoint dn");
}
Serial.println(setpoint);
//delay(700100);
displtimeon = millis(); //un-commentreset forbacklight serialtimeout
debugging }
if(millis() > displtimeon + displtime){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight, LOW); //turn off the screen
calstate = false;
Serial.printprintln("setpoint"displ + backlights off");
lcd.clear();
calunlock = "0; //lock calibration
}
if(millis() < lastcheck){
lastcheck = millis(); //reset timers in a millisecond rollover
}
//--------SETPOINT ERROR PROCEDURE
if(setpoint > 91 or setpoint < 9){ //in case setpoint is ever out of bounds
Serial.println(setpoint"O/B ERROR");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursor(0,1);
lcd.print("RESETTING");
delay(7001000);
for(int count = 9; count >= 0; count = count - 1){
lcd.setCursor(15,1); lcd.print(count); //un-commentcount fordown serialfrom debugging10
delay(1000);
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
//turn on humidifier relay if below setpoint --------RELAY CONTROL
//RELAY MODULE IS ACTIVE LOW
if(DHT.humidity <= setpoint - 3 and syson == true){ //if humidity is 3% lower than setpoint
Serial.println("humidifier ON, fan OFF");
digitalWrite(humidifier, LOW); //turn on humidifier
digitalWrite(fan, HIGH);
}
else if(DHT.humidity >= setpoint + 3 and syson == true){ //if humidity is 3% above setpoint
Serial.println("humidifier OFF, fan ON");
digitalWrite(humidifier, HIGH); //turn on fan
digitalWrite(fan, LOW);
}
else{
Serial.println("all off"); //if humidity is within 3% of setpoint
digitalWrite(humidifier, HIGH); //turn both off
digitalWrite(fan, HIGH);
}
//delay(700); //un-comment for serial debugging
Serial.print("setpoint = ");
Serial.println(setpoint);
//delay(700); //un-comment for serial debugging
}
/* WIRING DIAGRAMS
https://66.media.tumblr.com/98426b566744beacdd42bc0221092b76/tumblr_pvr0u8kvZy1yrmjh9o1_1280.png
*/
//HUMIDITY CONTROLLER 1.1
//written for NANO
//controls and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.
//added: "system off" function - allows both outlets to be turned off
// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT DHT_sens(10, DHT22);
#define DHT22_PIN A2
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration variable
int setpoint = 50; //feedback setpoint.
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of time the display is to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){
//check calibration buttons
calup = digitalRead(calupbtn);
caldown = digitalRead(caldownbtn);
if(calup == HIGH and caldown == HIGH){ //--------SYSTEM TOGGLE
syslock ++;
Serial.println(syslock);
if(syslock == 20){ //if both buttons held down for this many loops
systog++;
if(systog % 2 == 1){ //--------SYSTEM OFF
syson = false;
digitalWrite(fan, HIGH);
digitalWrite(humidifier, HIGH);
digitalWrite(lcdlight, HIGH);
Serial.println("SYSTEM TURNED OFF");
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(2000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
if(systog % 2 == 0){ //--------SYSTEM ON
syson = true;
Serial.println("SYSTEM TURNED ON");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM ON");
delay(2000);
lcd.clear();
displtimeon = millis();
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(millis() > lastcheck + interval and syson == true){ //read the DHT humidity
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.print("DHT read = ");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.println(" ");
}
//turn on the led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
digitalWrite(lcdlight, HIGH);
calstate = true;
displtimeon = millis(); //set display timer
Serial.println("cal btn ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
if(calstate == true and syson == true){ //--------DISPLAY ROUTINE
Serial.println("printing display");
//display variables on LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//keeps calibration locked until display cycles 5 times after initially turned on
//prevents adjustments on initial button press.
}
//--------CALIBRATION ADJUSTMENTS
if(calup == HIGH and caldown == LOW and calstate == true and syson == true){
if(setpoint < 90 and calunlock > 5){
setpoint = setpoint + 5; //increase setpoint
Serial.println("adj setpoint up");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
if(setpoint > 10 and calunlock > 5){
setpoint = setpoint - 5; //decrease setpoint
Serial.println("adj setpoint dn");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(millis() > displtimeon + displtime){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight, LOW); //turn off the screen
calstate = false;
Serial.println("displ + backlights off");
lcd.clear();
calunlock = 0; //lock calibration
}
if(millis() < lastcheck){
lastcheck = millis(); //reset timers in a millisecond rollover
}
//--------SETPOINT ERROR PROCEDURE
if(setpoint > 91 or setpoint < 9){ //in case setpoint is ever out of bounds
Serial.println("O/B ERROR");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursor(0,1);
lcd.print("RESETTING");
delay(1000);
for(int count = 9; count >= 0; count = count - 1){
lcd.setCursor(15,1);
lcd.print(count); //count down from 10
delay(1000);
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
//turn on humidifier relay if below setpoint --------RELAY CONTROL
//RELAY MODULE IS ACTIVE LOW
if(DHT.humidity <= setpoint - 3 and syson == true){ //if humidity is 3% lower than setpoint
Serial.println("humidifier ON, fan OFF");
digitalWrite(humidifier, LOW); //turn on humidifier
digitalWrite(fan, HIGH);
}
else if(DHT.humidity >= setpoint + 3 and syson == true){ //if humidity is 3% above setpoint
Serial.println("humidifier OFF, fan ON");
digitalWrite(humidifier, HIGH); //turn on fan
digitalWrite(fan, LOW);
}
else{
Serial.println("all off"); //if humidity is within 3% of setpoint
digitalWrite(humidifier, HIGH); //turn both off
digitalWrite(fan, HIGH);
}
//delay(700); //un-comment for serial debugging
Serial.print("setpoint = ");
Serial.println(setpoint);
//delay(700); //un-comment for serial debugging
}
//HUMIDITY CONTROLLER 1.1
//written for NANO
//controls and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.
//added: "system off" function - allows both outlets to be turned off
// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht DHT;
#define DHT11_PIN A2
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration variable
int setpoint = 50; //feedback setpoint.
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of time the display is to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){
//check calibration buttons
calup = digitalRead(calupbtn);
caldown = digitalRead(caldownbtn);
if(calup == HIGH and caldown == HIGH){ //--------SYSTEM TOGGLE
syslock ++;
Serial.println(syslock);
if(syslock == 20){ //if both buttons held down for this many loops
systog++;
if(systog % 2 == 1){ //--------SYSTEM OFF
syson = false;
digitalWrite(fan, HIGH);
digitalWrite(humidifier, HIGH);
digitalWrite(lcdlight, HIGH);
Serial.println("SYSTEM TURNED OFF");
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(2000);
lcd.setCursor(0,1);
lcd.print(" to turn on "); displtimeon = millis();
}
if(systog % 2 == 0){ //--------SYSTEM ON
syson = true;
Serial.println("SYSTEM TURNED ON");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM ON");
delay(2000);
lcd.clear();
displtimeon = millis();
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(millis() > lastcheck + interval and syson == true){ //read the DHT humidity int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.print("DHT read = ");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.println(" ");
}
//turn on the led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
digitalWrite(lcdlight, HIGH);
calstate = true;
displtimeon = millis(); //set display timer
Serial.println("cal btn ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
if(calstate == true and syson == true){ //--------DISPLAY ROUTINE
Serial.println("printing display"); //display variables on LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//keeps calibration locked until display cycles 5 times after initially turned on
//prevents adjustments on initial button press.
}
//--------CALIBRATION ADJUSTMENTS
if(calup == HIGH and caldown == LOW and calstate == true and syson == true){
if(setpoint < 90 and calunlock > 5){
setpoint = setpoint + 5; //increase setpoint
Serial.println("adj setpoint up");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
if(setpoint > 10 and calunlock > 5){
setpoint = setpoint - 5; //decrease setpoint
Serial.println("adj setpoint dn");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(millis() > displtimeon + displtime){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight, LOW); //turn off the screen
calstate = false;
Serial.println("displ + backlights off");
lcd.clear();
calunlock = 0; //lock calibration
}
if(millis() < lastcheck){
lastcheck = millis(); //reset timers in a millisecond rollover
}
//--------SETPOINT ERROR PROCEDURE
if(setpoint > 91 or setpoint < 9){ //in case setpoint is ever out of bounds
Serial.println("O/B ERROR");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursor(0,1);
lcd.print("RESETTING");
delay(1000);
for(int count = 9; count >= 0; count = count - 1){
lcd.setCursor(15,1); lcd.print(count); //count down from 10
delay(1000);
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
//turn on humidifier relay if below setpoint --------RELAY CONTROL
//RELAY MODULE IS ACTIVE LOW
if(DHT.humidity <= setpoint - 3 and syson == true){ //if humidity is 3% lower than setpoint
Serial.println("humidifier ON, fan OFF");
digitalWrite(humidifier, LOW); //turn on humidifier
digitalWrite(fan, HIGH);
}
else if(DHT.humidity >= setpoint + 3 and syson == true){ //if humidity is 3% above setpoint
Serial.println("humidifier OFF, fan ON");
digitalWrite(humidifier, HIGH); //turn on fan
digitalWrite(fan, LOW);
}
else{
Serial.println("all off"); //if humidity is within 3% of setpoint
digitalWrite(humidifier, HIGH); //turn both off
digitalWrite(fan, HIGH);
}
//delay(700); //un-comment for serial debugging
Serial.print("setpoint = ");
Serial.println(setpoint);
//delay(700); //un-comment for serial debugging
}
/* WIRING DIAGRAMS
https://66.media.tumblr.com/98426b566744beacdd42bc0221092b76/tumblr_pvr0u8kvZy1yrmjh9o1_1280.png
*/
Thomas Weller
- 1.1k
- 1
- 8
- 22
This is NOT my code; was posted on Project Hubposted on Project Hub. I tried to use the code (humidity controller) but it returns an error on line 270:
This is NOT my code; was posted on Project Hub. I tried to use the code (humidity controller) but it returns an error on line 270:
This is NOT my code; was posted on Project Hub. I tried to use the code (humidity controller) but it returns an error on line 270:
//HUMIDITY CONTROLLER 1.1
//written for NANO
//controls and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.
//added: "system off" function - allows both outlets to be turned off
// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht<Wire.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht DHT;
#define DHT11_PIN A2
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration#include variable<LiquidCrystal_I2C.h>
int setpoint = 50; //feedback#include setpoint<DHT.h>
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of timeset the displayLCD isaddress to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter0x27 for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
a 16 lcd.print("%chars and ");
2 line lcd.setCursor(0,1);display
lcd.print("setting: ");
LiquidCrystal_I2C lcd.setCursor(130x27,1);
lcd.print(setpoint);
lcd.setCursor(1516,1);
lcd.print("% ");
delay(1002);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
voidDHT loopDHT_sens(10, DHT22){;
//check calibration buttons
calup = digitalRead(calupbtn);
caldown#define =DHT22_PIN digitalRead(caldownbtn);A2
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration variable
int setpoint = 50; //feedback setpoint.
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of time the display is to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){
//check calibration buttons
calup = digitalRead(calupbtn);
caldown = digitalRead(caldownbtn);
if(calup == HIGH and caldown == HIGH){ //--------SYSTEM TOGGLE
syslock ++;
Serial.println(syslock);
if(syslock == 20){ //if both buttons held down for this many loops
systog++;
if(systog % 2 == 1){ //--------SYSTEM OFF
syson = false;
digitalWrite(fan, HIGH);
digitalWrite(humidifier, HIGH);
digitalWrite(lcdlight, HIGH);
Serial.println("SYSTEM TURNED OFF");
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(2000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
if(systog % 2 == 0){ //--------SYSTEM ON
syson = true;
Serial.println("SYSTEM TURNED ON");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM ON");
delay(2000);
lcd.clear();
displtimeon = millis();
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(millis() > lastcheck + interval and syson == true){ //read the DHT humidity
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.print("DHT read = ");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.println(" ");
}
//turn on the led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
digitalWrite(lcdlight, HIGH);
calstate = true;
displtimeon = millis(); //set display timer
Serial.println("SYSTEM"cal TURNEDbtn OFF"ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(20003000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
if(calstate == true and syson == true){ //--------DISPLAY ROUTINE
Serial.println("printing display");
//display variables on LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
to turn on "lcd.setCursor(13,1);
displtimeonlcd.print(setpoint);
= millis lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//keeps calibration locked until display cycles 5 times after initially turned on
//prevents adjustments on initial button press.
}
//--------CALIBRATION ADJUSTMENTS
if(systogcalup %== 2HIGH and caldown == 0LOW and calstate == true and syson == true){
if(setpoint < 90 and calunlock > 5){ setpoint = setpoint + 5; //--------SYSTEMincrease ONsetpoint
Serial.println("adj setpoint up");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
if(setpoint > 10 and calunlock > 5){
setpoint = true;setpoint - 5; //decrease setpoint
Serial.println("SYSTEM"adj TURNEDsetpoint ON"dn");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(millis() > displtimeon + displtime){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight, HIGHLOW); //turn off the screen
calstate = false;
Serial.println("displ + backlights off");
lcd.clear();
lcd.printcalunlock = 0; //lock calibration
}
if("millis() < lastcheck){
SYSTEM ON" lastcheck = millis(); //reset timers in a millisecond rollover
}
delay
//--------SETPOINT ERROR PROCEDURE
if(2000setpoint > 91 or setpoint < 9){ //in case setpoint is ever out of bounds
Serial.println("O/B ERROR");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursor(0,1);
lcd.print("RESETTING");
delay(1000);
for(int count = 9; count >= 0; count = count - 1){
lcd.setCursor(15,1);
lcd.print(count); //count down from 10
delay(1000);
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(millis() > lastcheck + interval and syson == true){ //read the DHT humidity
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.print("DHT read = ");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.println(" ");
}
//turn on the led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
digitalWrite(lcdlight, HIGH);
calstate = true;
displtimeon = millis(); //set display timer
Serial.println("cal btn ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
humidifier relay if(calstate == true and syson ==below true){setpoint //--------DISPLAYRELAY ROUTINECONTROL
Serial.println("printing display");
//display variables on LCD
lcd.setCursor(0,0);
RELAY MODULE IS lcd.print("Humidity:ACTIVE ");LOW
lcd.setCursor(13,0);
lcd.printif(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
<= lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//keeps calibration locked until display cycles 5 times after initially turned on
//prevents adjustments on initial button press.
}
//--------CALIBRATION ADJUSTMENTS
if(calup == HIGH and caldown == LOW and calstate == true3 and syson == true){ if(setpoint < 90 and calunlock > 5){
setpoint = setpoint + 5; //increase setpoint
if humidity is 3% lower Serial.println("adjthan setpoint up");
}
Serial.println(setpoint);
delay(100);
"humidifier displtimeonON, =fan millis(OFF"); //reset backlight timeout
}
if(caldown == HIGH and calup ==digitalWrite(humidifier, LOW and calstate == true and syson == true){
if(setpoint > 10; and calunlock//turn >on 5){humidifier
setpoint = setpoint - 5; //decrease setpoint
Serial.printlndigitalWrite("adj setpointfan, dn"HIGH);
}
Serial.println(setpoint);
delay(100);
displtimeon =else millisif(); //reset backlight timeout
DHT.humidity >= }
setpoint + if(millis()3 >and displtimeonsyson +== displtimetrue){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight,if LOW);humidity //turnis off3% theabove screensetpoint
calstate = false;
Serial.println("displ"humidifier +OFF, backlightsfan off"ON");
lcd.clear();
calunlock =digitalWrite(humidifier, 0;HIGH); //lock calibration
turn on }
fan
if(millis() < lastcheck){
lastcheck = millisdigitalWrite(fan, LOW); //reset timers in a millisecond rollover
}
else{
//--------SETPOINT ERROR PROCEDURE
ifSerial.println(setpoint > 91 or setpoint <"all 9off"){; //in caseif setpointhumidity is everwithin out3% of boundssetpoint
Serial.println("O/B ERROR");
digitalWrite(lcdlighthumidifier, HIGH); //turn both lcd.clear();off
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursordigitalWrite(0fan,1 HIGH);
lcd.print("RESETTING"); }
//delay(1000700); //un-comment for(int count = 9; count >=serial 0;debugging
count = count - 1){
lcdSerial.setCursorprint(15,1"setpoint = ");
lcdSerial.printprintln(countsetpoint); //count down from 10
//delay(1000700); //un-comment for serial debugging
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
//turn on humidifier relay if below setpoint --------RELAY CONTROL
//RELAY MODULE IS ACTIVE LOW
if(DHT.humidity <= setpoint - 3 and syson == true){ //if humidity is 3% lower than setpoint
Serial.println("humidifier ON, fan OFF");
digitalWrite(humidifier, LOW); //turn on humidifier
digitalWrite(fan, HIGH);
}
else if(DHT.humidity >= setpoint + 3 and syson == true){ //if humidity is 3% above setpoint
Serial.println("humidifier OFF, fan ON");
digitalWrite(humidifier, HIGH); //turn on fan
digitalWrite(fan, LOW);
}
else{
Serial.println("all off"); //if humidity is within 3% of setpoint
digitalWrite(humidifier, HIGH); //turn both off
digitalWrite(fan, HIGH);
}
//delay(700); //un-comment for serial debugging
Serial.print("setpoint = ");
Serial.println(setpoint);
//delay(700); //un-comment for serial debugging
}
//HUMIDITY CONTROLLER 1.1
//written for NANO
//controls and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.
//added: "system off" function - allows both outlets to be turned off
// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht DHT;
#define DHT11_PIN A2
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration variable
int setpoint = 50; //feedback setpoint.
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of time the display is to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){
//check calibration buttons
calup = digitalRead(calupbtn);
caldown = digitalRead(caldownbtn);
if(calup == HIGH and caldown == HIGH){ //--------SYSTEM TOGGLE
syslock ++;
Serial.println(syslock);
if(syslock == 20){ //if both buttons held down for this many loops
systog++;
if(systog % 2 == 1){ //--------SYSTEM OFF
syson = false;
digitalWrite(fan, HIGH);
digitalWrite(humidifier, HIGH);
digitalWrite(lcdlight, HIGH);
Serial.println("SYSTEM TURNED OFF");
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(2000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
if(systog % 2 == 0){ //--------SYSTEM ON
syson = true;
Serial.println("SYSTEM TURNED ON");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM ON");
delay(2000);
lcd.clear();
displtimeon = millis();
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(millis() > lastcheck + interval and syson == true){ //read the DHT humidity
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.print("DHT read = ");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.println(" ");
}
//turn on the led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
digitalWrite(lcdlight, HIGH);
calstate = true;
displtimeon = millis(); //set display timer
Serial.println("cal btn ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
if(calstate == true and syson == true){ //--------DISPLAY ROUTINE
Serial.println("printing display");
//display variables on LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//keeps calibration locked until display cycles 5 times after initially turned on
//prevents adjustments on initial button press.
}
//--------CALIBRATION ADJUSTMENTS
if(calup == HIGH and caldown == LOW and calstate == true and syson == true){ if(setpoint < 90 and calunlock > 5){
setpoint = setpoint + 5; //increase setpoint
Serial.println("adj setpoint up");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
if(setpoint > 10 and calunlock > 5){
setpoint = setpoint - 5; //decrease setpoint
Serial.println("adj setpoint dn");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(millis() > displtimeon + displtime){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight, LOW); //turn off the screen
calstate = false;
Serial.println("displ + backlights off");
lcd.clear();
calunlock = 0; //lock calibration
}
if(millis() < lastcheck){
lastcheck = millis(); //reset timers in a millisecond rollover
}
//--------SETPOINT ERROR PROCEDURE
if(setpoint > 91 or setpoint < 9){ //in case setpoint is ever out of bounds
Serial.println("O/B ERROR");
digitalWrite(lcdlight, HIGH); lcd.clear();
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursor(0,1);
lcd.print("RESETTING");
delay(1000); for(int count = 9; count >= 0; count = count - 1){
lcd.setCursor(15,1);
lcd.print(count); //count down from 10
delay(1000);
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
//turn on humidifier relay if below setpoint --------RELAY CONTROL
//RELAY MODULE IS ACTIVE LOW
if(DHT.humidity <= setpoint - 3 and syson == true){ //if humidity is 3% lower than setpoint
Serial.println("humidifier ON, fan OFF");
digitalWrite(humidifier, LOW); //turn on humidifier
digitalWrite(fan, HIGH);
}
else if(DHT.humidity >= setpoint + 3 and syson == true){ //if humidity is 3% above setpoint
Serial.println("humidifier OFF, fan ON");
digitalWrite(humidifier, HIGH); //turn on fan
digitalWrite(fan, LOW);
}
else{
Serial.println("all off"); //if humidity is within 3% of setpoint
digitalWrite(humidifier, HIGH); //turn both off
digitalWrite(fan, HIGH);
}
//delay(700); //un-comment for serial debugging
Serial.print("setpoint = ");
Serial.println(setpoint);
//delay(700); //un-comment for serial debugging
}
//HUMIDITY CONTROLLER 1.1
//written for NANO
//controls and displays relative humidity
//sesnor used is DHT11
//uses active-low relay module to control live lines for 2 standard electrical outlets
//uses i2c lcd to display humidity and humidity setting
//turns display off during inactivity
//setting is adjustable from 10%-90% using 2 buttons
//backlight of LCD is controlled by pin 4, connected to top LED jumper pin on i2c backpack
//serial communications to monitor to ensure all code is working.
//added: "system off" function - allows both outlets to be turned off
// █ G █ L █ O █ B █ A █ L █ S █
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT DHT_sens(10, DHT22);
#define DHT22_PIN A2
//buttons and variables to adjust calibration
int calupbtn = A0;
int calup;
int caldownbtn = A1;
int caldown;
//i2c PINS A4, A5
//pin for turning on humidifier relay
int humidifier = 3;
//pin for turning on the fan relay
int fan = 2;
//pin for turning on LCD backlights
int lcdlight = 4;
//calibration variable
int setpoint = 50; //feedback setpoint.
bool calstate = false; //enables calibration adjustments only when LCD is ON.
//backlight timing variables
int displtime = 12000; //amount of time the display is to be on before turning off
unsigned long displtimeon; //last recorded time display timer was reset
int calunlock = 0; //loop counter for unlocking calibration
//sensor timing variables
unsigned long lastcheck; //last time DHT was read
long interval = 30000; //time between DHT readings
//system variables
bool syson = true; //reference for system on/off.
byte systog = 2; //even number toggles on, odds toggle off
int syslock = 0; //loop counter for unlocking system toggle
// █ S █ E █ T █ U █ P █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █ + █
void setup(){
Serial.begin(9600); //serial communication used to ensure code is working correctly.
lcd.init(); //initialize LCD
lcd.backlight(); //enable LCD backlight, but doesn't turn it on
lastcheck = 0;
digitalWrite(lcdlight, HIGH);
Serial.println("STARTING");
lcd.print(" STARTING");
//pin assignments
pinMode(calupbtn, INPUT);
pinMode(caldownbtn, INPUT);
pinMode(humidifier, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(lcdlight, OUTPUT);
delay(1000);
//test fan
lcd.setCursor(0,1);
lcd.print("<FAN> HMDFR ");
digitalWrite(fan, LOW);
digitalWrite(humidifier, HIGH);
delay(6000);
//test humidifier
lcd.setCursor(0,1);
lcd.print(" FAN <HMDFR> ");
digitalWrite(fan, HIGH);
digitalWrite(humidifier, LOW);
delay(6000);
//stop startup test
digitalWrite(humidifier, HIGH);
digitalWrite(fan, HIGH);
lcd.clear();
displtimeon = millis();
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
}
// █ L █ O █ O █ P █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █ = █
void loop(){
//check calibration buttons
calup = digitalRead(calupbtn);
caldown = digitalRead(caldownbtn);
if(calup == HIGH and caldown == HIGH){ //--------SYSTEM TOGGLE
syslock ++;
Serial.println(syslock);
if(syslock == 20){ //if both buttons held down for this many loops
systog++;
if(systog % 2 == 1){ //--------SYSTEM OFF
syson = false;
digitalWrite(fan, HIGH);
digitalWrite(humidifier, HIGH);
digitalWrite(lcdlight, HIGH);
Serial.println("SYSTEM TURNED OFF");
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(2000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
if(systog % 2 == 0){ //--------SYSTEM ON
syson = true;
Serial.println("SYSTEM TURNED ON");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM ON");
delay(2000);
lcd.clear();
displtimeon = millis();
}
syslock = 0;
}
}
else(syslock = 0);
//read humidity at appropriate intervals
if(millis() > lastcheck + interval and syson == true){ //read the DHT humidity
int chk = DHT.read11(DHT11_PIN);
lastcheck = millis();
Serial.print("DHT read = ");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.println(" ");
}
//turn on the led lights when calibration buttons are pressed
if(calup == HIGH xor caldown == HIGH){
digitalWrite(lcdlight, HIGH);
calstate = true;
displtimeon = millis(); //set display timer
Serial.println("cal btn ACTIVE");
if(syson == false){
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print(" SYSTEM OFF");
lcd.setCursor(0,1);
lcd.print(" hold both btns");
delay(3000);
lcd.setCursor(0,1);
lcd.print(" to turn on ");
displtimeon = millis();
}
}
if(calstate == true and syson == true){ //--------DISPLAY ROUTINE
Serial.println("printing display");
//display variables on LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.setCursor(13,0);
lcd.print(DHT.humidity);
lcd.setCursor(15,0);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("setting: ");
lcd.setCursor(13,1);
lcd.print(setpoint);
lcd.setCursor(15,1);
lcd.print("% ");
delay(100);
calunlock ++;
//keeps calibration locked until display cycles 5 times after initially turned on
//prevents adjustments on initial button press.
}
//--------CALIBRATION ADJUSTMENTS
if(calup == HIGH and caldown == LOW and calstate == true and syson == true){
if(setpoint < 90 and calunlock > 5){ setpoint = setpoint + 5; //increase setpoint
Serial.println("adj setpoint up");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(caldown == HIGH and calup == LOW and calstate == true and syson == true){
if(setpoint > 10 and calunlock > 5){
setpoint = setpoint - 5; //decrease setpoint
Serial.println("adj setpoint dn");
}
Serial.println(setpoint);
delay(100);
displtimeon = millis(); //reset backlight timeout
}
if(millis() > displtimeon + displtime){ //-----------------BACKLIGHT TIMEOUT
digitalWrite(lcdlight, LOW); //turn off the screen
calstate = false;
Serial.println("displ + backlights off");
lcd.clear();
calunlock = 0; //lock calibration
}
if(millis() < lastcheck){
lastcheck = millis(); //reset timers in a millisecond rollover
}
//--------SETPOINT ERROR PROCEDURE
if(setpoint > 91 or setpoint < 9){ //in case setpoint is ever out of bounds
Serial.println("O/B ERROR");
digitalWrite(lcdlight, HIGH);
lcd.clear();
lcd.print("O/B ERROR"); //display error message on lcd
lcd.setCursor(0,1);
lcd.print("RESETTING");
delay(1000);
for(int count = 9; count >= 0; count = count - 1){
lcd.setCursor(15,1);
lcd.print(count); //count down from 10
delay(1000);
}
setpoint = 50; //reset setpoint at 50.
displtimeon = millis();
}
//turn on humidifier relay if below setpoint --------RELAY CONTROL
//RELAY MODULE IS ACTIVE LOW
if(DHT.humidity <= setpoint - 3 and syson == true){ //if humidity is 3% lower than setpoint
Serial.println("humidifier ON, fan OFF");
digitalWrite(humidifier, LOW); //turn on humidifier
digitalWrite(fan, HIGH);
}
else if(DHT.humidity >= setpoint + 3 and syson == true){ //if humidity is 3% above setpoint
Serial.println("humidifier OFF, fan ON");
digitalWrite(humidifier, HIGH); //turn on fan
digitalWrite(fan, LOW); }
else{
Serial.println("all off"); //if humidity is within 3% of setpoint
digitalWrite(humidifier, HIGH); //turn both off
digitalWrite(fan, HIGH);
}
//delay(700); //un-comment for serial debugging
Serial.print("setpoint = ");
Serial.println(setpoint);
//delay(700); //un-comment for serial debugging
}