#include <EEPROM.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHTTYPE DHT22 // Sensor DHT11
#define DHTPIN 7 // Digital pin connected to the DHT sensor
#define BTN_PIN 8
#define EEPROM_MIN_T 0
#define EEPROM_MIN_H (EEPROM_MIN_T + 2)
#define EEPROM_MAX_T (EEPROM_MIN_H + 2)
#define EEPROM_MAX_H (EEPROM_MAX_T + 2)
LiquidCrystal_I2C lcd(0x27,16,2);
byte dcircle[] = {
B00110,
B01001,
B01001,
B00110,
B00000,
B00000,
B00000,
B00000
};
DHT dht(DHTPIN, DHTTYPE);
float h, t;
float max_t, max_h, min_t, min_h;
bool STATE = true;
bool button_state, last_button_state;
void write_eeprom(int pos, float v)
{
int iv = (int)(v*10);
EEPROM.put(pos, iv);
}
float read_eeprom(int pos)
{
int iv;
EEPROM.get(pos, iv);
float v = (float)(iv/10);
return v;
}
void setup()
{
lcd.init();
lcd.clear();
lcd.backlight();
lcd.createChar(0, dcircle);
dht.begin();
pinMode(BTN_PIN, INPUT_PULLUP);
Serial.begin(9600);
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
write_eeprom(EEPROM_MAX_H, h);
write_eeprom(EEPROM_MIN_H, h);
write_eeprom(EEPROM_MAX_T, t);
write_eeprom(EEPROM_MIN_T, t);
}
bool first = true;
void loop() {
if (first) {
max_h = read_eeprom(EEPROM_MAX_H);
max_t = read_eeprom(EEPROM_MAX_T);
min_h = read_eeprom(EEPROM_MIN_H);
min_t = read_eeprom(EEPROM_MIN_T);
first = false;
}
h = dht.readHumidity();
t = dht.readTemperature(); // Celsius (the default)
if (STATE)
print_cur_status();
else
print_hist_status();
if (h > max_h) {
write_eeprom(EEPROM_MAX_H, h);
max_h = h;
}
if (h < min_h) {
write_eeprom(EEPROM_MIN_H, h);
min_h = h;
}
if (t > max_t) {
write_eeprom(EEPROM_MAX_T, t);
max_t = t;
}
if (t < min_t) {
write_eeprom(EEPROM_MIN_T, t);
min_t = t;
}
// capture button value (change dino pos)
button_state = digitalRead(BTN_PIN);
if (button_state != last_button_state) {
if (button_state == LOW) {
STATE = !STATE;
}
delay(50);
}
last_button_state = button_state;
delay(500);
}
void print_cur_status()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(t, 1);
lcd.print(" ");
lcd.write(0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print(h, 1);
lcd.print(" %");
}
void print_hist_status()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(min_t, 1);
lcd.setCursor(7, 0);
lcd.print(max_t, 1);
lcd.setCursor(13, 0);
lcd.write(0);
lcd.print("C");
lcd.print("");
lcd.setCursor(0, 1);
lcd.print(min_h, 1);
lcd.setCursor(7, 1);
lcd.print(max_h, 1);
lcd.setCursor(14, 0);
lcd.print("%");
}