Schema termostato con max6675 e relay module

Salve a tutti,
ho un problema con questo sketch. Vorrei far spegnere il relay1, se la temperatura è maggiore di 30 gradi, ma non funziona.

#include <max6675.h> //temperatura

#define RELAY1 31         // IN1 Relay collegato al pin digitale 31  
#define RELAY2 33         // IN2 Relay collegato al pin digitale 33
#define BUTTON 52         // pin di input dove è collegato il pulsante  
#define BUTTON2 53        // pin di input dove è collegato il pulsante 2 
#define TMAX 30


int temp;

int ktcSO = 8;          //pin termocoppia
int ktcCS = 9;          //pin termocoppia
int ktcCLK = 10;        //pin termocoppia 
int  val = 0;                 // si userà val per conservare lo stato del pin di input  
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);

void setup() {  
 Serial.begin(9600);
pinMode(RELAY1, OUTPUT);      // imposta il pin digitale come output  
 pinMode(RELAY2, OUTPUT);      // imposta il pin digitale come output
 pinMode(BUTTON, INPUT);       // imposta il pin digitale come input  
 delay(50);
}  
 
void loop() { 
Serial.print("temperatura = "); 
  Serial.print(ktc.readCelsius());
delay(250);
  
temp=byte(ktc.readCelsius());


  
val = digitalRead(BUTTON);  // legge il valore dell'input e lo conserva  

 // controlla che l'input sia LOW (pulsante premuto)  
 if (val == LOW) { 
   digitalWrite(RELAY1, HIGH);  //accende il relay  
    }
 
else {  
   digitalWrite(RELAY1, LOW);   //spegne il relay 
 }
//per temperatura
if (val == HIGH && temp>TMAX) {
 digitalWrite(RELAY1,LOW);
}
 
 val = digitalRead(BUTTON2);
 if (val == LOW) {  
   digitalWrite(RELAY2, HIGH);  //accende il relay 
 }  
 else {  
   digitalWrite(RELAY2, LOW);   //spegne il relay 
 }  
 

}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Eljon31:

  Serial.print("temperatura = ");

Serial.print(ktc.readCelsius());
  delay(250);

temp = byte(ktc.readCelsius());

This is going to lead to confusion because the temp value the code is working with may not be the same as the what was printed to the Serial Monitor, plus it's inefficient. Do this instead:

  temp = byte(ktc.readCelsius());
  Serial.print("temperatura = ");
  Serial.print(temp);

Eljon31:

  delay(250);

This is going to cause the code to not be very responsive to button presses. Whether that's an issue depends on if you have a momentary or a maintained button.

Eljon31:

  if (val == LOW) {

digitalWrite(RELAY1, HIGH);  //accende il relay
  }

else {
    digitalWrite(RELAY1, LOW);  //spegne il relay
  }
  //per temperatura
  if (val == HIGH && temp > TMAX) {
    digitalWrite(RELAY1, LOW);
  }

The else statement will cause RELAY1 to be set LOW when val is HIGH no matter what the temperature is so the last three lines do nothing. You might be able to fix your problem by doing this:

 if (val == LOW) {
    digitalWrite(RELAY1, HIGH);  //accende il relay
  }

  else if (val == HIGH && temp > TMAX) {
    digitalWrite(RELAY1, LOW);
  }

If you're having trouble understanding my English then you might be interested to know that there's a forum section specifically for the Italian language:
http://forum.arduino.cc/index.php?board=34.0
EDIT: thread was moved to Italiano forum section

>Eljon31: Prima di tutto, nella sezione in lingua Inglese si può scrivere SOLO in Inglese ... quindi, per favore, la prossima volta presta più attenzione ...

... poi, essendo questo il tuo primo post, nel rispetto del regolamento, ti chiedo di presentarti QUI (spiegando bene quali conoscenze hai di elettronica e di programmazione) e di leggere con MOLTA attenzione il su citato REGOLAMENTO ...

... infine, in conformità al suddetto regolamento, punto 7, devi editare il tuo primo post (quindi NON scrivendo un nuovo post, ma utilizzando il bottone More -> Modify che si trova in basso a destra del tuo post) e racchiudere il codice all'interno dei tag CODE (... sono quelli che in edit inserisce il bottone con icona fatta così: </>, tutto a sinistra).

Grazie.

Guglielmo

P.S.: Il tuo post è stato spostato nell'opportuna sezione del forum "Italiano"

Ciao a tutti, ho effettuato le modifiche, ma il tasto si accende solo se va a temperatura...non va bene, invece:
1-il tasto accende il relè
2-il relè arriva ad una temperatura > 30° si deve spegnere

hai messo delle resistenze Pullup sul pulsante?

è un interruttore

Eljon31:
è un interruttore

... e allora facci lo schema esatto di come lo hai connesso (... perché, hai fini di ciò che chiede Uwe poco cambia).

Guglielmo

Questo è lo schema