#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHT.h"
#define ONE_WIRE_BUS 4
#define DHTPIN 36
#define DHTTYPE DHT22
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
DHT dht(DHTPIN, DHTTYPE);
float humi,tempC ,temp;
//Variables for auto scroll
unsigned long previousMillis = 0;
unsigned long interval = 5000; //Desired wait time 10 s
int page_counter=1 ;
int up = 34; //Up button
int down = 33;
boolean current_up = LOW;
boolean last_up=LOW;
boolean last_down = LOW;
boolean current_down = LOW;
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}
void setup()
{
Serial.begin(115200);
dht.begin(); // initialize the sensor
lcd.init();
lcd.backlight(); //Turns backlight on
sensors.begin();
}
void loop(){
unsigned long currentMillis = millis();
sensors.requestTemperatures();
temp = sensors.getTempCByIndex(0);
current_up = debounce(last_up, up); //Debounce for Up button
current_down = debounce(last_down, down);
if (last_up== LOW && current_up == HIGH){ //When up button is pressed
lcd.clear(); //When page is changed, lcd clear to print new page
if(page_counter <2){ //Page counter never higher than 3(total of pages)
page_counter= page_counter +1; //Page up
}
else{
page_counter= 2;
}
}
last_up = current_up;
//Page Down
if (last_down== LOW && current_down == HIGH){ //When down button is pressed
lcd.clear(); //When page is changed, lcd clear to print new page
if(page_counter >1){ //Page counter never lower than 1 (total of pages)
page_counter= page_counter -1; //Page down
}
else{
page_counter= 1;
}
}
last_down = current_down;
switch (page_counter) {
case 1:{ //Design of home page 1
lcd.setCursor (0, 0 );
lcd.print("Victor: ");
lcd.print(temp,1);
lcd.print((char)223); // print ° character
lcd.print("C");
}
break;
case 2: { //Design of page 2
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Liv.room: ");
lcd.print(tempC,1); // print the temperature
lcd.print((char)223); // print ° character
lcd.print("C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print("Humidity: ");
lcd.print(humi,1); // print the humidity
lcd.print("%");
}
break;
}//switch end
//unsigned long currentTime = millis();
humi = dht.readHumidity(); // read humidity
tempC = dht.readTemperature(); // read temperature
if (currentMillis - previousMillis > interval) { //If interval is reached, scroll page
previousMillis = currentMillis; //replace previous millis with current millis as new start point
lcd.clear(); //lcd clear if page is changed.
if (page_counter <2){ //Page counter never higher than 3 (total of pages)
page_counter = page_counter +1; //Go to next page
}
else{
page_counter=1; //if counter higher than 3 (last page) return to page 1
}
}
delay(400);
}