#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#define MQ2pin A0
#define DHTpin 8
#define button1Pin 2
#define button2Pin 3
dht DHT;
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
volatile bool showTempHum = false;
volatile bool showGas = false;
char daysOfTheWeek[7][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
void handleButton1() {
showTempHum = true;
}
void handleButton2() {
showGas = true;
}
void setup() {
Wire.begin();
rtc.begin();
lcd.init();
lcd.backlight();
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button1Pin), handleButton1, FALLING);
attachInterrupt(digitalPinToInterrupt(button2Pin), handleButton2, FALLING);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() {
DHT.read11(DHTpin);
float t = (DHT.temperature * 9.0 / 5.0) + 32.0;
float h = DHT.humidity;
int gasValue = analogRead(MQ2pin);
if (showTempHum) {
showTempHum = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print((char)223);
lcd.print("F");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(h);
lcd.print("%");
delay(4000);
}
if (showGas) {
showGas = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas Level:");
lcd.setCursor(0, 1);
lcd.print(gasValue);
delay(4000);
}
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(now.year());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.day());
lcd.setCursor(0, 1);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
lcd.print(now.hour());
lcd.print(':');
if (now.minute() < 10) {
lcd.print('0');
}
lcd.print(now.minute());
delay(1000);
}
Comments