Hi my friend,
I post here my code because I have a problem when I save data into EEPROM
#include <EEPROM.h>
#include <dht.h>
#include <LiquidCrystal.h>
dht DHT;
//Definisco tutti i PIN
#define UMIDIFICATORE 1
#define RISCALDATORE 6
#define NEXT 7
#define DOWN 8
#define UP 9
#define OK 10
#define DHT11_PIN 13
//Inizializzo il display Hitachi HDD7780 (16x2)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Creo il menù e il submenù
char* menu_principale[4] = {
"Home","Temperatura","Umidita'","Configurazione" };
char* submenut[2] = {
"Tmax","Tmin" };
char* submenuh[2] = {
"Hmax","Hmin" };
//Variabili
int posizione = 0;
double tmax = 0;
double tmin = 0;
double hmax = 0;
double hmin = 0;
void setup(){
Serial.begin(9600);
//Inizializzazione lcd
lcd.begin(16,2);
//Inizializzazione PIN (INPUT - OUTPUT)
pinMode(NEXT, INPUT);
digitalWrite(NEXT, HIGH);
pinMode(DOWN, INPUT);
digitalWrite(DOWN, HIGH);
pinMode(UP, INPUT);
digitalWrite(UP, HIGH);
pinMode(OK, INPUT);
digitalWrite(OK, HIGH);
pinMode(RISCALDATORE, OUTPUT);
digitalWrite(RISCALDATORE, LOW);
pinMode(UMIDIFICATORE, OUTPUT);
digitalWrite(UMIDIFICATORE, LOW);
//Pulisco e setto il display
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LOADING...");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Home");
}
void loop(){
int chk = DHT.read11(DHT11_PIN);
//Gestisco il sensore di umidità e temperatura DHT11
switch (chk)
{
case DHTLIB_OK:
//Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
//Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
//Serial.print("Time out error,\t");
break;
default:
//Serial.print("Unknown error,\t");
break;
}
//Menù
if(digitalRead(NEXT) == LOW){
posizione++;
if ( posizione > 3 )
posizione = 0;
if ( posizione < 0 )
posizione = 3;
delay(200);
switch(posizione){
case 0:
lcd.clear();
lcd.setCursor(0,0);
lcd.print(menu_principale[0]);
lcd.setCursor(0,1);
//Visualizzo i valori letti dal sensore DHT11
lcd.print(DHT.humidity,2);
lcd.print("% ");
lcd.print(DHT.temperature,2);
lcd.print("C");
delay(200);
break;
case 1:
//Inizializzo le sogliee con i valori salvati nella EEPROM
tmax = EEPROM_readDouble(0);
tmin = EEPROM_readDouble(4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(menu_principale[1]);
//Imposto le soglie di temperature
tmax = set_temp_max();
tmin = set_temp_min();
delay(200);
break;
case 2:
//Inizializzo le sogliee con i valori salvati nella EEPROM
hmax = EEPROM_readDouble(8);
hmin = EEPROM_readDouble(12);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(menu_principale[2]);
//Imposte le soglie di umidità
hmax = set_hum_max();
hmin = set_hum_min();
delay(200);
break;
case 3:
//Visualizzo le tutte le soglie impostate
lcd.clear();
lcd.setCursor(0,0);
//lcd.print(menu_principale[3]);
lcd.print("H:");
lcd.print(hmax);
lcd.print("% ");
lcd.print(hmin);
lcd.print("% ");
lcd.setCursor(0,1);
lcd.print("T:");
lcd.print(tmax);
lcd.print("C ");
lcd.print(tmin);
lcd.print("C ");
//Salvo le soglie nella EEPROM
EEPROM_writeDouble(0, tmax);
EEPROM_writeDouble(4, tmin);
EEPROM_writeDouble(8, hmax);
EEPROM_writeDouble(12, hmin);
delay(200);
break;
}
//Gestisco l'umidificatore e il riscaldatore in funzione delle soglie e dei valori letti
if(DHT.temperature > tmax){
digitalWrite(RISCALDATORE, LOW);
}
else {
digitalWrite(RISCALDATORE, HIGH);
}
if(DHT.temperature < tmin){
digitalWrite(RISCALDATORE, HIGH);
}
else {
digitalWrite(RISCALDATORE, LOW);
}
if(DHT.humidity > hmax){
digitalWrite(UMIDIFICATORE, LOW);
}
else {
digitalWrite(UMIDIFICATORE, HIGH);
}
if(DHT.humidity < hmin){
digitalWrite(UMIDIFICATORE, HIGH);
}
else {
digitalWrite(UMIDIFICATORE, LOW);
}
}
}
//Imposto la soglia di temperatura massima
double set_temp_max(){
int i = 0;
boolean message = true;
int tup;
int tdown;
int tok;
while(i == 0){
tup = digitalRead(UP);
tdown = digitalRead(DOWN);
tok = digitalRead(OK);
if( message ){
lcd.clear();
lcd.print(submenut[i]);
message = false;
}
if(tup == LOW || tdown == LOW || tok == LOW){
if(tup == LOW){
if(i == 0){
tmax = tmax + 0.1;
lcd.setCursor(0,1);
lcd.print(tmax);
}
}
if(tdown == LOW){
if(i == 0){
tmax = tmax - 0.1;
lcd.setCursor(0,1);
lcd.print(tmax);
}
}
if(tok == LOW){
i = i + 1;
message = true;
delay(200);
}
}
delay( 100 );
}
return tmax;
}
//Imposto la soglia di temperatura minima
double set_temp_min(){
int i = 1;
boolean message = true;
int tup;
int tdown;
int tok;
while(i == 1){
tup = digitalRead(UP);
tdown = digitalRead(DOWN);
tok = digitalRead(OK);
if( message ){
lcd.clear();
lcd.print(submenut[i]);
message = false;
}
if(tup == LOW || tdown == LOW || tok == LOW){
if(tup == LOW){
if(i == 1){
tmin = tmin + 0.1;
lcd.setCursor(0,1);
lcd.print(tmin);
}
}
if(tdown == LOW){
if(i == 1){
tmin = tmin - 0.1;
lcd.setCursor(0,1);
lcd.print(tmin);
}
}
if(tok == LOW){
i = i + 1;
message = true;
delay(200);
}
}
delay( 100 );
}
return tmin;
}
//Imposto la soglia di umidità massima
double set_hum_max(){
int j = 0;
boolean message = true;
int hup;
int hdown;
int hok;
while(j == 0){
hup = digitalRead(UP);
hdown = digitalRead(DOWN);
hok = digitalRead(OK);
if( message ){
lcd.clear();
lcd.print(submenuh[j]);
message = false;
}
if(hup == LOW || hdown == LOW || hok == LOW){
if(hup == LOW){
if(j == 0){
hmax = hmax + 0.1;
lcd.setCursor(0,1);
lcd.print(hmax);
}
}
if(hdown == LOW){
if(j == 0){
hmax = hmax - 0.1;
lcd.setCursor(0,1);
lcd.print(hmax);
}
}
if(hok == LOW){
j = j + 1;
message = true;
delay(200);
}
}
delay( 100 );
}
return hmax;
}
//Imposto la soglia di umidità minima
double set_hum_min(){
int j = 1;
boolean message = true;
int hup;
int hdown;
int hok;
while(j == 1){
hup = digitalRead(UP);
hdown = digitalRead(DOWN);
hok = digitalRead(OK);
if( message ){
lcd.clear();
lcd.print(submenuh[j]);
message = false;
}
if(hup == LOW || hdown == LOW || hok == LOW){
if(hup == LOW){
if(j == 1){
hmin = hmin + 0.1;
lcd.setCursor(0,1);
lcd.print(hmin);
}
}
if(hdown == LOW){
if(j == 1){
hmin = hmin - 0.1;
lcd.setCursor(0,1);
lcd.print(hmin);
}
}
if(hok == LOW){
j = j + 1;
message = true;
delay(200);
}
}
delay( 100 );
}
return hmin;
}
//Funzione salva su EEPROM
void EEPROM_writeDouble(int ee, double value){
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
}
//Funzione leggi da EEPROM
double EEPROM_readDouble(int ee){
double value = 0.0;
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return value;
}
I check the temperature and humidity to terrarium, in my first program I didn't save data in the EEPROM but when I turn off the power Arduino lost my configuration. So I introduce the last two function:
void EEPROM_writeDouble(int ee, double value)
double EEPROM_readDouble(int ee)
But now I have a problem, when I press the button UP the variable tmax doesn't increase. It's strange because instead tmin, hmax and hmin when I press UP increase. If I remove this instruction:
tmax = EEPROM_readDouble(0);
tmin = EEPROM_readDouble(4);
The variable tmax increases. I don't understand the problem.
Can you help me, please?
Thank you.
Bye, Antonio.