Problem with stand-by

Hi my friend,
I have a problem with my program. I want that if I don't use the circuit, the PIN 6 becomes LOW after "x" seconds.
I have written this code:

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;
  }
  
  time++;
    if(time == 2500){
      digitalWrite(RETROILL, LOW);
      time = 0;
      }
  if(digitalRead(NEXT) == LOW || digitalRead(OK) == LOW || digitalRead(UP) == LOW || digitalRead(DOWN) == LOW){
    digitalWrite(RETROILL, HIGH);
    time = 0;
    }
  
  //Menù
  if(digitalRead(NEXT) == LOW){
    posizione++;

    //Inizializzo le sogliee con i valori salvati nella EEPROM
    tmax = EEPROM_readDouble(0);
    tmin = EEPROM_readDouble(4);
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    hmax = EEPROM_readDouble(8);
    hmin = EEPROM_readDouble(12);
    
    lcd.clear();
    
    if ( posizione > 4 ) 
      posizione = 0;
    if ( posizione < 0 )
      posizione = 4; 

    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((float)DHT.humidity,2);
      lcd.print("%    ");
      lcd.print((float)DHT.temperature,2);
      lcd.print("C");
      posizione = -1;
      time = 0;
      delay(200);
      break;
    case 1:
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(menu_principale[1]);
      //Imposto le soglie di temperature
      tmax = set_temp_max();
      tmin = set_temp_min();
      //Controllo soglie temperatura
      if(tmin > tmax){
        tmin = tmax - 4;
        lcd.setCursor(0,1);
        lcd.print(tmin);
      }
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(0, tmax);
      EEPROM_writeDouble(4, tmin);
      time = 0;
      delay(200);
      break;
    case 2:
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(menu_principale[2]);
      //Imposte le soglie di umidità
      hmax = set_hum_max();
      hmin = set_hum_min();
      //Controllo soglie umidità
      if(hmin > hmax){
        hmin = hmax - 10;
        lcd.setCursor(0,1);
        lcd.print(hmin);
      }
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(8, hmax);
      EEPROM_writeDouble(12, hmin);
      time = 0;
      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 ");      
      time = 0;
      delay(200);
      break;
    case 4:
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(menu_principale[0]);
      lcd.setCursor(0,1);
      //Visualizzo i valori letti dal sensore DHT11
      lcd.print((float)DHT.humidity,2);
      lcd.print("%    ");
      lcd.print((float)DHT.temperature,2);
      lcd.print("C");
      posizione = -1;
      time = 0;
      delay(200);
      break;
    }  
    
    //Gestisco l'umidificatore e il riscaldatore in funzione delle soglie e dei valori letti
    while(posizione == -1){
      if(DHT.temperature > tmax-1){
//      Serial.println("T off 1");
        digitalWrite(RISCALDATORE, LOW);
      }
      if(DHT.temperature < tmin+1){
//      Serial.print("T on 2");
        digitalWrite(RISCALDATORE, HIGH);
      }
      if(DHT.temperature > tmin+1 && DHT.temperature < tmax-1){
//      Serial.print("T on 3");
        digitalWrite(RISCALDATORE, HIGH);
      }

      if(DHT.humidity > hmax-5){
//      Serial.print("H off 1");
        digitalWrite(UMIDIFICATORE, LOW);
      }
      if(DHT.humidity < hmin+5){
//      Serial.print("H on 2");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
      if(DHT.humidity > hmin+5 && DHT.humidity < hmax-5){
//      Serial.print("H on 3");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
 
      if(digitalRead(NEXT) == LOW){
        posizione++;
      }
    }
  }
}

But I don't understand why it doesn't go on standby when I scroll down all the menu and I come back to the "home page".

Clarification: it doesn't have to go on standby on "Temperatura" and "Umidita'' pages, but if I leave the menu on "configurazione" page, it goes on standy.

Thank you.

I have a problem with my program. I want that if I don't use the circuit, the PIN 6 becomes LOW after "x" seconds.

