//DHT 22
#include "DHT.h"
#define DHTPIN 12
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
float temperature;
float humidity;
DHT dht(DHTPIN, DHTTYPE);
unsigned long currentMillis;
unsigned long previousMillisDHT22 = 0;
const unsigned long periodDHT22 = 2500;
unsigned long previousMillisRelay = 0; //previousmillis=0
unsigned long relayOffTime = 2000; //onTime
unsigned long relayOnTime =1000; //offTime
int setTemp=30;
int setHum=60;
int offTime=30;
int onTime=1;
//int offTimeMin=0;
//int onTimeMin=0;
int offTimeRelay=0;
int onTimeRelay=0;
int intervalRelay = relayOnTime; //int interval=onTime
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Arduino pins to lcd pins (RS,E,D4,D5,D6,D7)
//Counters to change positions of pages and sub-menus
int page_counter=1 ; //To move beetwen pages
int subpage_counter=0; //To move submenu 1 RGB
int subpage2_counter=0; //To move submenu 2 Led
//-------Pins-----//
int up = 8; //Up/Yes button
int sel = 14; //Select button
int down = 10; //Down/No button
int relay = 13; //relay
//---------Storage states of buttons for debounce function-----//
boolean current_up = LOW;
boolean last_up=LOW;
boolean current_sel = LOW;
boolean last_sel = LOW;
boolean last_down = LOW;
boolean current_down = LOW;
//--------Relay states-----------//
boolean relay_state = true; //boolean led state=true
//Custom return char
byte back[8] = {
0b00100,
0b01000,
0b11111,
0b01001,
0b00101,
0b00001,
0b00001,
0b11111
};
//Custom arrow char
byte arrow[8] = {
0b01000,
0b00100,
0b00010,
0b11111,
0b00010,
0b00100,
0b01000,
0b00000
};
void setup() {
// Declare pin modes
pinMode(relay, OUTPUT);
lcd.begin(16,2);
lcd.print("System ");
lcd.setCursor(1, 1);
lcd.print(" loading...");
lcd.blink();
delay(1000);
lcd.noBlink();
lcd.clear();
lcd.createChar(1, back);
lcd.createChar(2, arrow);
dht.begin();
}
//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin){
boolean current = digitalRead(pin);
if (last != current){
delay(20);
current = digitalRead(pin);
}
return current;
}
void loop() {
currentMillis = millis();
if (currentMillis - previousMillisDHT22 >= periodDHT22) {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
previousMillisDHT22 = currentMillis;
}
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
lcd.println(F("Failed to read from DHT sensor!"));
return;
}
current_up = debounce(last_up, up); //Debounce for Up button
current_sel = debounce(last_sel, sel); //Debounce for Select button
current_down = debounce(last_down, down); //Debounce for Down button
//----Page counter function to move pages----//
if(subpage_counter==0 && subpage2_counter==0){ // up/down buttons enabled if subcounters are 0,Disabled if 1,2..etc to work on submenus
//Page Up
if (last_up== LOW && current_up == HIGH){ //Up button pressed
lcd.clear(); //Clear lcd if page is changed to print new one
if(page_counter <4){ //Page counter never higher than 3(total of pages)
page_counter= page_counter +1; //Page up
}
else{
page_counter= 1; //If higher than 3 (last page) stay on page 3(change to 1 if you want to rotate)
}
}
last_up = current_up; //Save up button last state
//Page Down
if (last_down== LOW && current_down == HIGH){//Down button pressed
lcd.clear(); //Clear lcd if page is changed to print new one
if(page_counter >1){ //Page counter never lower than 1 (total of pages)
page_counter= page_counter -1; //Page down
}
else{
page_counter= 4; //If lower than 1(first page) stay on page 1(change to 3 if you want to rotate)
}
}
last_down = current_down; //Save down button last state
}
//------- Switch function to write and show what you want---//
switch (page_counter) {
case 1:{ //Design of home page 1
lcd.setCursor(0, 0);
lcd.print("Hum.: ");
lcd.print(humidity,1);
lcd.print(" ");
lcd.setCursor(13,0);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("Temp.:");
lcd.print(temperature,1);
lcd.print(" ");
lcd.setCursor(12,1);
lcd.print(char(223)); lcd.print("C");
}//case1 end
break;
case 2: { //Design of page 2 RGB control
const unsigned long periodset1 = 2000;
//Static objects
lcd.setCursor(4,0);
lcd.print("Settings");
lcd.setCursor(1,1);
lcd.print("Hu<");
lcd.setCursor(9,1);
lcd.print("Te>");
lcd.setCursor(15,1);
lcd.write(byte(1)); //Return custom char
//FUNCTIONS
// Sub counter control
if (last_sel== LOW && current_sel == HIGH){ //select button pressed
if(subpage_counter <3){ // subpage counter never higher than 4 (total of items)
subpage_counter ++; //subcounter to move beetwen submenu
}
else{ //If subpage higher than 4 (total of items) return to first item
subpage_counter=1;
}
}
last_sel=current_sel; //Save last state of select button
//First item control(subpage_counter =1) red led
if(subpage_counter==1){
lcd.setCursor(14,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(0,1);
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){ //Up, red led on
setHum=setHum+5;
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){//Down, red led off
setHum=setHum-5;
}
last_down=current_down;
}
if (setHum<0){
setHum=100;
}
if (setHum>100){
setHum=0;
}
if (setHum<10&&setHum>=0) {
lcd.setCursor(5,1);
lcd.print(" ");
}
if (setHum>100||setHum<100&&setHum>10){
lcd.setCursor(6,1);
lcd.print(" ");
}
lcd.setCursor(4,1);
lcd.print(setHum);
lcd.print(" ");
//Second item control (subpage_counter=2) green led
if(subpage_counter==2){
lcd.setCursor(0,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(8,1); //Place the arrow
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){
// green_state=HIGH;
setTemp=++setTemp;
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){
// green_state=LOW;
setTemp=--setTemp;
}
last_down=current_down;
}
if (setTemp<4){
setTemp=40;
}
if (setTemp>40){
setTemp=4;
}
if (setTemp<10){
lcd.setCursor(13,1);
lcd.print(" ");
}
}
lcd.setCursor(12,1);
lcd.print(setTemp);
//Fourth item control (subpage_counter=4) Back
if(subpage_counter==3){
lcd.setCursor(8,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(14,1); //Place the arrow
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){ //Yes button pressed
lcd.setCursor(14,1);
lcd.print(" "); //Delete arrow
subpage_counter=0; //if sub page 0, exit sub menu, up/down pages enabled
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){//No button pressed
subpage_counter=1; //Stay on sub menu, return to R
}
last_down=current_down;
}
//case2 end
break;
case 3: { //Page 3
lcd.setCursor(0,0);
lcd.print("Set op.time:");
lcd.setCursor(1,1);
lcd.print("On:");
lcd.setCursor(8,1);
lcd.print("Off:");
lcd.setCursor(15,1);
lcd.write(byte(1)); //Return custom char
// Sub counter control
if (last_sel== LOW && current_sel == HIGH){ //select button pressed
if(subpage_counter <3){ // subpage counter never higher than 4 (total of items)
subpage_counter ++; //subcounter to move beetwen submenu
}
else{ //If subpage higher than 4 (total of items) return to first item
subpage_counter=1;
}
}
last_sel=current_sel; //Save last state of select button
//First item control(subpage_counter =1) red led
if(subpage_counter==1){
lcd.setCursor(14,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(0,1);
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){ //Up, red led on
onTime=++onTime;
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){//Down, red led off
onTime=--onTime;
}
last_down=current_down;
}
if (onTime>60 ){
onTime=60;
}
if (onTime<0 ){
onTime=0;
}
if (onTime<10 ){
lcd.setCursor(5,1);
lcd.print(" ");
}
lcd.setCursor(4,1);
lcd.print(onTime);
//Second item control(subpage_counter =2)
if(subpage_counter==2){
lcd.setCursor(0,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(7,1);
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){ //Up, red led on
offTime=++offTime;
//lcd.setCursor(12,1);
//lcd.print(offTime);
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){//Down, red led off
offTime=--offTime;
}
last_down=current_down;
if (offTime>180 ){
offTime=180;
}
}
if (offTime<0 ){
offTime=0;
}
if (offTime<10 ){
lcd.setCursor(13,1);
lcd.print(" ");
}
lcd.setCursor(12,1);
lcd.print(offTime);
//Third item control (subpage_counter=3) Back
if(subpage_counter==3){
lcd.setCursor(7,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(14,1); //Place the arrow
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){ //Yes button pressed
lcd.setCursor(14,1);
lcd.print(" "); //Delete arrow
subpage_counter=0; //if sub page 0, exit sub menu, up/down pages enabled
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){//No button pressed
subpage_counter=0; //Stay on sub menu, return to R
}
last_down=current_down;
}
}//case3 end
break;
case 4: {
lcd.home();
lcd.setCursor(1,0);
lcd.print("Manual control");
lcd.setCursor(1,1);
lcd.print("Relay:");
lcd.setCursor(15,1);
lcd.write(byte(1)); //Return custom char
// Sub counter control
if (last_sel== LOW && current_sel == HIGH){ //select button pressed
if(subpage_counter <3){ // subpage counter never higher than 4 (total of items)
subpage_counter ++; //subcounter to move beetwen submenu
}
else{ //If subpage higher than 4 (total of items) return to first item
subpage_counter=1;
}
}
last_sel=current_sel; //Save last state of select button
//First item control(subpage_counter =1) red led
if(subpage_counter==1){
lcd.setCursor(14,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(0,1);
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){ //Up, red led on
relay_state=HIGH;
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){//Down, red led off
relay_state=LOW;
}
last_down=current_down;
if (relay_state==HIGH){
lcd.setCursor(7,1);
lcd.print("ON ");
}
if (relay_state==LOW){
lcd.setCursor(7,1);
lcd.print("OFF");
}
}
if(subpage_counter==2){
lcd.setCursor(0,1);
lcd.print(" "); //Delete last arrow position
lcd.setCursor(14,1); //Place the arrow
lcd.write(byte(2));
if (last_up== LOW && current_up == HIGH){ //Yes button pressed
lcd.setCursor(14,1);
lcd.print(" "); //Delete arrow
subpage_counter=0; //if sub page 0, exit sub menu, up/down pages enabled
}
last_up=current_up;
if(last_down== LOW && current_down == HIGH){//No button pressed
subpage_counter=0; //Stay on sub menu, return to R
}
last_down=current_down;
}
}//case 4 end
break;
}//switch end
// Realy timing
digitalWrite(relay, relay_state);
if (setHum>humidity && setTemp<temperature){
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(currentMillis - previousMillisRelay) >= intervalRelay) {
// Change wait interval, based on current LED state
if (relay_state) {
// LED is currently on, set time to stay off
intervalRelay = relayOffTime;
} else {
// LED is currently off, set time to stay on
intervalRelay = relayOnTime;
}
// Toggle the LED's state, Fancy, eh!?
relay_state = !(relay_state);
// Save the current time to compare "later"
previousMillisRelay = currentMillis;
}
} //end of relay timing
}//loop end