#include <LiquidCrystal_I2C.h> /*include LCD I2C Library*/
#include <dmtimer.h> //Časovač
#include <thermistor.h> //Donwload it here: https://electronoobs.com/eng_arduino_thermistor.php
LiquidCrystal_I2C lcd(0x27, 16, 4); /*I2C scanned address defined + I2C screen size*/
thermistor therm1(A7, 0); //Connect thermistor on A7, 0 represents TEMP_SENSOR_0
const int ButtonPump = 2; // tlačítko start pumpy
const int ButtonHeat = 3; // tlačítko start topení
const int ButtonPowerHeat1 = 4; // Výkon Heat1
const int ButtonPowerHeat2 = A0; // Výkon Heat2
const int ButtonPowerHeat3 = 8; // Výkon Heat3
const int ButtonPowerHeat4 = 13; // Výkon Heat4
const int ButtonStart = 6; // tlačítko START AUTO
const int ButtonStop = 7; // tlačítko STOP
const int ControlValve = A2; // ovládání ventilu
const int ControlHeat1 = 10; // ovládání topení
const int ControlHeat2 = 11; // ovládání topení
const int ControlHeat3 = 5; // ovládání topení
const int ControlHeat4 = 9; // ovládání topení
const int SensorVac = A6; // senzor Vakua
const int ControlPump = A1; // ovládání pumpy
const int ControlBeep = A3; // pípák
const int InputMold = 12; // optočidlo
// A4 a A5 - I2C sběrnice
// A7 - teplotní čidlo
// Každé tlačítko má status
int ButtonPumpState = 0;
int ButtonHeatState = 0;
int ButtonStartState = 0;
int ButtonStopState = 0;
int ButtonPowerHeat1State = 0;
int ButtonPowerHeat2State = 0;
int ButtonPowerHeat3State = 0;
int ButtonPowerHeat4State = 0;
int BeepMillis = 0;
int InputMoldState = 0; //???
// každé tlačítko má Last Button State
int ButtonPumpStateLBS = 1;
int ButtonHeatStateLBS = 1;
int ButtonStartStateLBS = 1;
int ButtonStopStateLBS = 1;
int InputMoldStateLBS = 1;
int ButtonPowerHeat1StateLBS = 1;
int ButtonPowerHeat2StateLBS = 1;
int ButtonPowerHeat3StateLBS = 1;
int ButtonPowerHeat4StateLBS = 1;
int ButtonPumpState2 = 1; // pro každé tlačítko
int ButtonHeatState2 = 1;
int ButtonStartState2 = 1;
int ButtonStopState2 = 1;
int InputMoldState2 = 1; //????
int ButtonPowerHeat1State2 = 1;
int ButtonPowerHeat2State2 = 1;
int ButtonPowerHeat3State2 = 1;
int ButtonPowerHeat4State2 = 1;
int BeepState = 0;
unsigned long LastDebounceTime = 0; // the last time the output pin was toggled
unsigned long DebounceDelay = 50; // the debounce time; increase if the output flickers
int VacValueKpa;
int SensorVacValue;
int VacValueMinLimit = 75;
int VacValueMaxLimit = 85;
int VacHystState = 0;
int BeepShort = 2500;
int BeepLong = 5000;
int BeepLenght = 0;
int PumpState = 0;
int HeatState = 0; // global
int HeatState1 = 0;
int HeatState2 = 0;
int HeatState3 = 0;
int HeatState4 = 0;
int ValveState = 0;
int MoldState = 0;
int AutoState = 0;
int VacReadyState = 0;
int HeatReadyState = 0;
int MoldReadyState = 0;
int MoldStateRun = 0;
int VacTimer = 100; // jak dlouho po začátku ještě jede vývěva, 100 je cca. 10 vteřit
int VacTimerRun = 0;
int TempReadyLimit = 320; // pri plném výkonu je cca. 340 - 350°
unsigned long CurrentMillis = 0;
unsigned long CurrentMillis1 = 0;
unsigned long StartMillis = 0;
unsigned long StartMillis1 = 0;
unsigned long CurrentMillis2 = 0;
unsigned long StartMillis2 = 0;
unsigned long CurrentMillis3 = 0;
unsigned long StartMillis3 = 0;
unsigned long CurrentMillis4 = 0;
unsigned long StartMillis4 = 0;
int HeatPower1 = 0;
int HeatPower2 = 0;
int HeatPower3 = 0;
int HeatPower4 = 0;
int HeatPowerONState = 1000;
double Temp1 = 0;
byte OFF[8] = { 0b11111, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111, 0b00000 };
byte ON[8] = { 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b00000 };
byte AUT[8] = { 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b00000 };
byte kPa[8] = { 0b10000, 0b10100, 0b11000, 0b10100, 0b11100, 0b10111, 0b11101, 0b10111 };
void setup() {
digitalWrite(ControlValve, 1);
digitalWrite(ControlPump, 1);
pinMode(ButtonPump, INPUT_PULLUP); // tlačítko start VAC
pinMode(ButtonHeat, INPUT_PULLUP); // tlačítko start TEMP
pinMode(ButtonPowerHeat1, INPUT_PULLUP); // tlačítko nastavení výkonu Heat1
pinMode(ButtonStart, INPUT_PULLUP); // tlačítko ovládání Start
pinMode(ButtonStop, INPUT_PULLUP); // tlačítko ovládání Stop
pinMode(ButtonPowerHeat2, INPUT_PULLUP); // tlačítko nastavení výkonu Heat2
pinMode(ButtonPowerHeat3, INPUT_PULLUP); // tlačítko nastavení výkonu Heat3
pinMode(ButtonPowerHeat4, INPUT_PULLUP); // tlačítko nastavení výkonu Heat4
pinMode(InputMold, INPUT); // senzor začátku
pinMode(ControlValve, OUTPUT); // ovládání ventilu
pinMode(ControlHeat1, OUTPUT); // ovládání topení
pinMode(ControlHeat2, OUTPUT); // ovládání topení
pinMode(ControlHeat3, OUTPUT); // ovládání topení
pinMode(ControlHeat4, OUTPUT); // ovládání topení
pinMode(ControlPump, OUTPUT); // ovládání pumpy
pinMode(ControlBeep, OUTPUT); // beeeeeeeeep
lcd.init(); /*LCD display initialized*/
lcd.clear(); /*Clear LCD Display*/
lcd.backlight(); /*Turn ON LCD Backlight*/
lcd.createChar(1, ON);
lcd.createChar(0, OFF);
lcd.createChar(3, kPa);
VacTimerRun = VacTimer;
StartMillis = millis();
StartMillis1 = millis();
StartMillis2 = millis();
StartMillis3 = millis();
StartMillis4 = millis();
Refresh_CONTROL();
Refresh_LCD();
Serial.begin(9600);
}
void loop() {
// čtení tlačítek
//---------------------------------------------------- PUMP
int ButtonPumpState = digitalRead(ButtonPump);
if (ButtonPumpState != ButtonPumpStateLBS) {
LastDebounceTime = millis();
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonPumpState != ButtonPumpState2) {
ButtonPumpState2 = ButtonPumpState;
if (ButtonPumpState2 == 0) {
if (PumpState == 0) {
PumpState = 1;
} else {
PumpState = 0;
}
}
}
}
ButtonPumpStateLBS = ButtonPumpState;
//---------------------------------------------------- HEAT
int ButtonHeatState = digitalRead(ButtonHeat);
if (ButtonHeatState != ButtonHeatStateLBS) {
LastDebounceTime = millis();
Serial.println("XX");
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonHeatState != ButtonHeatState2) {
ButtonHeatState2 = ButtonHeatState;
if (ButtonHeatState2 == 0) {
if (HeatState == 0) {
HeatState = 1;
} else {
HeatState = 0;
HeatState1 = 0;
HeatState2 = 0;
HeatState3 = 0;
HeatState4 = 0;
}
}
}
}
ButtonHeatStateLBS = ButtonHeatState;
//---------------------------------------------------- PowerHeat1
int ButtonPowerHeat1State = digitalRead(ButtonPowerHeat1);
if (ButtonPowerHeat1State != ButtonPowerHeat1StateLBS) {
LastDebounceTime = millis();
Serial.println("XX");
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonPowerHeat1State != ButtonPowerHeat1State2) {
ButtonPowerHeat1State2 = ButtonPowerHeat1State;
if (ButtonPowerHeat1State2 == 0) {
Serial.println("XX-XX");
HeatPower1 = HeatPower1 + 1000;
if (HeatPower1 > 5000) {
HeatPower1 = 0;
}
}
}
}
ButtonPowerHeat1StateLBS = ButtonPowerHeat1State;
//---------------------------------------------------- PowerHeat2
int ButtonPowerHeat2State = digitalRead(ButtonPowerHeat2);
if (ButtonPowerHeat2State != ButtonPowerHeat2StateLBS) {
LastDebounceTime = millis();
Serial.println("XX");
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonPowerHeat2State != ButtonPowerHeat2State2) {
ButtonPowerHeat2State2 = ButtonPowerHeat2State;
if (ButtonPowerHeat2State2 == 0) {
Serial.println("XX-XX");
HeatPower2 = HeatPower2 + 1000;
if (HeatPower2 > 5000) {
HeatPower2 = 0;
}
}
}
}
ButtonPowerHeat2StateLBS = ButtonPowerHeat2State;
//---------------------------------------------------- PowerHeat3
int ButtonPowerHeat3State = digitalRead(ButtonPowerHeat3);
if (ButtonPowerHeat3State != ButtonPowerHeat3StateLBS) {
LastDebounceTime = millis();
Serial.println("XX");
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonPowerHeat3State != ButtonPowerHeat3State2) {
ButtonPowerHeat3State2 = ButtonPowerHeat3State;
if (ButtonPowerHeat3State2 == 0) {
Serial.println("XX-XX");
HeatPower3 = HeatPower3 + 1000;
if (HeatPower3 > 5000) {
HeatPower3 = 0;
}
}
}
}
ButtonPowerHeat3StateLBS = ButtonPowerHeat3State;
//---------------------------------------------------- PowerHeat4
int ButtonPowerHeat4State = digitalRead(ButtonPowerHeat4);
if (ButtonPowerHeat4State != ButtonPowerHeat4StateLBS) {
LastDebounceTime = millis();
Serial.println("XX");
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonPowerHeat4State != ButtonPowerHeat4State2) {
ButtonPowerHeat4State2 = ButtonPowerHeat4State;
if (ButtonPowerHeat4State2 == 0) {
Serial.println("XX-XX");
HeatPower4 = HeatPower4 + 1000;
if (HeatPower4 > 5000) {
HeatPower4 = 0;
}
}
}
}
ButtonPowerHeat4StateLBS = ButtonPowerHeat4State;
//---------------------------------------------------- CIDLO
MoldState = digitalRead(InputMold); // čidlo začátku
//---------------------------------------------------- AUTO
int ButtonStartState = digitalRead(ButtonStart);
if (ButtonStartState != ButtonStartStateLBS) {
LastDebounceTime = millis();
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonStartState != ButtonStartState2) {
ButtonStartState2 = ButtonStartState;
if (ButtonStartState2 == 0) {
if (AutoState == 0) {
AutoState = 1;
PumpState = 0;
HeatState = 0;
HeatState1 = 0;
HeatState2 = 0;
HeatState3 = 0;
HeatState4 = 0;
ValveState = 0;
VacReadyState = 0;
HeatReadyState = 0;
MoldReadyState = 0;
VacHystState = 0;
}
}
}
}
ButtonStartStateLBS = ButtonStartState;
//---------------------------------------------------- STOP
int ButtonStopState = digitalRead(ButtonStop);
if (ButtonStopState != ButtonStopStateLBS) {
LastDebounceTime = millis();
}
if ((millis() - LastDebounceTime) > DebounceDelay) {
if (ButtonStopState != ButtonStopState2) {
ButtonStopState2 = ButtonStopState;
if (ButtonStopState2 == 0) {
if (AutoState == 0) {
PumpState = 0;
HeatState = 0;
HeatState1 = 0;
HeatState2 = 0;
HeatState3 = 0;
HeatState4 = 0;
ValveState = 0;
VacHystState = 0;
VacReadyState = 0;
HeatReadyState = 0;
MoldReadyState = 0;
VacTimerRun = 0;
lcd.clear();
} else {
AutoState = 0;
PumpState = 0;
HeatState = 0;
HeatState1 = 0;
HeatState2 = 0;
HeatState3 = 0;
HeatState4 = 0;
ValveState = 0;
VacHystState = 0;
VacReadyState = 0;
HeatReadyState = 0;
MoldReadyState = 0;
VacTimerRun = 0;
lcd.clear();
}
}
}
}
ButtonStopStateLBS = ButtonStopState;
//---------------------------------------------------- CTENI HODNOT
// Snímání podtlaku v zásobníku
SensorVacValue = analogRead(SensorVac);
VacValueKpa = map(SensorVacValue, 200, 1020, 90, 0); // zmerit skutečné hodnoty !!
// Snímání teploty topení
Temp1 = therm1.analog2temp(); // read temperature
// -------------------------------------------- Režim AUTOMAT / MANUAL
if (AutoState == 1) {
//--------------- VAC
if (VacValueKpa < VacValueMaxLimit && VacHystState == 0) { // min 50, max 75
PumpState = 1;
VacHystState = 0;
VacReadyState = 0;
} else if (VacValueKpa >= VacValueMaxLimit && VacHystState == 0) {
PumpState = 0;
VacHystState = 1;
VacReadyState = 1;
}
if (VacValueKpa < VacValueMinLimit && VacHystState == 1) {
PumpState = 1;
VacHystState = 0;
VacReadyState = 0;
MoldReadyState = 0;
}
// --------------- HEAT
if (VacReadyState == 1 && HeatReadyState == 0) {
HeatState = 1; // topení zapnuté
}
if (VacReadyState == 1) {
if (Temp1 <= TempReadyLimit && MoldStateRun == 0) {
HeatState = 1; // topení zapnuté
HeatReadyState = 0;
MoldReadyState = 0;
}
if (Temp1 > TempReadyLimit && MoldReadyState == 0) {
HeatReadyState = 1; // preheat status - 0 - off, 1 - ready
MoldReadyState = 1;
if (BeepState == 0) {
BeepMillis = millis();
BeepLenght = BeepShort;
}
}
if (MoldReadyState == 1 && HeatReadyState == 1) {
MoldReadyState = 2;
VacTimerRun = VacTimer;
}
}
// Lisování
if (MoldReadyState == 2 && MoldState == 1) {
MoldStateRun = 1;
PumpState = 1;
ValveState = 1;
HeatState = 0;
HeatState1 = 0;
HeatState2 = 0;
HeatState3 = 0;
HeatState4 = 0;
}
if (MoldStateRun == 1) {
VacTimerRun = --VacTimerRun;
if (VacTimerRun == 0) {
PumpState = 0;
HeatState = 0;
HeatState1 = 0;
HeatState2 = 0;
HeatState3 = 0;
HeatState4 = 0;
ValveState = 0;
AutoState = 0;
VacReadyState = 0;
HeatReadyState = 0;
MoldReadyState = 0;
VacHystState = 0;
MoldStateRun = 0;
}
}
}
Refresh_LCD();
Refresh_CONTROL();
Refresh_BEEP();
//serial print test
// testovací seriový monitor - nutno odkomentovat taky serial.begin
Serial.print("HEAT : ");
Serial.print(HeatState1);
Serial.print(" X ");
Serial.print(HeatState2);
Serial.print(" X ");
Serial.print(HeatState3);
Serial.print(" X ");
Serial.println(HeatState4);
}
void Refresh_LCD() {
lcd.setCursor(0, 0);
lcd.print("V:");
lcd.setCursor(0, 1);
lcd.print("H:");
lcd.setCursor(-4, 2); // bylo -4
lcd.print("S:"); // pak se zruší
lcd.setCursor(4, 0);
lcd.print("B:");
lcd.setCursor(4, 1);
lcd.print("t:");
lcd.setCursor(0, 2); // bylo 5
lcd.print("M:"); // pak se zruší
lcd.setCursor(10, 0);
lcd.print("Z");
lcd.setCursor(10, 1);
lcd.print("S");
lcd.setCursor(6, 2); // bylo 5
lcd.print("S");
lcd.setCursor(6, 3); // bylo 5
lcd.print("P");
// pozice Pump
lcd.setCursor(2, 0);
lcd.write(byte(PumpState)); // 1 - ON, 0 - OFF
// pozice Heat
lcd.setCursor(2, 1);
lcd.write(byte(HeatState)); // 1 - ON, 0 - OFF
// pozice Temp
lcd.setCursor(6, 1); // bylo 0
lcd.print(String(Temp1, 0));
// Pozice hodnota podtlaku
lcd.setCursor(6, 0); // bylo 11
lcd.print(VacValueKpa);
// lcd.write(byte(3));
lcd.print(" ");
// pozice S - ovládací ventil
lcd.setCursor(-2, 2);
lcd.write(byte(ValveState));
// pozice M - senzor lisování
lcd.setCursor(2, 2); // bylo 8
//lcd.write(byte(MoldState)); // 1 - ON, 0 - OFF
lcd.write(byte(MoldState)); // 1 - ON, 0 - OFF
// pozice nastavení topení
lcd.setCursor(12, 0);
lcd.print(HeatPower1);
lcd.setCursor(12, 1);
lcd.print(HeatPower2);
lcd.setCursor(8, 2); // bylo 5
lcd.print(HeatPower3);
lcd.setCursor(8, 3); // bylo 5
lcd.print(HeatPower4);
if (AutoState == 0) {
lcd.setCursor(-4, 3); // bylo -3
lcd.print("MANU ");
} else if (AutoState == 1) {
lcd.setCursor(-4, 3); // bylo -3
lcd.print("AUTO ");
}
if (MoldReadyState == 2) {
lcd.setCursor(-4, 3);
lcd.print("MOLD ");
}
// STATUSY a timer
/*
lcd.setCursor(16, 0);
lcd.print(VacReadyState);
//lcd.print(HeatState);
lcd.setCursor(16, 1);
lcd.print(HeatReadyState);
lcd.setCursor(16, 2);
lcd.print(MoldReadyState);
*/
lcd.setCursor(1, 3);
if (VacTimerRun > 99) {
lcd.print(VacTimerRun);
} else {
lcd.print(" ");
if (VacTimerRun >= 10) {
lcd.setCursor(2, 3);
lcd.print(VacTimerRun);
} else {
if (VacTimerRun >= 1) {
lcd.setCursor(3, 3);
lcd.print(VacTimerRun);
} else {
if (VacTimerRun <= 0) {
lcd.setCursor(1, 3);
lcd.print("OK ");
}
}
}
}
}
void Refresh_CONTROL() {
if (HeatState == 1) {
CurrentMillis1 = millis();
if ((CurrentMillis1 - StartMillis1) >= HeatPower1)
{
HeatState1 = 1;
if ((CurrentMillis1 - StartMillis1) >= (HeatPower1 + HeatPowerONState))
{
HeatState1 = 0;
StartMillis1 = CurrentMillis1;
}
}
CurrentMillis2 = millis();
if ((CurrentMillis2 - StartMillis2) >= HeatPower2)
{
HeatState2 = 1;
if ((CurrentMillis2 - StartMillis2) >= (HeatPower2 + HeatPowerONState))
{
HeatState2 = 0;
StartMillis2 = CurrentMillis2;
}
}
CurrentMillis3 = millis();
if ((CurrentMillis3 - StartMillis3) >= HeatPower3)
{
HeatState3 = 1;
if ((CurrentMillis3 - StartMillis3) >= (HeatPower3 + HeatPowerONState))
{
HeatState3 = 0;
StartMillis3 = CurrentMillis3;
}
}
CurrentMillis4 = millis();
if ((CurrentMillis4 - StartMillis4) >= HeatPower4)
{
HeatState4 = 1;
if ((CurrentMillis4 - StartMillis4) >= (HeatPower4 + HeatPowerONState))
{
HeatState4 = 0;
StartMillis4 = CurrentMillis4;
}
}
}
/*
if (HeatState == 1) {
HeatState1 = 1;
HeatState2 = 1;
HeatState3 = 1;
HeatState4 = 1;
} else {
HeatState1 = 0;
HeatState2 = 0;
HeatState3 = 0;
HeatState4 = 0;
}
*/
digitalWrite(ControlPump, !PumpState);
digitalWrite(ControlHeat1, HeatState1);
digitalWrite(ControlHeat2, HeatState2);
digitalWrite(ControlHeat3, HeatState3);
digitalWrite(ControlHeat4, HeatState4);
digitalWrite(ControlValve, !ValveState);
}
void Refresh_BEEP() {
if (millis() - BeepMillis < BeepLenght ) {
digitalWrite(ControlBeep, HIGH);
BeepState = 1;
} else {
digitalWrite(ControlBeep, LOW);
BeepState = 0;
}
}
VAC - - HEAT
START - - STOP
VALVE - - SET
UP - - DOWN