#define btnIn 2
#define relOut 3
unsigned int helixSetCall = 10; // how many calls are needed to run comp
unsigned int helixCount = 0; // CountValue
unsigned int waitHelix = 5; // wait for further Helix after reach min HelixCount
unsigned int burnOutWaitTme = 5; // wait for Burn Out to start Comp
unsigned int compRunTme = 5; // comp Run Tme /sec
unsigned long compNowRunTme = 0; // Now Comp Run Time
unsigned long compNowWaitHelix = 0; // Now wait for further Helix after reach HelixCount
unsigned long compNowBurnOutTme = 0; // Now Comp Run Time
byte compSwL = 0; // Compressor LevelSwitch
bool btnState = LOW;
//---------------------------------------------
byte compSwLold;
unsigned int helixCountold;
//---------------------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(btnIn, INPUT_PULLUP);
pinMode(relOut, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
showSerial();
comp();
}
//------------------Funktion Comp-------------------
//--------------------------------------------------
void comp(){
if (compSwL == 0){
if (btnState == LOW && digitalRead(btnIn) == LOW){
btnState = HIGH;
helixCount ++;
}
if (btnState == HIGH && digitalRead(btnIn)== HIGH){
btnState = LOW;
}
if (helixCount > helixSetCall){ //min Helix Calls reached
// digitalWrite(relOut, HIGH);
compSwL = 1;
helixCount = 0;
}
}
if (compSwL == 1){ //set Now for further Helix Calls
compNowWaitHelix = millis();
compSwL = 2;
}
if (compSwL == 2){
if (digitalRead(btnIn) == LOW){ // if more Calls set NOw back
compSwL = 1;
}
if (millis() - compNowWaitHelix >= (waitHelix * 1000)) { // if no more Calls, start BurnOut Wait Time
compSwL = 3;
compNowBurnOutTme = millis();
//digitalWrite(relOut, HIGH);
}
}
if (compSwL == 3){ // BurnOut Wait
if (millis() - compNowBurnOutTme >= (burnOutWaitTme * 1000)) { // if no more Calls, start BurnOut Wait Time
compSwL = 4;
compNowRunTme = millis();
//digitalWrite(relOut, HIGH);
}
}
if (compSwL == 4){ // Start Comp
digitalWrite(relOut, HIGH);
if (millis() - compNowRunTme >= (compRunTme * 1000)) { // Run Comp x sek
compSwL = 5;
}
}
if (compSwL == 5){ // stopp comp and expanse
compSwL = 0;
digitalWrite(relOut, LOW);
}
}
// --------------Serial----------------
void showSerial(){
if( compSwL != compSwLold or
helixCount != helixCountold){
Serial.print("helixCount: ");
Serial.println(helixCount);
Serial.print("compSwL: ");
Serial.println(compSwL);
compSwLold = compSwL;
helixCountold = helixCount;
}
}