//VARIABILI DI APPOGGIO
//int start = 0; // abilita la accensione del regolatore
//int rot = 0; // status rotazione rot=0 orario rot=1 antoorario
//byte tbreak = 1; // tempo di frenatura
//int stoptime =0;
int buttonstatea = 0;
int valbuttonold = 0;
int memon = 0;
int ledonblink = 0;
int ledstate = 0;
int drytime = 0;
int rotchange = 0;
int rotchange1 = 0;
int rotor = 0;
int changedirection = 0;
int changedirectioncommand = 0;
int rotan = 0;
int changedirection1 = 0;
int safetyrotor = 0;
int safetyrotoron = 0;
int safetyrotanon = 0;
int safetyrotan = 0;
int rot1 = 0;
int timetostop = 0;
int timeprog = 0;
int coolingdown = 0;
int timetostop1 = 0;
int dryfinish = 0;
int dryon = 0;
int temp = 0;
int dryfinishdelaytime = 0;
int cooldown = 0;
int endprogram = 0;
int dooropen = 0;
int coolingdowntime = 10;
int memdelay = 0;
int buttonstated = 0;
int valbuttondold = 0;
int delaytime = 0;
int delay1 = 10; //1h-3600
int delay2 = 20; //2h-7200
int delay3 = 30; //3h-10800
int delay4 = 40; //4h-14400
int leddelaystate = 0;
int delaycount = 0;
int delayon = 0;
// PROGRAMMI / TEMPERATURE
// 1800 30min
int ara = 0;
int aratime = 1800;
// 2400 40min
int ala = 0;
int alatime = 2400;
// 600 10min
int ars = 0;
int arstime = 600;
// 600 20 min
int als = 0;
int alstime = 1200;
//
int minheat = 0;
int maxheat = 0;
int mintemp = 200;
int maxtemp = 400;
int worktemp = 0;
///////////MILLIS//////////////
// DEBUG
unsigned long previousMillis1 = 0; // memorizza l'ultimo stato
unsigned long interval1 = 1000; //intervallo di lettura
// MONITOR SERIALE
unsigned long previousMillis2 = 0;
unsigned long interval2 = 1000;
// TASTO AVVIO
unsigned long previousMillis3 = 0;
unsigned long interval3 = 1000;
// LEDONBLINK
unsigned long previousMillis4 = 0;
unsigned long interval4 = 700;
// TEMPO PROGRAMMI
unsigned long previousMillis5 = 0;
unsigned long interval5 = 1000;
// PARTENZA RITARDATA
unsigned long previousMillis6 = 0;
unsigned long interval6 = 1000;
// LAMPEGGIO LED PARTENZA RITARDATA
unsigned long previousMillis7 = 0;
unsigned long interval7 = 300;
// GESTIONE RITARDO AVVIO
unsigned long previousMillis8 = 0;
unsigned long interval8 = 1000;
// RAFFREDDAMENTO
unsigned long previousMillis9 = 0;
unsigned long interval9 = 1000;
// LETTURA TC
unsigned long previousMillis10 = 0;
unsigned long interval10 = 1000;
// ATTESA RAFFREDDAMENTO
unsigned long previousMillis11 = 0;
unsigned long interval11 = 1000;
//INGRESSI
int in2 = 0;
int in3 = 0;
int in4 = 0;
//2-3-4 non definiti, selettore programmi
#define swa 5
#define ledfinish 6
#define leddry 7
#define swdelay 8
#define errorcond 9
#define errorfilter 10
#define leddelay 11
#define leddelay1 12
#define ledon 13
#define rotoron A0
#define rotanon A1
#define heat500 A2
#define heat1000 A3
#define door A4
#define tc A5
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
pinMode(A5, INPUT);
//
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
//
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
}
void loop() {
// SICUREZZE
dooropen = digitalRead(A4);
if (memon == 0) {
digitalWrite(rotoron, 0);
digitalWrite(rotanon, 0);
digitalWrite(heat500, 0);
digitalWrite(heat1000, 0);
}
/////////////////////////////////////////////////// SELEZIONE PROGRAMMA
in2 = digitalRead(2);
in3 = digitalRead(3);
in4 = digitalRead(4);
///////////////////////////////////////////////////MONITOR SERIALE
if (millis() - previousMillis2 > interval2) {
previousMillis2 = millis();
Serial.println("-------");
//INGRESSI SELETTORE PROGRAMMI
//Serial.print("IN2: ");
//Serial.println(in2, DEC);
//Serial.print("IN3: ");
//Serial.println(in3, DEC);
//Serial.print("IN4: ");
//Serial.println(in4, DEC);
//Serial.println("------");
//Serial.print("Avvio/pausa");
//Serial.println(memon, DEC);
//Serial.println("------");
//Serial.println (rotchange1, DEC);
//Serial.print(ara, DEC);
//Serial.println("ara");
//Serial.print(ala, DEC);
//Serial.println("ala");
//Serial.print(ars, DEC);
//Serial.println("ars");
//Serial.print(als, DEC);
//Serial.println("als");
Serial.print("Tempo per asciugatura: ");
Serial.println(timeprog, DEC);
//Serial.print("TC: ");
//Serial.println(temp, DEC);
//Serial.println(memdelay, DEC);
}
if (millis() - previousMillis10 > interval10) {
previousMillis10 = millis();
temp = analogRead(tc);
}
//////////////////////////////////////////////////////LETTURA TASTO AVVIO CICLO
buttonstatea = digitalRead(swa);
if (buttonstatea == 0 && valbuttonold == 0 && dooropen == 0) {
if (millis() - previousMillis3 > interval3) {
previousMillis3 = millis();
memon ^= 1; // Inverte lo stato (da ON a OFF, da OFF a ON)
digitalWrite(ledfinish, 0);
valbuttonold = buttonstatea;
}
}
///////////////////////////////////////////////////////TASTO PARTENZA RITARDATA
buttonstated = digitalRead(swdelay);
if (buttonstated == 0 && valbuttondold == 0) {
if (millis() - previousMillis6 > interval6) {
previousMillis6 = millis();
memdelay++; // Inverte lo stato (da ON a OFF, da OFF a ON)
valbuttondold = buttonstated;
}
}
if (memdelay == 1) {
delaytime = delay1;
digitalWrite(leddelay, 1);
digitalWrite(leddelay1, 0);
}
if (memdelay == 2) {
digitalWrite(leddelay1, 0);
delaytime = delay2;
if (millis() - previousMillis7 > interval7) {
previousMillis7 = millis();
if (leddelaystate == LOW) {
leddelaystate = HIGH;
} else {
leddelaystate = LOW;
}
digitalWrite(leddelay, leddelaystate);
}
}
if (memdelay == 3) {
delaytime = delay3;
digitalWrite(leddelay, 0);
digitalWrite(leddelay1, 1);
}
if (memdelay == 4) {
delaytime = delay4;
digitalWrite(leddelay, 0);
if (millis() - previousMillis7 > interval7) {
previousMillis7 = millis();
if (leddelaystate == LOW) {
leddelaystate = HIGH;
} else {
leddelaystate = LOW;
}
digitalWrite(leddelay1, leddelaystate);
}
}
if (memdelay == 5) {
memdelay = 0;
}
//GESTIONE AVVIO RITARDATO
if (memdelay > 0 && memon == 1) {
delayon = 1;
}
if (memdelay == 0 && memon == 1) {
dryon = 1;
}
if (((memdelay == 1 || memdelay == 2 || memdelay == 3 || memdelay == 4) && delaycount < delaytime) && delayon == 1) {
if (millis() - previousMillis8 > interval8) {
previousMillis8 = millis();
delaycount++;
Serial.print("Ritardo impostato: ");
Serial.print(delaytime, DEC);
Serial.print(" Ritardo avvio: ");
Serial.println(delaycount, DEC);
}
if (delaycount == delaytime) {
dryon = 1;
memdelay = 0;
digitalWrite(leddelay, 0);
digitalWrite(leddelay1, 0);
delayon = 0;
}
}
//////////////////////////////////////////////////////SELEZIONE PROGRAMMA
if (in2 == 0 & in3 == 1 & in4 == 1) {
ledonblink = 1;
ara = 1;
ala = 0;
ars = 0;
als = 0;
maxheat = 1;
minheat = 0;
}
if (in2 == 0 & in3 == 0 & in4 == 1) {
ledonblink = 1;
ala = 1;
ara = 0;
ars = 0;
als = 0;
maxheat = 0;
minheat = 1;
}
if (in2 == 1 & in3 == 1 & in4 == 0) {
ledonblink = 1;
ars = 1;
als = 0;
ala = 0;
ara = 0;
maxheat = 1;
minheat = 0;
}
if (in2 == 0 & in3 == 1 & in4 == 0) {
ledonblink = 1;
als = 1;
ars = 0;
ala = 0;
ara = 0;
maxheat = 0;
minheat = 1;
}
/////////////////////////////////////////////////////////LEDONBLINK
if (ledonblink == 1 && memon == 0 && dooropen == 0) {
if (millis() - previousMillis4 > interval4) {
previousMillis4 = millis();
if (ledstate == LOW) {
ledstate = HIGH;
} else {
ledstate = LOW;
}
digitalWrite(ledon, ledstate);
}
} else {
if (memon == 1) {
digitalWrite(ledon, HIGH);
}
}
/////////////////////////////////////////////////////////SETTAGGIO PARAMETRI ASCIUGATURA
if (ara == 1) {
timeprog = aratime;
}
if (ala == 1) {
timeprog = alatime;
}
if (ars == 1) {
timeprog = arstime;
}
if (als == 1) {
timeprog = alstime;
}
////////////////////////////////////////////////////////AVVIO ASCIUGATURE
if (dryon == 1 && cooldown == 0) {
if (millis() - previousMillis5 > interval5) {
previousMillis5 = millis();
if (dooropen == 0 && memon == 1) {
drytime++;
}
Serial.print("Tempo ciclo: ");
Serial.println(drytime, DEC);
digitalWrite(leddry, 1);
motorcicle();
heating();
}
if (drytime == timeprog) {
timetostop = 1;
}
if (timetostop == 1 && safetyrotanon == 0 && safetyrotoron == 0) {
dryfinish = 1;
rotor = 0;
rotan = 0;
safetyrotoron = 0;
safetyrotanon = 0;
rotchange = 0;
rotchange1 = 0;
changedirection = 0;
changedirection1 = 0;
previousMillis9 = 0;
safetyrotor = 0;
changedirectioncommand = 0;
rot1 = 0;
safetyrotor = 0;
safetyrotan = 0;
dryon = 0;
}
}
if (dryfinish == 1 && dryfinishdelaytime < 2) {
if (millis() - previousMillis11 > interval11) {
previousMillis11 = millis();
dryfinishdelaytime++;
}
digitalWrite(heat500, 0);
digitalWrite(heat1000, 0);
}
if (dryfinishdelaytime == 2) {
cooldown = 1;
}
//RAFFREDDAMENTO
if (memon == 1 && cooldown == 1) {
if (millis() - previousMillis9 > interval9) {
previousMillis9 = millis();
coolingdown++;
motorcicle();
Serial.println("Cooling down");
}
if (coolingdown == coolingdowntime) {
timetostop1 = 1;
}
if (timetostop1 == 1 && safetyrotanon == 0 && safetyrotoron == 0) {
endprogram = 1;
}
}
if (endprogram == 1) {
Serial.println("FINE CICLO");
digitalWrite(rotoron, 0);
digitalWrite(rotanon, 0);
digitalWrite(leddry, 0);
digitalWrite(ledfinish, 1);
digitalWrite(heat500, 0);
digitalWrite(heat1000, 0);
drytime = 0;
safetyrotor = 0;
safetyrotan = 0;
safetyrotanon = 0;
safetyrotoron = 0;
timetostop1 = 0;
timetostop = 0;
rotor = 0;
rotan = 0;
rot1 = 0;
cooldown = 0;
coolingdown = 0;
dryon = 0;
dryfinish = 0;
dryfinishdelaytime = 0;
changedirectioncommand = 0;
changedirection1 = 0;
changedirection = 0;
memon = 0;
previousMillis9 = 0;
rotchange = 0;
rotchange1 = 0;
delaycount = 0;
previousMillis8 = 0;
delaytime = 0;
previousMillis7 = 0;
previousMillis6 = 0;
memdelay = 0;
endprogram = 0;
}
//GRAFFA LOOP
}
//------------------------------------------------------------------RICHIAMI FUNZIONI----------------------------------------------------------------------------------------------
///////////////////////////////////////////////////////MOTORE
void motorcicle() {
//ROTAZIONE ORARIA
if ((dryon == 1 || cooldown == 1) && rotor < 300 && safetyrotor == 0 && safetyrotanon == 0 && dooropen == 0 && memon == 1) {
rotor++;
Serial.println("Rot.Oraria");
if (dooropen == 0 && memon == 1) {
digitalWrite(rotoron, 1);
} else {
digitalWrite(rotoron, 0);
}
safetyrotoron = 1;
rotchange = 0;
rotchange1 = 0;
if (rotor == 300) {
digitalWrite(rotoron, 0);
changedirection = 1;
safetyrotor = 1;
}
} else {
digitalWrite(rotoron, 0);
}
//ROTAZIONE ANTIORARIA
if ((dryon == 1 || cooldown == 1) && changedirectioncommand == 1 && rotan < 15 && safetyrotoron == 0 && safetyrotan == 0 && dooropen == 0 && memon == 1) {
if (dooropen == 0 && memon == 1) {
digitalWrite(rotanon, 1);
} else {
digitalWrite(rotanon, 0);
}
rotan++;
safetyrotanon = 1;
Serial.println("Rot.Antioraria");
if (rotan == 15) {
digitalWrite(rotanon, 0);
changedirection1 = 1;
changedirectioncommand = 0;
safetyrotan == 1;
}
} else {
digitalWrite(rotanon, 0);
}
//FERMO CESTELLO PER CAMBIO ROTAZIONE
if ((changedirection == 1 || changedirection1 == 1) && ((rotchange <= 3 && rotor == 300 && rot1 == 0) || (rotchange1 <= 3 && rotan == 15))) {
Serial.println("Fermo cestello");
if (rotor == 300) {
rotchange++;
}
if (rotan == 15) {
rotchange1++;
}
if (rotchange == 3 && rotor == 300 && rot1 == 0) {
changedirectioncommand = 1;
safetyrotoron = 0;
//Serial.println("prima rotazione");
rot1 = 1;
}
if (rotchange1 == 3 && rotan == 15) {
//Serial.println("seconda rotazione");
rotor = 0;
rotan = 0;
rot1 = 0;
changedirectioncommand = 0;
safetyrotor = 0;
safetyrotan = 0;
safetyrotanon = 0;
changedirection1 = 0;
changedirection = 0;
}
}
}
/////////////////////////////////////////////////////////////////RESISTENZA
void heating() {
temp = analogRead(tc);
if (maxheat == 1 && dooropen == 0 && memon == 1 && (safetyrotoron == 1 || safetyrotanon == 1)) {
if (temp < maxtemp) {
digitalWrite(heat500, 0);
digitalWrite(heat1000, 1);
} else {
digitalWrite(heat500, 0);
digitalWrite(heat1000, 0);
}
} else {
digitalWrite(heat500, 0);
digitalWrite(heat1000, 0);
}
if (minheat == 1 && dooropen == 0 && memon == 1 && (safetyrotoron == 1 || safetyrotanon == 1)) {
if (temp < mintemp) {
digitalWrite(heat1000, 0);
digitalWrite(heat500, 1);
} else {
digitalWrite(heat500, 0);
digitalWrite(heat1000, 0);
}
} else {
if (maxheat == 0) {
digitalWrite(heat500, 0);
digitalWrite(heat1000, 0);
}
}
}