//Define library custom
#include "PIN.h"
#include "SETTING.h"
// define library standard
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variable declaration
unsigned long Time0;
bool StopPressed =false, appo=false;
byte phase=0, ErrorCode;
void setup() {
Serial.begin(11520);
lcd.init();
//Pin Mode Input
pinMode(STOP, INPUT);
pinMode(Return_PC1, INPUT);
pinMode(Return_PC2, INPUT);
//Pin Mode Output
pinMode(ShutDownPC1, OUTPUT);
pinMode(ShutDownPC2, OUTPUT);
pinMode(SelfRestrained, OUTPUT);
digitalWrite(SelfRestrained , HIGH);
// Turn on the blacklight and print a message.
lcd.backlight();
}
void loop() {
switch (phase){
case 0:
phase++;
break;
case 1:
phase++;
break;
case 2:
phase++;
break;
case 3:
phase=0;
break;
case 10:
Shutdown();
break;
case 11:
ErrorState();
break;
}
ShutdownRequestControl();
}
////////////////////////////////////////////
///// Shut down Request Control /////////
////////////////////////////////////////////
void ShutdownRequestControl(){
if ( (digitalRead(STOP)) || StopPressed ){
if (!StopPressed){
StopPressed=true;
Time0=millis();
}
if ( (digitalRead(STOP)) && ((millis() - Time0)>(ButtonPressTimeLimit *1000)) ){
phase = 10;
delay(50);
}
else if (!digitalRead(STOP)){
StopPressed=false;
}
}
}
////////////////////////////////////////////
//////////// Shut down ///////////////
////////////////////////////////////////////
void Shutdown(){
if (!appo){
Time0=millis();
Serial.println("solo una volta");
appo=true;
}
digitalWrite(ShutDownPC1, HIGH);
digitalWrite(ShutDownPC2, HIGH);
if ( (digitalRead(Return_PC1) && NumberPc==1 ) || ( digitalRead(Return_PC1) && digitalRead(Return_PC2) && NumberPc==2) ) {
digitalWrite(SelfRestrained, LOW);
digitalWrite(ShutDownPC1, LOW);
digitalWrite(ShutDownPC2, LOW);
phase=0;
Serial.println("spento");
}
lcd.setCursor(0, 0);
lcd.print("SPEGNIMENTO");
lcd.setCursor(13, 0);
lcd.print((millis()-Time0)/1000);
if ((millis()-Time0)>(ShutdownTimeout*1000)){
phase=11;
}
}
////////////////////////////////////////////
//////////// Error Status ///////////////
////////////////////////////////////////////
void ErrorState(){
lcd.setCursor(0, 0);
lcd.print(" ERRORE IN ");
lcd.setCursor(0, 1);
lcd.print(" SPEGNIMENTO");
}