I can't identify pin 6 in the partial program that you posted.
What is "x" in the partial program that you posted ?
What is controlling timing in the program ?

I have resolved that problem, thank you.
But now I have another problem, the lcd screen is refreshed continuously when the program is in the case 0 e in the case 3.
How can I resolve this problem?

#include <EEPROM.h>
#include <dht.h>
#include <LiquidCrystal.h>

dht DHT;

//Definisco tutti i PIN

#define UMIDIFICATORE  A4
#define RISCALDATORE   A5

#define NEXT           7
#define DOWN           8
#define UP             9
#define OK             10
#define DHT11_PIN      13
#define RETROILL       6

//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;  
//Variabili che uso per convertire le soglie da tipo double a float
float temp_max;
float temp_min;
float hum_max;
float hum_min;
//Variabile che uso per lo standby della retroilluminazione del display
int time = 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);
  pinMode(RETROILL, OUTPUT);
  digitalWrite(RETROILL, HIGH);
  //Pulisco e setto il display
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LOADING...");
  delay(2000); 
  
  //Inizializzo il sensore
  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;
  }
  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;
  }
  
  time++;
  if(time == 300){
    digitalWrite(RETROILL, LOW);
    }
  if(digitalRead(NEXT) == LOW || digitalRead(OK) == LOW || digitalRead(UP) == LOW || digitalRead(DOWN) == LOW ){ 
    if(digitalRead(RETROILL) == LOW){ 
      digitalWrite(RETROILL, HIGH); 
      posizione--; 
    } 
    time = 0; 
  }
    
  //Menù
  if(digitalRead(NEXT) == LOW){
    posizione++;
  }
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    tmax = EEPROM_readDouble(0);
    tmin = EEPROM_readDouble(4);
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    hmax = EEPROM_readDouble(8);
    hmin = EEPROM_readDouble(12);
    
    if ( posizione > 3 ) 
      posizione = 0;
    if ( posizione < 0 )
      posizione = 3; 

    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:
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[1]);
        
      //Imposto le soglie di temperature
      tmax = set_temp_max();
      tmin = set_temp_min();
      //Controllo soglie temperatura
      if(tmin > tmax){
        tmin = tmax - 4;
        lcd.setCursor(0,1);
        lcd.print(tmin);
      }
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(0, tmax);
      EEPROM_writeDouble(4, tmin);
      time = 0;
      delay(200);
      break;
    case 2:
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[2]);
        
      //Imposte le soglie di umidità
      hmax = set_hum_max();
      hmin = set_hum_min();
      //Controllo soglie umidità
      if(hmin > hmax){
        hmin = hmax - 10;
        lcd.setCursor(0,1);
        lcd.print(hmin);
      }
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(8, hmax);
      EEPROM_writeDouble(12, hmin);
      time = 0;
      delay(200);
      break;
    case 3:      
        lcd.clear();
        //Visualizzo le tutte le soglie impostate
        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 "); 
      delay(200);   
      break;
    }  
    
    //Gestisco l'umidificatore e il riscaldatore in funzione delle soglie e dei valori letti
 //   while(posizione == -1){
      if(DHT.temperature > tmax-1){
//      Serial.println("T off 1");
        digitalWrite(RISCALDATORE, LOW);
      }
      if(DHT.temperature < tmin+1){
//      Serial.print("T on 2");
        digitalWrite(RISCALDATORE, HIGH);
      }
      if(DHT.temperature > tmin+1 && DHT.temperature < tmax-1){
//      Serial.print("T on 3");
        digitalWrite(RISCALDATORE, HIGH);
      }

      if(DHT.humidity > hmax-5){
//      Serial.print("H off 1");
        digitalWrite(UMIDIFICATORE, LOW);
      }
      if(DHT.humidity < hmin+5){
//      Serial.print("H on 2");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
      if(DHT.humidity > hmin+5 && DHT.humidity < hmax-5){
//      Serial.print("H on 3");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
   // }

}

the lcd screen is refreshed continuously when the program is in the case 0 e in the case 3.
How can I resolve this problem?

When in these cases and you first write to the screen set a variable to true. Check the variable before writing to the screen again and if it is true only write the data if it has changed since last written, not the whole screen. When the case number changes set the variable to false so that the whole screen is updated next time the case occurs.

Ok, I have thought at this solution and I have wrote this code, I use a flag that assume as value 0 or 1. But in this case the program didn't change a submenu and it didn't go in the case 3.

#include <EEPROM.h>
#include <dht.h>
#include <LiquidCrystal.h>

dht DHT;

//Definisco tutti i PIN

#define UMIDIFICATORE  A4
#define RISCALDATORE   A5

#define NEXT           7
#define DOWN           8
#define UP             9
#define OK             10
#define DHT11_PIN      13
#define RETROILL       6

//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;  
//Variabili che uso per convertire le soglie da tipo double a float
float temp_max;
float temp_min;
float hum_max;
float hum_min;
//Variabile che uso per lo standby della retroilluminazione del display
int time = 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);
  pinMode(RETROILL, OUTPUT);
  digitalWrite(RETROILL, HIGH);
  //Pulisco e setto il display
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LOADING...");
  delay(2000); 
  
  //Inizializzo il sensore
  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;
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Home");
}

