#define START 0
#define STOP 1
#define RELE 13
#define LED_R 12
#define LED 11
#define LED_LAMP A0
//-----------------
int T_LED, T_START, T_RELE, T_VALL; // variabili di tempo
int S_ORTO=0, I_POSZ=1 /*S_RELE*/;
int POMPE[8] = {2,3,4,5,6,7,8,9};
void setup()
{
DDRD = 0b11111100;
DDRB = 0b00011011;
pinMode(LED_LAMP,OUTPUT);
pinMode(START,INPUT_PULLUP);
pinMode(STOP,INPUT_PULLUP);
Serial.begin(9600);
Serial.print("inizio programma");
delayMicroseconds(1000);
}
void S_RELE()
{
Serial.print("tempo relè");
T_RELE++;
Serial.println(T_RELE);
if(T_RELE == 200)
{
V_RELE();
T_RELE=0;
}
}
void loop()
{
S_RELE();
delay(1000);
//----controllo ogni 2000ms-----
//-------------------------------
switch(S_ORTO)
{
case 0:
digitalWrite(LED,0);
digitalWrite(LED_R,0);
Serial.println("sono dentro macchina a stati");
//---conteggio premitura del pulsante
if(digitalRead(START) == 0)
{
Serial.print("tempo pulsante é ");
T_START++;
Serial.println(T_START);
}
else if(digitalRead(START) == 1 && T_START >= 3)//se tempo del pulsante = 300 si attiva il programma
{
S_ORTO = 1;
T_START = 0;
}
else if(digitalRead(START) == 1 && T_START < 3)T_START=0;
break;
case 1: //attiva le pompe una a una fino a giro compiuto
Serial.print("attiva pompa ");
Serial.println(I_POSZ);
digitalWrite(POMPE[I_POSZ],1);
T_VALL++;
if(T_VALL==15)
{
digitalWrite(POMPE[I_POSZ],0);
I_POSZ++;
T_VALL=0;
}
if(I_POSZ > 8) //fine programma
{
I_POSZ =0;
T_VALL =0;
S_ORTO =0;
T_START =0;
}
if(V_STOP() == 1) //blocco programma
{
I_POSZ =0;
T_VALL =0;
S_ORTO =0;
T_START =0;
}
if(V_RELE() == 1)//blocco programma
{
I_POSZ =0;
T_VALL =0;
S_ORTO =0;
T_START =0;
}
digitalWrite(LED,1);
break;
}
}
// se il relè é attivo blocca il programma
bool V_RELE()
{
if(RELE == 1) return 1;
else
{
digitalWrite(LED_R,1);
return 0;
}
}
//se viene premuto stop il programma si ferma
bool V_STOP()
{
if(STOP == 0)
{
return 1;
Serial.println("STOP");
}
else return 0;
}