#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20 , 4);
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long currentMillis = 0;
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
const int buttonEscPin = 3; // the number of the pushbutton pin
const int buttonEnterPin = 2; // the number of the pushbutton pin
const byte ledPin = 13;
int escFlag = 0;
int enterFlag = 0;
int startProgramFlag = 0;
int stepSoudeFlag = 0;
int runFlag = 0;
// Here a structure to describe a valve
struct kValve {
char name; //enum to describe which valve
bool isOpen; // if valve is realy open or not
bool toBeOpen; // if valvle has to be opened by a script
};
struct kProgram {
char name; // name/type, such as "keg washing" or "fermentor washing"
bool isRunning; // flag set to "true" if programm in progress
bool toBeRan; // flag set to "true" if program is selected and has to be ran
};
struct kStep {
char name; // the name/type of step, such as "caustic washing"
char nature; // name of product in it, such as "caustic"
int duration; // duration of this step
int start;
};
struct kValve vanneIn {'A', false, false};
struct kValve vanneOut {'B', false, false};
bool vanneInFlag = false;
bool vanneOutFlag = false;
bool vanneSoudeInFlag = false;
bool vanneSoudeOutFlag = false;
enum programme {
programme1,
programme2,
programme3
};
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50;
void setup()
{
runFlag = 1;
lcd.init();
Serial.begin(9600);
lcd.begin(20, 4);
pinMode(ledPin, OUTPUT);
pinMode(buttonEscPin, INPUT_PULLUP);
pinMode(buttonEnterPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonEscPin), myEsc, FALLING);
attachInterrupt(digitalPinToInterrupt(buttonEnterPin), myEnter, FALLING);
}
int myTimer(int previous, int interval) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
return 1;
}
return 0;
}
void displayTime(int time){
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(time / 1000);
}
void displayFlag(){
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
if (escFlag) {
lcd.print("ESC");
escFlag = 0;
}
if (enterFlag) {
lcd.print("ENTER");
enterFlag = 0;
}
}
void myEsc(){
escFlag = 1;
}
void myEnter(){
enterFlag = 1;
}
void runProgram() {
// par exemple :
// on ouvre les vannes de soude et on place le circuit correctement
// on pompe 1min
// on arrete la pompe
// on ferme la vanne de soude et on ferme la boucle du circuit
// on fait tourner 1min
// on stop la pompe
// on ouvre la vanne de retour de soude, et on rouvre le circuit correctement
// on fait tourner 1min
// on arrete la pompe
// ici test : on affiche un timer pendant 5 secondes puis on sort
if (myTimer(previousMillis, 5000)) {
Serial.print("run");
if (myTimer(previousMillis, interval)) {
// if the LED is off turn it on and vice-versa:
displayTime(currentMillis);
}
else {
displayError();
}
}
else {
runFlag = 0;
}
}
void displayError() {
lcd.setCursor(0, 0);
lcd.print("error");
}
void displayMenu() {
lcd.setCursor(0, 0);
lcd.print("Menu");
}
void loop() {
currentMillis = millis();
// Check state :
// on regarde si RUN est setté
// soit RUN a une valeur et on est dans un programme
// soit RUN est a 0 et on affiche le menu
if(runFlag){
runProgram();
}
else
{
displayMenu();
}
// if (digitalRead(buttonDownPin)) {
// downFlag = 1;
// displayFlag();
// }
// if (digitalRead(buttonUpPin)) {
// upFlag = 1;
// displayFlag();
// }
if (escFlag || enterFlag) {
displayFlag();
}
}