void loop(){

  int chk = DHT.read11(DHT11_PIN);
  int flag = 0;
  
  //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;
  }
  
  time++;
  if(time == 300){
    digitalWrite(RETROILL, LOW);
    }
  if(digitalRead(NEXT) == LOW || digitalRead(OK) == LOW || digitalRead(UP) == LOW || digitalRead(DOWN) == LOW ){ 
    if(digitalRead(RETROILL) == LOW){ 
      digitalWrite(RETROILL, HIGH); 
      posizione--; 
    } 
    time = 0; 
  }
    
  //Menù
  if(digitalRead(NEXT) == LOW){
    posizione++;
    flag = 1;
  }
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    tmax = EEPROM_readDouble(0);
    tmin = EEPROM_readDouble(4);
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    hmax = EEPROM_readDouble(8);
    hmin = EEPROM_readDouble(12);
    
    if ( posizione > 3 ) 
      posizione = 0;
    if ( posizione < 0 )
      posizione = 3; 

    switch(posizione){
    case 0:
      if(flag == 1){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[0]);
        flag = 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:
      if(flag == 1){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[1]);
        flag = 0;
        }
        
      //Imposto le soglie di temperature
      tmax = set_temp_max();
      tmin = set_temp_min();
      //Controllo soglie temperatura
      if(tmin > tmax){
        tmin = tmax - 4;
        lcd.setCursor(0,1);
        lcd.print(tmin);
      }
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(0, tmax);
      EEPROM_writeDouble(4, tmin);
      time = 0;
      delay(200);
      break;
    case 2:
      if(flag == 1){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[2]);
        flag = 0;
        }
        
      //Imposte le soglie di umidità
      hmax = set_hum_max();
      hmin = set_hum_min();
      //Controllo soglie umidità
      if(hmin > hmax){
        hmin = hmax - 10;
        lcd.setCursor(0,1);
        lcd.print(hmin);
      }
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(8, hmax);
      EEPROM_writeDouble(12, hmin);
      time = 0;
      delay(200);
      break;
    case 3:      
      if(flag == 1){
        lcd.clear();
        //Visualizzo le tutte le soglie impostate
        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 "); 
        flag = 0;
        }
      delay(200);   
      break;
    }  
    
    //Gestisco l'umidificatore e il riscaldatore in funzione delle soglie e dei valori letti
 //   while(posizione == -1){
      if(DHT.temperature > tmax-1){
//      Serial.println("T off 1");
        digitalWrite(RISCALDATORE, LOW);
      }
      if(DHT.temperature < tmin+1){
//      Serial.print("T on 2");
        digitalWrite(RISCALDATORE, HIGH);
      }
      if(DHT.temperature > tmin+1 && DHT.temperature < tmax-1){
//      Serial.print("T on 3");
        digitalWrite(RISCALDATORE, HIGH);
      }

      if(DHT.humidity > hmax-5){
//      Serial.print("H off 1");
        digitalWrite(UMIDIFICATORE, LOW);
      }
      if(DHT.humidity < hmin+5){
//      Serial.print("H on 2");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
      if(DHT.humidity > hmin+5 && DHT.humidity < hmax-5){
//      Serial.print("H on 3");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
   // }

}

The principle of what you have done looks OK but the problem will be in the detail. One thing I notice is that when you increment posizione you set flag = 1 to trigger the LCD display for the new state but you do not do it when you decrement posizione.

The purpose of the flag variable would be more obvious if it had a different name and was declared as a boolean such as
boolean screenAlreadyDisplayed = false;It certainly does not need to be an int which wastes space.

I have changed it, but I have the same problem. It doesn't go in the case 3, and it doesn't change the submenu to "Temperatura" to "Umidita".

#include <EEPROM.h>
#include <dht.h>
#include <LiquidCrystal.h>

dht DHT;

//Definisco tutti i PIN

#define UMIDIFICATORE  A4
#define RISCALDATORE   A5

#define NEXT           7
#define DOWN           8
#define UP             9
#define OK             10
#define DHT11_PIN      13
#define RETROILL       6

//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;  
//Variabili che uso per convertire le soglie da tipo double a float
float temp_max;
float temp_min;
float hum_max;
float hum_min;
//Variabile che uso per lo standby della retroilluminazione del display
int time = 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);
  pinMode(RETROILL, OUTPUT);
  digitalWrite(RETROILL, HIGH);
  //Pulisco e setto il display
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LOADING...");
  delay(2000); 
  
  //Inizializzo il sensore
  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;
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Home");
}

