//--------LIBRERIE-------------------------
#include <LiquidCrystal_I2C.h> //lcd
#include <Keypad.h>
#include <EEPROM.h>
//-----------------------------------------
#define pompa 10
#define valvola 11
//----------------LCD----------------------
LiquidCrystal_I2C lcd(0x27,20,4);
//-----------------------------------------
//-----Tastierino------------
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//------------------------MENU-------------------------
//-------------------------------------------------------
//------------TMP-------------
const float BETA = 3950;
int val = 0;
int sensore = A0;
int temp = 0;
//-----------------------------
int stato = 0;
bool x = 0;
bool o = 0;
bool i = 0;
bool a = 0;
bool time = 0;
bool seconda_cifra = 0;
int margine = 0;
int MARGINE = 0;
//--------------------------
int mem_EEPROM = 0;
int LCDRow = 0;
int unita = 0;
int decine = 0;
int num_temp = 0, num_temp2 = 0;
int filtro = 48;
int conferma = 0;
int c = 67;
bool pressione_decine = 0;
bool pressione_unita = 0;
int cifra_1 = 0;
int temp_impostata;
bool mod =0;
int posizione = 0;
//--------------------------
void setup()
{
//----------EEPROM------------
int v_val = EEPROM.read(0);
//----------------------------
//----------LCD---------------
lcd.init();
//----------------------------
//----------TMPSENS-----------
pinMode(sensore, INPUT);
Serial.begin(9600);
//----------------------------
pinMode(pompa, OUTPUT);
pinMode(valvola, OUTPUT);
}
//--------------------------
//--------------------------
int key = 0;
void loop()
{
Serial.print("stato");
Serial.println(stato);
switch(stato){
case 0:
showSpalshScreen();
lcd.clear();
if(x == 1) stato = 1;
break;
case 1:
leggo_temp();
LCD();
menu();
break;
case 2:
lcd.clear();
stato = 3;
break;
case 3:
writing();
break;
case 4:
error();
stato =5;
break;
case 5:
cifra_1 = 0;
num_temp = 0;
num_temp2 = 0;
unita = 0;
pressione_unita = 0;
pressione_decine = 0;
decine = 0;
LCDRow = 0;
conferma = 0;
lcd.clear();
stato = 3;
break;
case 6:
Serial.print("temp impos");
Serial.println(temp_impostata);
confi();
menu();
break;
case 7:
leggo_temp();
LCD();
menu();
attuatori();
if(stato == 7 )posizione = 1;
Serial.print(temp_impostata);
break;
case 8:
cifra_1 = 0;
num_temp = 0;
num_temp2 = 0;
unita = 0;
pressione_unita = 0;
pressione_decine = 0;
decine = 0;
LCDRow = 0;
conferma = 0;
lcd.clear();
digitalWrite(pompa, LOW);
digitalWrite(valvola, LOW);
stato = 1;
break;
case 9:
LCD_att();
break;
}
}
void attuatori(){
Serial.print("mod");
Serial.println(mod);
Serial.print("MARGINE");
Serial.println(MARGINE);
MARGINE = temp_impostata + margine;
if(mod == 1 ){
if(temp_impostata < temp){
Serial.print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
lcd.setCursor(14,1);
lcd.print('*');
}
if(mod == 1 && (temp < temp_impostata)){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Change Mod or");
lcd.setCursor(0,1);
lcd.print("Temperature");
delay(1000);
lcd.clear();
stato = 9;
}
}
if(mod == 0){
if(temp <= temp_impostata){
digitalWrite(pompa, HIGH);
digitalWrite(valvola, HIGH);
Serial.print("aaaaaaaaaaaaaaaaaaaaa");
}
if(mod == 0 && (temp > temp_impostata)){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Reggime Raggiunto");
lcd.setCursor(0,1);
lcd.print("Back To Home...");
delay(1000);
lcd.clear();
stato = 8;
}
}
}
void leggo_temp(){
val = analogRead(sensore);
temp = 1 / (log(1 / (1023. / val - 1)) / BETA + 1.0 / 298.15) - 273.15;
if(temp >= -9 ){
lcd.setCursor(14,0);
lcd.print(" ");
}
if(temp < 9){
if(temp >= 0){
lcd.setCursor(13,0);
lcd.print(" ");
}
}
}
void LCD_att(){
char key = keypad.getKey();
if(key == 'A') {
stato = 8;
}
lcd.setCursor(0,0);
lcd.print("(A) MOD");
lcd.setCursor(0,1);
lcd.print("(B) Temperature");
if(key == 'B') {
lcd.setCursor(0,1);
lcd.print(" ");
stato = 5;
}
}
void LCD(){
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(12, 0);
lcd.print(temp);
lcd.setCursor(15, 0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Mod:");
lcd.setCursor(4,1);
if(mod == 0){
lcd.print("Inverno ");
}
else{
lcd.print("Estate ");
lcd.setCursor(15, 1);
lcd.print(" ");
}
}
void menu(){
int conf = 0;
char key = keypad.getKey();
if(key == 'A' && posizione == 0) stato = 2;
if(key == 'A' && posizione == 1) stato = 5;
if(key == 'D') conf = 2;
if(key == 'B') stato = 7;
if(key == 'C' && stato != 1) conf = 1;
if(conf == 1)stato = 7;
if(conf == 2)stato = 5;
if(key == '*'){
mod = !mod;
}
}
void showSpalshScreen() {
lcd.setCursor(0,0);
lcd.print("Carotti Spa.");
lcd.setCursor(3, 1);
String message = "Loading...";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
x = 1;
}
void writing (){
lcd.setCursor(0,0);
lcd.print("Impostare Temp:");
char key = keypad.getKey();
if(key == c)conferma = 1;
if(pressione_decine == 1 && pressione_unita == 0 && conferma == 1){
unita = decine;
decine = 0;
}
mem_EEPROM = temp_impostata;
if(pressione_decine == 1 && pressione_unita == 1){
temp_impostata = decine + unita;
if(temp_impostata > 80 && conferma == 1)stato = 4;
if(temp_impostata <= 80 && conferma == 1){
conferma = 0;
stato = 6;
}
}
if (key){
cifra_1++;
if(cifra_1 == 1){
num_temp = key;
if(num_temp != 35){
if(num_temp != 42){
if(num_temp != 65){
if(num_temp != 66){
if(num_temp != 67){
if(num_temp != 68){
lcd.setCursor (LCDRow, 1);
lcd.print(key);
decine=(num_temp-filtro)*10;
pressione_decine = 1;
lcd.setCursor (++LCDRow, 1);
seconda_cifra = 1;
}
}
}
}
}
}
}
if(cifra_1 == 2){
if(LCDRow == 1){
num_temp2 = key;
if(num_temp2 != 35){
if(num_temp2 != 42){
if(num_temp2 != 65){
if(num_temp2 != 66){
if(num_temp2 != 67){
if(num_temp2 != 68){
lcd.setCursor (LCDRow, 1);
lcd.print(key);
unita=(num_temp2-filtro);
pressione_unita = 1;
lcd.setCursor (LCDRow, 1);
Serial.print("aaaaaaaaaaaaaaa");
}
}
}
}
}
}
}
}
if(key == 'A'){
stato = 8;
}
if(key == 'D'){
num_temp = 0;
num_temp2 = 0;
unita = 0;
decine = 0;
pressione_unita = 0;
pressione_decine = 0;
cifra_1 = 0;
LCDRow = 0;
lcd.setCursor(0,1);
lcd.print(" ");
}
}
}
void confi(){
lcd.setCursor(0,0);
lcd.print("CONFERMI:");
lcd.setCursor(9,0);
if(temp_impostata < 10 ){
lcd.print(temp_impostata);
lcd.setCursor(10,0);
lcd.print(" ");
}else{
lcd.print(temp_impostata);
lcd.setCursor(11,0);
lcd.print(" ");
}
lcd.setCursor(0,1);
lcd.print("C SI");
lcd.setCursor(12,1);
lcd.print("D NO");
}
void error(){
lcd.clear();
lcd.setCursor(6,0);
lcd.print("ERROR");
lcd.setCursor(0,1);
lcd.print("Max Temp 80C");
delay(1000);
lcd.clear();
}
/*
void writing (){
lcd.setCursor(0,0);
lcd.print("Impostare Temp:");
char key = keypad.getKey();
lcd.setCursor(0,1);
lcd.print(key);
if(key > 80){
lcd.setCursor(0,1);
lcd.print("ERROR");
delay(1000);
lcd.print("MAX Value 80");
lcd.clear();
}
if(LCDRow == 2){
lcd.setCursor(1,0);
lcd.print(" ");
LCDRow = 0;
}
Serial.print("decine");
Serial.println(decine);
Serial.print("unita");
Serial.println(unita);
Serial.print("temperatututututututut");
Serial.println(temp_impostata);
if(LCDRow == 2){
num_temp2 = key;
unita=num_temp2-filtro;
pressione_unita = 1;
}
Serial.print("num");
Serial.println(num);
Serial.println(num_temp);
lcd.setCursor (++LCDRow, 1);
lcd.print(key);
}
Serial.print("LCDROW");
Serial.println(LCDRow);
Serial.print("decine");
Serial.println(decine);
Serial.print("unita");
Serial.println(unita);
Serial.print("risultato temp");
Serial.println(temp_impostata);
valore che deve ricordare EEPROM
int valore = 100;
EEPROM.write(0, valore);
*/