#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <LCD_I2C.h>
#include <EEPROM.h>
int EEPROM_lgt = 8;
int EEPROM_add = 0;
int EEPROM_lst = 0;
long EEPROM_ok = 0;
bool useCelsius = true;
DHT dht(11, DHT11);
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change according
#define button4 4
#define pot A0
int myPos = 0;
int myPosOld = 0;
int myPosCalc = 0;
float celsius = 0;
float fahrenheit = 0;
float humidity = 0;
float saved_Celsius = 0;
float saved_Frenheit = 0;
//-------------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(button4, INPUT_PULLUP);
lcd.begin(); // If you are using more I2C devices using the Wire library use lcd.begin(false)
lcd.backlight();
dht.begin();
celsius = dht.readTemperature();
fahrenheit = dht.readTemperature(true);
humidity = dht.readHumidity();
if (EEPROM.get(0x03FC, EEPROM_ok) == 0x0A0A0A0A) {
EEPROM.get(0x03F8, EEPROM_lst);
Serial.println(EEPROM_ok, HEX);
Serial.println(EEPROM_lst, HEX);
}
else {
EEPROM.put(0x03FC, 0x0A0A0A0A);
EEPROM.put(0x03F8, EEPROM_lst);
}
saving();
lcd.setCursor(0, 0);
lcd.print(" Celsius ");
lcd.setCursor(7, 1); //
lcd.print(celsius); //
lcd.print("C");
lcd.print(" ");
}
//-------------------------------------------------------------------
void temperature() {
celsius = dht.readTemperature();
fahrenheit = dht.readTemperature(true);
humidity = dht.readHumidity();
useCelsius = !useCelsius;
if (!useCelsius) {
saving();
lcd.setCursor(0, 0);
lcd.print(" Fahrenheit ");
lcd.setCursor(7, 1); //
lcd.print(fahrenheit); //
lcd.print("F");
lcd.print(" ");
}
else {
saving();
lcd.setCursor(0, 0);
lcd.print(" Celsius ");
lcd.setCursor(7, 1); //
lcd.print(celsius); //
lcd.print("C");
lcd.print(" ");
}
}
//-------------------------------------------------------------------
void saving() {
lcd.setCursor(0, 0);
lcd.print(" SAVING ");
lcd.setCursor(0, 1);
lcd.print(" ");
EEPROM.put(EEPROM_lst, celsius);
EEPROM_lst += 4;
EEPROM.put(EEPROM_lst, fahrenheit);
EEPROM_lst += 4;
if (EEPROM_lst >= 0x03F8) {
EEPROM_lst >= 0x0;
}
EEPROM.put(0x03F8, EEPROM_lst);
//Serial.println(EEPROM_lst, HEX);
delay(100);
}
//-------------------------------------------------------------------
void loop() {
if (digitalRead(button4) == LOW) {
delay(30);
if (digitalRead(button4) == LOW) {
temperature();
}
}
while (digitalRead(button4) == LOW) {}
myPos = analogRead(pot);
if (myPos != myPosOld) {
myPosOld = myPos;
myPosCalc = map(myPos, 0, 1024, 0, 256) * 4;
EEPROM.get(myPosCalc, saved_Celsius);
if (!isnan(saved_Celsius)) {
Serial.print("EEPROM ");
Serial.print(myPosCalc);
Serial.print(" = ");
Serial.print(saved_Celsius);
Serial.print(" C ");
}
EEPROM.get(myPosCalc + 4, saved_Frenheit);
if (!isnan(saved_Frenheit)) {
Serial.print("EEPROM ");
Serial.print(myPosCalc + 4);
Serial.print(" = ");
Serial.print(saved_Frenheit);
Serial.println( "F" );
}
}
}