void loop(){

  int chk = DHT.read11(DHT11_PIN);
  boolean flag = false;
  
  //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;
  }
  
  time++;
  if(time == 300){
    digitalWrite(RETROILL, LOW);
    }
  if(digitalRead(NEXT) == LOW || digitalRead(OK) == LOW || digitalRead(UP) == LOW || digitalRead(DOWN) == LOW ){ 
    if(digitalRead(RETROILL) == LOW){ 
      digitalWrite(RETROILL, HIGH); 
      posizione--; 
    } 
    time = 0; 
  }
    
  //Menù
  if(digitalRead(NEXT) == LOW){
    posizione++;
    flag = true;
  }
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    tmax = EEPROM_readDouble(0);
    tmin = EEPROM_readDouble(4);
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    hmax = EEPROM_readDouble(8);
    hmin = EEPROM_readDouble(12);
    
    if ( posizione > 3 ) 
      posizione = 0;
    if ( posizione < 0 )
      posizione = 3; 

    switch(posizione){
    case 0:
      if(flag == true && posizione == 0){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[0]);
        flag = false;
        }
      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:
      if(flag == true){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[1]);
        flag = false;
        }
        
      //Imposto le soglie di temperature
      tmax = set_temp_max();
      tmin = set_temp_min();
      
      //Controllo soglie temperatura
      if(tmin > tmax){
        tmin = tmax - 4;
        lcd.setCursor(0,1);
        lcd.print(tmin);
      }
      
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(0, tmax);
      EEPROM_writeDouble(4, tmin);
      
      time = 0;
      
      delay(200);
      break;
      
    case 2:
      if(flag == true){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[2]);
        flag = false;
        }
        
      //Imposte le soglie di umidità
      hmax = set_hum_max();
      hmin = set_hum_min();
      
      //Controllo soglie umidità
      if(hmin > hmax){
        hmin = hmax - 10;
        lcd.setCursor(0,1);
        lcd.print(hmin);
      }
      
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(8, hmax);
      EEPROM_writeDouble(12, hmin);
      time = 0;
      delay(200);
      break;
    case 3:      
      if(flag == true){
        lcd.clear();
        //Visualizzo le tutte le soglie impostate
        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 "); 
        flag = false;
        }
      delay(200);   
      break;
    }  
    
    //Gestisco l'umidificatore e il riscaldatore in funzione delle soglie e dei valori letti
 //   while(posizione == -1){
      if(DHT.temperature > tmax-1){
//      Serial.println("T off 1");
        digitalWrite(RISCALDATORE, LOW);
      }
      if(DHT.temperature < tmin+1){
//      Serial.print("T on 2");
        digitalWrite(RISCALDATORE, HIGH);
      }
      if(DHT.temperature > tmin+1 && DHT.temperature < tmax-1){
//      Serial.print("T on 3");
        digitalWrite(RISCALDATORE, HIGH);
      }

      if(DHT.humidity > hmax-5){
//      Serial.print("H off 1");
        digitalWrite(UMIDIFICATORE, LOW);
      }
      if(DHT.humidity < hmin+5){
//      Serial.print("H on 2");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
      if(DHT.humidity > hmin+5 && DHT.humidity < hmax-5){
//      Serial.print("H on 3");
        digitalWrite(UMIDIFICATORE, HIGH);
      }
   // }

}
    switch(posizione){
    case 0:
      if(flag == true && posizione == 0){

The if statement can't possibly be executed in case 0 unless the value in posizione IS 0, so the condition is silly.

    if(digitalRead(RETROILL) == LOW){ 
      digitalWrite(RETROILL, HIGH); 
      posizione--; 
    }

It would be far better if you'd just remember the state you wrote to the pin.

It doesn't go in the case 3

You have no way of knowing that. "It doesn't get to case 3 WHEN flag is true" is a demonstrable fact. "It doesn't get to case 3" is not.

Put some Serial.print()s in your code so that you can follow its flow and the value of pertinent variables at that time. Good places would be before you use variables such as posizione before the switch that uses its value. Work out on paper what the values should be and what you get in practice.

The if statement can't possibly be executed in case 0 unless the value in posizione IS 0, so the condition is silly.

Ok I have deleted the condition "posizione == 0"

It would be far better if you'd just remember the state you wrote to the pin.

I have changed the code:

  time++;
  if(time == 300){
    digitalWrite(RETROILL, LOW);
    }
  if(digitalRead(NEXT) == LOW || digitalRead(OK) == LOW || digitalRead(UP) == LOW || digitalRead(DOWN) == LOW ){ 
    if(digitalRead(RETROILL) == LOW && digitalRead(NEXT) == LOW){ 
      digitalWrite(RETROILL, HIGH); 
      posizione--; 
    }
    else if(digitalRead(RETROILL) == LOW && (digitalRead(OK) == LOW || digitalRead(UP) == LOW || digitalRead(DOWN) == LOW)){
      digitalWrite(RETROILL, HIGH);  
    }
    time = 0; 
  }

You have no way of knowing that. "It doesn't get to case 3 WHEN flag is true" is a demonstrable fact. "It doesn't get to case 3" is not.

I don't understand, why it doesn't go in the case 3?

You are still reading from the OUTPUT pin. Create a variable to hold the state of that pin. Then, diddle with the value in the variable, and use the variable in the digitalWrite call and in place of all the digitalRead calls for that pin.

  if(digitalRead(NEXT) == LOW || digitalRead(OK) == LOW || digitalRead(UP) == LOW || digitalRead(DOWN) == LOW ){ 
    if(digitalRead(RETROILL) == LOW && digitalRead(NEXT) == LOW){

Why do you need to test whether the NEXT pin is low twice?
Why do you need to test whether the NEXT pin is low twice?

I don't understand, why it doesn't go in the case 3?

You don't know that it doesn't. All you know is that it doesn't go there WHILE flag is true.

Do as you were told. Give that variable a meaningful name. Add some Serial.print() statements to the code. What is in posizione when the switch statement is executed?