#include "max6675.h"
int termoCLK = 6;
int termoCS = 7;
int termoSO = 8;
MAX6675 thermocouple(termoCLK, termoSO, termoCS);
#define ENC_A 2
#define ENC_B 3
int clockPin; // Placeholder por pin status used by the rotary encoder
int clockPinState; // Placeholder por pin status used by the rotary encoder
int set_temperature = 1; // This set_temperature value will increas or decreas if when the rotarty encoder is turned
int max_temperature = 25;
float temperature_value_c = 0.0; // stores temperature value
#define LED_MIN 4
#define LED_MAX 5
#define OUT_RELAY 9
#define OUT_BUZZER 10
void setup() {
pinMode(LED_MIN, OUTPUT);
pinMode(LED_MAX, OUTPUT);
pinMode(OUT_RELAY, OUTPUT);
pinMode(OUT_BUZZER, OUTPUT);
// Set encoder pins and attach interrupts
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENC_A), read_encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENC_B), read_encoder, CHANGE);
Serial.begin(9600);
delay(1000);
digitalWrite(LED_MIN, LOW);
digitalWrite(LED_MAX, LOW);
}
void loop() {
int sTemp = thermocouple.readCelsius();
clockPin = digitalRead(ENC_A);
if (clockPin == 1 ) {
digitalWrite(LED_MAX, LOW);
digitalWrite(LED_MIN, HIGH);
}
if (sTemp == set_temperature){
digitalWrite(OUT_RELAY, LOW);
}else{
digitalWrite(OUT_RELAY, HIGH);
}
Serial.print("Set Temp:");
Serial.print(set_temperature );
Serial.println(" C");
Serial.print("Read Temp:");
Serial.print(sTemp );
Serial.println(" C");
delay(1000);
}
void read_encoder() // In this function we read the encoder data and increment the counter if it is rotating clockwise and decrement the counter if it's rotating counterclockwise
{
clockPin = digitalRead(ENC_A); // we read the clock pin of the rotary encoder
if (clockPin != clockPinState && clockPin == 1) { // if this condition is true then the encoder is rotaing counter clockwise and we decremetn the counter
if (digitalRead(ENC_B) != clockPin) set_temperature = set_temperature - 3; // decrmetn the counter.
else set_temperature = set_temperature + 3; // Encoder is rotating CW so increment
if (set_temperature <= 1 ) {
set_temperature = 1; // if the counter value is less than 1 the set it back to 1
digitalWrite(LED_MAX, LOW);
digitalWrite(LED_MIN, HIGH);
}
if (set_temperature > max_temperature ) {
set_temperature = max_temperature; //if the counter value is grater than 150 then set it back to 150
digitalWrite(LED_MIN, LOW);
digitalWrite(LED_MAX, HIGH);
}
}
clockPinState = clockPin; // Remember last CLK_PIN state
}