#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
DHT dht(14, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
int temp;
int hum;
int y;
int m;
int d;
int h;
int mi;
int s;
int swtime = 0;
const int buttonPin = 13;
const int ledPin = 15;
int ledState = HIGH;
int buttonState = LOW;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
// pinMode(13, INPUT_PULLUP);
// pinMode(12, INPUT_PULLUP);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Hello World");
delay(3000);
//lcd.clear();
rtc.begin();
}
void loop() {
debouncesw();
/////////////////////////////////////
/* int sw1 = digitalRead(13);
delay(1000);
if (sw1 == 0) {
swtime = !swtime + 1;
lcd.clear();
}
if (swtime == 0) {
time();
}
if (swtime == 1) {
displaytemp();
}
if (swtime == 2) {
text();
}
if (swtime >= 3) {
swtime = 0;
}
Serial.print("sw1 =");
Serial.println(sw1);
Serial.print("swtime =");
Serial.println(swtime); */
} ///////////// end loop
void displaytemp() {
temp = dht.readTemperature();
hum = dht.readHumidity();
Serial.print("Temperature : ");
Serial.println(temp);
Serial.print("Humidity : ");
Serial.println(hum);
// lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp : ");
lcd.print(temp);
delay(100);
lcd.setCursor(7, 0);
lcd.print(" ");
delay(100);
lcd.setCursor(0, 1);
lcd.print("Hum : ");
lcd.print(hum);
lcd.print(" ");
}
void time() {
DateTime now = rtc.now();
y = now.year();
m = now.month();
d = now.day();
h = now.hour();
mi = now.minute();
s = now.second();
/////////////////////////////////
lcd.setCursor(0, 0);
lcd.print("Date:");
lcd.print(d);
lcd.print("/");
if (m < 10) {
lcd.print("0");
}
lcd.print(m);
lcd.print("/");
lcd.println(y + 543);
////////////////////////////////////
/////////////////////////////////
lcd.setCursor(0, 1);
lcd.print("Time:");
lcd.print(h);
lcd.print(":");
if (mi < 10) {
lcd.print("0");
}
lcd.print(mi);
lcd.print(":");
if (s < 10) {
lcd.print("0");
}
lcd.print(s);
lcd.print(" ");
/////////////////////////
}
void text() {
lcd.setCursor(0, 0);
lcd.print("Hello World");
}
////////////////////////////////////////////
void debouncesw(){
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
swtime = swtime +1;
}