From 70ac49072f1a24fce3d48581b386a8c45b476167 Mon Sep 17 00:00:00 2001 From: Makuro Date: Mon, 18 Dec 2023 21:51:24 +0100 Subject: [PATCH] Bugfix: Inonsistent Brew & Steam switch behaviours #413 #414 Debounce on Brew trigger. Steam trigger code duplication disabled debounce. Debounce times to 20ms. Added variable brewSwitchLongPress and set to 500ms. Rename BREWSWITCHDELAY to SHOTTIMERDISPLAYDELAY, adapt comments to fit to functionality. Rename brewSwitchLongPress -> brewTriggerLongPress and lastButtonStateBrew -> lastStateBrewTrigger clean brewvoid rename a lot of variables to naming scheme rename var steam and power trigger more rename variables --- src/EmbeddedWebserver.h | 2 +- src/MQTT.h | 6 +- src/brewscaleini.h | 55 -------- src/brewvoid.h | 303 ++++++++++++++++++++++------------------ src/display.h | 4 +- src/main.cpp | 137 +++++++----------- src/powerswitchvoid.h | 8 +- src/scalevoid.h | 6 +- src/steamswitchvoid.h | 25 +++- src/userConfig_sample.h | 2 +- 10 files changed, 253 insertions(+), 295 deletions(-) delete mode 100644 src/brewscaleini.h diff --git a/src/EmbeddedWebserver.h b/src/EmbeddedWebserver.h index c31e19fec..7ffe35b01 100644 --- a/src/EmbeddedWebserver.h +++ b/src/EmbeddedWebserver.h @@ -255,7 +255,7 @@ void serverSetup() { }); server.on("/toggleBackflush", HTTP_POST, [](AsyncWebServerRequest *request) { - int backflush = flipUintValue(backflushON); + int backflush = flipUintValue(backflushOn); setBackflush(backflush); debugPrintf("Toggle backflush mode: %i \n", backflush); diff --git a/src/MQTT.h b/src/MQTT.h index cba892a32..13cbef44a 100644 --- a/src/MQTT.h +++ b/src/MQTT.h @@ -65,7 +65,7 @@ String getMachineName(MACHINE machine) { * MQTT is also using maxWifiReconnects! */ void checkMQTT() { - if (offlineMode == 1 || brewcounter > kBrewIdle) return; + if (offlineMode == 1 || brewCounter > kBrewIdle) return; if ((millis() - lastMQTTConnectionAttempt >= wifiConnectionDelay) && (MQTTReCnctCount <= maxWifiReconnects)) { if (!mqtt.connected()) { @@ -451,7 +451,7 @@ int sendHASSIODiscoveryMsg() { // Switch Devices DiscoveryObject pidOn = GenerateSwitchDevice("pidON", "Use PID"); DiscoveryObject steamON = GenerateSwitchDevice("steamON", "Steam"); - DiscoveryObject backflushON = GenerateSwitchDevice("backflushON", "Backflush"); + DiscoveryObject backflushOn = GenerateSwitchDevice("backflushOn", "Backflush"); DiscoveryObject startUsePonM = GenerateSwitchDevice("startUsePonM", "Use PonM"); @@ -474,7 +474,7 @@ int sendHASSIODiscoveryMsg() { brewtime, pidOn, steamON, - backflushON, + backflushOn, startUsePonM }; diff --git a/src/brewscaleini.h b/src/brewscaleini.h deleted file mode 100644 index 13701450c..000000000 --- a/src/brewscaleini.h +++ /dev/null @@ -1,55 +0,0 @@ -/** - * @file brewscaleini.h - * - * @brief TODO - * - */ - -#pragma once -#include - - -enum BrewState { - kBrewIdle = 10, - kPreinfusion = 20, - kWaitPreinfusion = 21, - kPreinfusionPause = 30, - kWaitPreinfusionPause = 31, - kBrewRunning = 40, - kWaitBrew = 41, - kBrewFinished = 42, - kWaitBrewOff = 43 -}; - -// Normal Brew -BrewState brewcounter = kBrewIdle; -int brewswitch = 0; -int brewswitchTrigger = LOW; -int buttonStateBrewTrigger; // the current reading from the input pin -unsigned long lastDebounceTimeBrewTrigger = 0; // the last time the output pin was toggled -unsigned long debounceDelayBrewTrigger = 50; -unsigned long brewswitchTriggermillis = 0; -int brewswitchTriggerCase = 10; -boolean brewswitchWasOFF = false; -double totalBrewTime = 0; // total brewtime set in software -double timeBrewed = 0; // total brewed time -double lastbrewTimeMillis = 0; // for shottimer delay after disarmed button -double lastbrewTime = 0 ; -unsigned long startingTime = 0; // start time of brew -boolean brewPIDdisabled = false; // is PID disabled for delay after brew has started? -const unsigned long analogreadingtimeinterval = 10; // ms -unsigned long previousMillistempanalogreading; // ms for analogreading - -// Shot timer with or without scale -#if (ONLYPIDSCALE == 1 || BREWMODE == 2) - int shottimercounter = 10 ; - float calibrationValue = SCALE_CALIBRATION_FACTOR; // use calibration example to get value - float weight = 0; // value from HX711 - float weightPreBrew = 0; // value of scale before wrew started - float weightBrew = 0; // weight value of brew - float scaleDelayValue = 2.5; // value in gramm that takes still flows onto the scale after brew is stopped - bool scaleFailure = false; - const unsigned long intervalWeight = 200; // weight scale - unsigned long previousMillisScale; // initialisation at the end of init() - HX711_ADC LoadCell(PIN_HXDAT, PIN_HXCLK); -#endif diff --git a/src/brewvoid.h b/src/brewvoid.h index 30730e831..b85005870 100644 --- a/src/brewvoid.h +++ b/src/brewvoid.h @@ -7,93 +7,130 @@ #pragma once +#include + +enum BrewState { + kBrewIdle = 10, + kPreinfusion = 20, + kWaitPreinfusion = 21, + kPreinfusionPause = 30, + kWaitPreinfusionPause = 31, + kBrewRunning = 40, + kWaitBrew = 41, + kBrewFinished = 42, + kWaitBrewOff = 43 +}; + +// Normal Brew +BrewState brewCounter = kBrewIdle; +int brewSwitch = 0; +int brewSwitchTrigger = LOW; +int lastStateBrewTrigger; // the last valid reading from the input pin (debounced) +unsigned long lastDebounceTimeBrewTrigger = 0; // the last time the output pin was toggled +unsigned long debounceDelayBrewTrigger = 20; // >20ms when switches are "flicked" +unsigned long brewSwitchTriggerMillis = 0; +const unsigned long brewTriggerLongPress = 500; // time in ms until brew trigger will be interpreted as manual brewing +int brewSwitchTriggerCase = 10; +boolean brewSwitchWasOff = false; +double totalBrewTime = 0; // total brewtime set in software +double timeBrewed = 0; // total brewed time +double lastBrewTimeMillis = 0; // for shottimer delay after disarmed button +double lastBrewTime = 0 ; +unsigned long startingTime = 0; // start time of brew +boolean brewPIDDisabled = false; // is PID disabled for delay after brew has started? + +// Shot timer with or without scale +#if (ONLYPIDSCALE == 1 || BREWMODE == 2) + int shottimerCounter = 10 ; + float calibrationValue = SCALE_CALIBRATION_FACTOR; // use calibration example to get value + float weight = 0; // value from HX711 + float weightPreBrew = 0; // value of scale before wrew started + float weightBrew = 0; // weight value of brew + float scaleDelayValue = 2.5; // value in gramm that takes still flows onto the scale after brew is stopped + bool scaleFailure = false; + const unsigned long intervalWeight = 200; // weight scale + unsigned long previousMillisScale; // initialisation at the end of init() + HX711_ADC LoadCell(PIN_HXDAT, PIN_HXCLK); +#endif /** - * @brief Digtalswitch OR Read analog input pin for BREW SWITCH + * @brief Switch or trigger input for BREW SWITCH */ void checkbrewswitch() { #if BREWSWITCHTYPE == 1 - #if (PIN_BREWSWITCH > 0) - // Digital GIPO - brewswitch = digitalRead(PIN_BREWSWITCH); - #endif + // Digital + brewSwitch = digitalRead(PIN_BREWSWITCH); #endif #if BREWSWITCHTYPE == 2 - #if (PIN_BREWSWITCH > 0) int reading = digitalRead(PIN_BREWSWITCH); - if (reading != brewswitchTrigger) { - // reset the debouncing timer + if (reading != lastStateBrewTrigger) { + // restart the debouncing timer lastDebounceTimeBrewTrigger = millis(); + // set new button state } - - if ((millis() - lastDebounceTimeBrewTrigger) > debounceDelayBrewTrigger) { + else if ((millis() - lastDebounceTimeBrewTrigger) > debounceDelayBrewTrigger) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: - - // if the button state has changed: - if (reading != buttonStateBrewTrigger) { - buttonStateBrewTrigger = reading; + + if (brewSwitchTrigger != reading) { + brewSwitchTrigger = reading; } } - - brewswitchTrigger = reading; - #endif + lastStateBrewTrigger = reading; // Convert trigger signal to brew switch state - switch (brewswitchTriggerCase) { + switch (brewSwitchTriggerCase) { case 10: - if (brewswitchTrigger == HIGH) { - brewswitchTriggermillis = millis(); - brewswitchTriggerCase = 20; - debugPrintln("brewswitchTriggerCase 10: HIGH"); + if (brewSwitchTrigger == HIGH) { + brewSwitchTriggerMillis = millis(); + brewSwitchTriggerCase = 20; + debugPrintln("brewSwitchTriggerCase 10: HIGH"); } break; case 20: // Only one push, brew - if (brewswitchTrigger == LOW) { + if (brewSwitchTrigger == LOW) { // Brew trigger - brewswitch = HIGH; - brewswitchTriggerCase = 30; - debugPrintln("brewswitchTriggerCase 20: Brew Trigger HIGH"); + brewSwitch = HIGH; + brewSwitchTriggerCase = 30; + debugPrintln("brewSwitchTriggerCase 20: Brew Trigger HIGH"); } - // Button more than one 1sec pushed - if (brewswitchTrigger == HIGH && (brewswitchTriggermillis + 1000 <= millis())) { - debugPrintln("brewswitchTriggerCase 20: Manual Trigger - brewing"); - brewswitchTriggerCase = 31; - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayON); + // Button more than brewTriggerLongPress pushed + if (brewSwitchTrigger == HIGH && (brewSwitchTriggerMillis + brewTriggerLongPress <= millis())) { + debugPrintln("brewSwitchTriggerCase 20: Manual Trigger - flushing"); + brewSwitchTriggerCase = 31; + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOn); } break; + case 30: // Stop brew trigger (one push) brewswitch == HIGH - if ((brewswitchTrigger == HIGH && brewswitch == HIGH) || (machineState == kShotTimerAfterBrew) ) { - brewswitch = LOW; - brewswitchTriggerCase = 40; - brewswitchTriggermillis = millis(); - debugPrintln("brewswitchTriggerCase 30: Brew Trigger LOW"); + if ((brewSwitchTrigger == HIGH && brewSwitch == HIGH) || (machineState == kShotTimerAfterBrew) ) { + brewSwitch = LOW; + brewSwitchTriggerCase = 40; + debugPrintln("brewSwitchTriggerCase 30: Brew Trigger LOW"); } break; + case 31: // Stop Manual brewing, button goes low: - if (brewswitchTrigger == LOW && brewswitch == LOW) { - brewswitchTriggerCase = 40; - brewswitchTriggermillis = millis(); + if (brewSwitchTrigger == LOW && brewSwitch == LOW) { + brewSwitchTriggerCase = 40; debugPrintln("brewswitchTriggerCase 31: Manual Trigger - brewing stop"); - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); } break; case 40: - // wait 5 Sec until next brew, detection - if (brewswitchTriggermillis + 5000 <= millis()) { - brewswitchTriggerCase = 10; - debugPrintln("brewswitchTriggerCase 40: Brew Trigger Next Loop"); - } + // Go back to start and wait for brew button press + brewSwitchTriggerCase = 10; + debugPrintln("brewSwitchTriggerCase 40: Brew Trigger Next Loop"); break; case 50: @@ -107,9 +144,9 @@ void checkbrewswitch() { * @brief Backflush */ void backflush() { - if (backflushState != 10 && backflushON == 0) { - backflushState = 43; // Force reset in case backflushON is reset during backflush! - } else if (offlineMode == 1 || brewcounter > kBrewIdle || maxflushCycles <= 0 || backflushON == 0) { + if (backflushState != 10 && backflushOn == 0) { + backflushState = 43; // Force reset in case backflushOn is reset during backflush! + } else if (offlineMode == 1 || brewCounter > kBrewIdle || maxflushCycles <= 0 || backflushOn == 0) { return; } @@ -123,14 +160,14 @@ void backflush() { checkbrewswitch(); - if (brewswitch == LOW && backflushState > 10) { // Abort function for state machine from every state + if (brewSwitch == LOW && backflushState > 10) { // Abort function for state machine from every state backflushState = 43; } - // State machine for brew + // State machine for backflush switch (backflushState) { case 10: // waiting step for brew switch turning on - if (brewswitch == HIGH && backflushON) { + if (brewSwitch == HIGH && backflushOn) { startingTime = millis(); backflushState = 20; } @@ -139,8 +176,8 @@ void backflush() { case 20: // portafilter filling debugPrintln("portafilter filling"); - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayON); + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOn); backflushState = 21; break; @@ -155,8 +192,8 @@ void backflush() { case 30: // flushing debugPrintln("flushing"); - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); flushCycles++; backflushState = 31; @@ -173,10 +210,10 @@ void backflush() { break; case 43: // waiting for brewswitch off position - if (brewswitch == LOW) { + if (brewSwitch == LOW) { debugPrintln("backflush finished"); - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); flushCycles = 0; backflushState = 10; } @@ -185,44 +222,43 @@ void backflush() { } } - #if (BREWMODE == 1) // normal brew mode (based on time) /** * @brief PreInfusion, Brew Normal */ void brew() { if (OnlyPID == 0) { - unsigned long currentMillistemp = millis(); + unsigned long currentMillisTemp = millis(); checkbrewswitch(); - if (brewswitch == LOW && brewcounter > kBrewIdle) { + if (brewSwitch == LOW && brewCounter > kBrewIdle) { // abort function for state machine from every state debugPrintln("Brew stopped manually"); - brewcounter = kWaitBrewOff; + brewCounter = kWaitBrewOff; } - if (brewcounter > kBrewIdle && brewcounter < kWaitBrewOff) { - timeBrewed = currentMillistemp - startingTime; + if (brewCounter > kBrewIdle && brewCounter < kWaitBrewOff) { + timeBrewed = currentMillisTemp - startingTime; } - if (brewswitch == LOW && movingAverageInitialized) { + if (brewSwitch == LOW && movingAverageInitialized) { // check if brewswitch was turned off at least once, last time, - brewswitchWasOFF = true; + brewSwitchWasOff = true; } - totalBrewTime = (preinfusion * 1000) + (preinfusionpause * 1000) + - (brewtime * 1000); // running every cycle, in case changes are done during brew + totalBrewTime = (preinfusion * 1000) + (preinfusionPause * 1000) + + (brewTime * 1000); // running every cycle, in case changes are done during brew // state machine for brew - switch (brewcounter) { + switch (brewCounter) { case kBrewIdle: // waiting step for brew switch turning on - if (brewswitch == HIGH && backflushState == 10 && backflushON == 0 && brewswitchWasOFF) { + if (brewSwitch == HIGH && backflushState == 10 && backflushOn == 0 && brewSwitchWasOff) { startingTime = millis(); - if (preinfusionpause == 0 || preinfusion == 0) { - brewcounter = kBrewRunning; + if (preinfusionPause == 0 || preinfusion == 0) { + brewCounter = kBrewRunning; } else { - brewcounter = kPreinfusion; + brewCounter = kPreinfusion; } coldstart = false; // force reset coldstart if shot is pulled @@ -234,69 +270,69 @@ void brew() { case kPreinfusion: // preinfusioon debugPrintln("Preinfusion"); - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayON); - brewcounter = kWaitPreinfusion; + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOn); + brewCounter = kWaitPreinfusion; break; case kWaitPreinfusion: // waiting time preinfusion if (timeBrewed > (preinfusion * 1000)) { - brewcounter = kPreinfusionPause; + brewCounter = kPreinfusionPause; } break; case kPreinfusionPause: // preinfusion pause debugPrintln("Preinfusion pause"); - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayOFF); - brewcounter = kWaitPreinfusionPause; + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOff); + brewCounter = kWaitPreinfusionPause; break; case kWaitPreinfusionPause: // waiting time preinfusion pause - if (timeBrewed > ((preinfusion * 1000) + (preinfusionpause * 1000))) { - brewcounter = kBrewRunning; + if (timeBrewed > ((preinfusion * 1000) + (preinfusionPause * 1000))) { + brewCounter = kBrewRunning; } break; case kBrewRunning: // brew running debugPrintln("Brew started"); - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayON); - brewcounter = kWaitBrew; + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOn); + brewCounter = kWaitBrew; break; case kWaitBrew: // waiting time brew - lastbrewTime = timeBrewed; + lastBrewTime = timeBrewed; if (timeBrewed > totalBrewTime) { - brewcounter = kBrewFinished; + brewCounter = kBrewFinished; } break; case kBrewFinished: // brew finished debugPrintln("Brew stopped"); - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); - brewcounter = kWaitBrewOff; + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); + brewCounter = kWaitBrewOff; timeBrewed = 0; break; case kWaitBrewOff: // waiting for brewswitch off position - if (brewswitch == LOW) { - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); + if (brewSwitch == LOW) { + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); // disarmed button - currentMillistemp = 0; + currentMillisTemp = 0; brewDetected = 0; // rearm brewDetection - brewcounter = kBrewIdle; + brewCounter = kBrewIdle; timeBrewed = 0; } @@ -306,7 +342,6 @@ void brew() { } #endif - #if (BREWMODE == 2) /** * @brief Weight based brew mode @@ -314,35 +349,35 @@ void brew() { void brew() { if (OnlyPID == 0) { checkbrewswitch(); - unsigned long currentMillistemp = millis(); + unsigned long currentMillisTemp = millis(); - if (brewswitch == LOW && brewcounter > kBrewIdle) { + if (brewSwitch == LOW && brewCounter > kBrewIdle) { // abort function for state machine from every state - brewcounter = kWaitBrewOff; + brewCounter = kWaitBrewOff; } - if (brewcounter > kBrewIdle && brewcounter < kWaitBrewOff) { - timeBrewed = currentMillistemp - startingTime; + if (brewCounter > kBrewIdle && brewCounter < kWaitBrewOff) { + timeBrewed = currentMillisTemp - startingTime; weightBrew = weight - weightPreBrew; } - if (brewswitch == LOW && movingAverageInitialized) { + if (brewSwitch == LOW && movingAverageInitialized) { // check if brewswitch was turned off at least once, last time, - brewswitchWasOFF = true; + brewSwitchWasOff = true; } - totalBrewTime = ((preinfusion * 1000) + (preinfusionpause * 1000) + - (brewtime * 1000)); // running every cycle, in case changes are done during brew + totalBrewTime = ((preinfusion * 1000) + (preinfusionPause * 1000) + + (brewTime * 1000)); // running every cycle, in case changes are done during brew // state machine for brew - switch (brewcounter) { + switch (brewCounter) { case 10: // waiting step for brew switch turning on - if (brewswitch == HIGH && backflushState == 10 && backflushON == 0 && brewswitchWasOFF) { + if (brewSwitch == HIGH && backflushState == 10 && backflushOn == 0 && brewSwitchWasOff) { startingTime = millis(); - brewcounter = kPreinfusion; + brewCounter = kPreinfusion; - if (preinfusionpause == 0 || preinfusion == 0) { - brewcounter = kBrewRunning; + if (preinfusionPause == 0 || preinfusion == 0) { + brewCounter = kBrewRunning; } coldstart = false; // force reset coldstart if shot is pulled @@ -355,71 +390,71 @@ void brew() { case 20: // preinfusioon debugPrintln("Preinfusion"); - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayON); - brewcounter = kWaitPreinfusion; + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOn); + brewCounter = kWaitPreinfusion; break; case 21: // waiting time preinfusion if (timeBrewed > (preinfusion * 1000)) { - brewcounter = kPreinfusionPause; + brewCounter = kPreinfusionPause; } break; case 30: // preinfusion pause debugPrintln("preinfusion pause"); - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayOFF); - brewcounter = kWaitPreinfusionPause; + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOff); + brewCounter = kWaitPreinfusionPause; break; case 31: // waiting time preinfusion pause - if (timeBrewed > ((preinfusion * 1000) + (preinfusionpause * 1000))) { - brewcounter = kBrewRunning; + if (timeBrewed > ((preinfusion * 1000) + (preinfusionPause * 1000))) { + brewCounter = kBrewRunning; } break; case 40: // brew running debugPrintln("Brew started"); - digitalWrite(PIN_VALVE, relayON); - digitalWrite(PIN_PUMP, relayON); - brewcounter = kWaitBrew; + digitalWrite(PIN_VALVE, relayOn); + digitalWrite(PIN_PUMP, relayOn); + brewCounter = kWaitBrew; break; case 41: // waiting time brew if (timeBrewed > totalBrewTime || (weightBrew > (weightSetpoint - scaleDelayValue))) { - brewcounter = kBrewFinished; + brewCounter = kBrewFinished; } if (timeBrewed > totalBrewTime) { - brewcounter = kBrewFinished; + brewCounter = kBrewFinished; } break; case 42: // brew finished debugPrintln("Brew stopped"); - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); - brewcounter = kWaitBrewOff; + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); + brewCounter = kWaitBrewOff; break; case 43: // waiting for brewswitch off position - if (brewswitch == LOW) { - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); + if (brewSwitch == LOW) { + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); // disarmed button - currentMillistemp = 0; + currentMillisTemp = 0; timeBrewed = 0; brewDetected = 0; // rearm brewDetection - brewcounter = kBrewIdle; + brewCounter = kBrewIdle; } weightBrew = weight - weightPreBrew; // always calculate weight to show on display diff --git a/src/display.h b/src/display.h index e193cd442..296bcb260 100644 --- a/src/display.h +++ b/src/display.h @@ -106,7 +106,7 @@ void displayShottimer(void) { u8g2.drawXBMP(0, 0, brewlogo_width, brewlogo_height, brewlogo_bits_u8g2); u8g2.setFont(u8g2_font_profont22_tf); u8g2.setCursor(64, 25); - u8g2.print(lastbrewTime / 1000, 1); + u8g2.print(lastBrewTime / 1000, 1); u8g2.setFont(u8g2_font_profont11_tf); displayWaterIcon(119, 1); u8g2.sendBuffer(); @@ -135,7 +135,7 @@ void displayShottimer(void) { u8g2.drawXBMP(0, 0, brewlogo_width, brewlogo_height, brewlogo_bits_u8g2); u8g2.setFont(u8g2_font_profont22_tf); u8g2.setCursor(64, 15); - u8g2.print(lastbrewTime/1000, 1); + u8g2.print(lastBrewTime/1000, 1); u8g2.print("g"); u8g2.setCursor(64, 38); u8g2.print(weightBrew, 0); diff --git a/src/main.cpp b/src/main.cpp index 0bf3dee3d..3c4b9bd2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -182,9 +182,9 @@ double aggKp = AGGKP; double aggTn = AGGTN; double aggTv = AGGTV; double aggIMax = AGGIMAX; -double brewtime = BREW_TIME; // brewtime in s +double brewTime = BREW_TIME; // brewtime in s double preinfusion = PRE_INFUSION_TIME; // preinfusion time in s -double preinfusionpause = PRE_INFUSION_PAUSE_TIME; // preinfusion pause time in s +double preinfusionPause = PRE_INFUSION_PAUSE_TIME; // preinfusion pause time in s double weightSetpoint = SCALE_WEIGHTSETPOINT; // PID - values for offline brew detection @@ -225,12 +225,12 @@ SysPara sysParaBrewSetpoint(&brewSetpoint, BREW_SETPOINT_MIN, BREW_SETPO SysPara sysParaTempOffset(&brewTempOffset, BREW_TEMP_OFFSET_MIN, BREW_TEMP_OFFSET_MAX, STO_ITEM_BREW_TEMP_OFFSET); SysPara sysParaBrewPIDDelay(&brewPIDDelay, BREW_PID_DELAY_MIN, BREW_PID_DELAY_MAX, STO_ITEM_BREW_PID_DELAY); SysPara sysParaUseBDPID(&useBDPID, 0, 1, STO_ITEM_USE_BD_PID); -SysPara sysParaBrewTime(&brewtime, BREW_TIME_MIN, BREW_TIME_MAX, STO_ITEM_BREW_TIME); +SysPara sysParaBrewTime(&brewTime, BREW_TIME_MIN, BREW_TIME_MAX, STO_ITEM_BREW_TIME); SysPara sysParaBrewSwTime(&brewtimesoftware, BREW_SW_TIME_MIN, BREW_SW_TIME_MAX, STO_ITEM_BREW_SW_TIME); SysPara sysParaBrewThresh(&brewSensitivity, BD_THRESHOLD_MIN, BD_THRESHOLD_MAX, STO_ITEM_BD_THRESHOLD); SysPara sysParaWifiCredentialsSaved(&wifiCredentialsSaved, 0, 1, STO_ITEM_WIFI_CREDENTIALS_SAVED); SysPara sysParaPreInfTime(&preinfusion, PRE_INFUSION_TIME_MIN, PRE_INFUSION_TIME_MAX, STO_ITEM_PRE_INFUSION_TIME); -SysPara sysParaPreInfPause(&preinfusionpause, PRE_INFUSION_PAUSE_MIN, PRE_INFUSION_PAUSE_MAX, STO_ITEM_PRE_INFUSION_PAUSE); +SysPara sysParaPreInfPause(&preinfusionPause, PRE_INFUSION_PAUSE_MIN, PRE_INFUSION_PAUSE_MAX, STO_ITEM_PRE_INFUSION_PAUSE); SysPara sysParaPidKpSteam(&steamKp, PID_KP_STEAM_MIN, PID_KP_STEAM_MAX, STO_ITEM_PID_KP_STEAM); SysPara sysParaSteamSetpoint(&steamSetpoint, STEAM_SETPOINT_MIN, STEAM_SETPOINT_MAX, STO_ITEM_STEAM_SETPOINT); SysPara sysParaWeightSetpoint(&weightSetpoint, WEIGHTSETPOINT_MIN, WEIGHTSETPOINT_MAX, STO_ITEM_WEIGHTSETPOINT); @@ -238,7 +238,7 @@ SysPara sysParaStandbyModeOn(&standbyModeOn, 0, 1, STO_ITEM_STANDBY_MOD SysPara sysParaStandbyModeTime(&standbyModeTime, STANDBY_MODE_TIME_MIN, STANDBY_MODE_TIME_MAX, STO_ITEM_STANDBY_MODE_TIME); // Other variables -int relayON, relayOFF; // used for relay trigger type. Do not change! +int relayOn, relayOff; // used for relay trigger type. Do not change! boolean coldstart = true; // true = Rancilio started for first time boolean emergencyStop = false; // Emergency stop if temperature is too high double EmergencyStopTemp = 120; // Temp EmergencyStopTemp @@ -246,7 +246,7 @@ float inX = 0, inY = 0, inOld = 0, inSum = 0; // used for filterPressureValue() int signalBars = 0; // used for getSignalStrength() boolean brewDetected = 0; boolean setupDone = false; -int backflushON = 0; // 1 = backflush mode active +int backflushOn = 0; // 1 = backflush mode active int flushCycles = 0; // number of active flush cycles int backflushState = 10; // counter for state machine @@ -265,9 +265,6 @@ unsigned long timeBrewDetection = 0; int isBrewDetected = 0; // flag is set if brew was detected bool movingAverageInitialized = false; // flag set when average filter is initialized, also used for sensor check -// Brewing, 1 = Normal Preinfusion , 2 = Scale & Shottimer = 2 -#include "brewscaleini.h" - // Sensor check boolean sensorError = false; int error = 0; @@ -317,6 +314,8 @@ PID bPID(&temperature, &pidOutput, &setpoint, aggKp, aggKi, aggKd, 1, DIRECT); // TSIC 306 temp sensor ZACwire Sensor2(PIN_TEMPSENSOR, 306); // set pin to receive signal from the TSic 306 +#include "brewvoid.h" + // Embedded HTTP Server #include "EmbeddedWebserver.h" @@ -556,12 +555,12 @@ boolean checkSensor(float tempInput) { * If the value is not valid, new data is not stored. */ void refreshTemp() { - unsigned long currentMillistemp = millis(); + unsigned long currentMillisTemp = millis(); previousInput = temperature; if (TempSensor == 1) { - if (currentMillistemp - previousMillistemp >= intervaltempmesds18b20) { - previousMillistemp = currentMillistemp; + if (currentMillisTemp - previousMillistemp >= intervaltempmesds18b20) { + previousMillistemp = currentMillisTemp; #if TEMPSENSOR == 1 sensors.requestTemperatures(); @@ -587,8 +586,8 @@ void refreshTemp() { } if (TempSensor == 2) { - if (currentMillistemp - previousMillistemp >= intervaltempmestsic) { - previousMillistemp = currentMillistemp; + if (currentMillisTemp - previousMillistemp >= intervaltempmestsic) { + previousMillistemp = currentMillisTemp; #if TEMPSENSOR == 2 temperature = Sensor2.getTemp(15); @@ -614,7 +613,6 @@ void refreshTemp() { } -#include "brewvoid.h" #include "powerswitchvoid.h" #include "steamswitchvoid.h" #include "scalevoid.h" @@ -647,7 +645,7 @@ void initOfflineMode() { * @brief Check if Wifi is connected, if not reconnect abort function if offline, or brew is running */ void checkWifi() { - if (offlineMode == 1 || brewcounter > kBrewIdle) return; + if (offlineMode == 1 || brewCounter > kBrewIdle) return; /* if coldstart is still true when checkWifi() is called, then there was no WIFI connection * at boot -> connect and if it does not suceed, enter offlinemode @@ -754,7 +752,7 @@ void brewDetection() { // timeBrewed counter if ((digitalRead(PIN_BREWSWITCH) == optocouplerOn) && brewDetected == 1) { timeBrewed = millis() - startingTime; - lastbrewTime = timeBrewed; + lastBrewTime = timeBrewed; } // OFF: reset brew @@ -778,7 +776,7 @@ void brewDetection() { isBrewDetected = 1; } } else if (brewDetectionMode == 2) { // HW BD - if (brewcounter > kBrewIdle && brewDetected == 0) { + if (brewCounter > kBrewIdle && brewDetected == 0) { debugPrintln("HW Brew detected"); timeBrewDetection = millis(); isBrewDetected = 1; @@ -796,7 +794,7 @@ void brewDetection() { timeOptocouplerOn = millis(); isBrewDetected = 1; brewDetected = 0; - lastbrewTime = 0; + lastBrewTime = 0; brewSteamDetectedQM = 1; debugPrintln("Quick Mill: setting brewSteamDetectedQM = 1"); logbrew.reset(); @@ -839,7 +837,7 @@ void brewDetection() { startingTime = millis(); isBrewDetected = 1; brewDetected = 1; - lastbrewTime = 0; + lastBrewTime = 0; } } } @@ -860,39 +858,6 @@ float filterPressureValue(float input) { return inSum; } - -/** - * @brief steamON & Quickmill - */ -void checkSteamON() { - if (STEAMSWITCHTYPE == 0) { - return; - } - - // check digital GIPO - if (digitalRead(PIN_STEAMSWITCH) == HIGH) { - steamON = 1; - } - - // if activated via web interface then steamFirstON == 1, prevent override - if (digitalRead(PIN_STEAMSWITCH) == LOW && steamFirstON == 0) { - steamON = 0; - } - - // monitor QuickMill thermoblock steam-mode - if (machine == QuickMill) { - if (steamQM_active == true) { - if (checkSteamOffQM() == true) { // if true: steam-mode can be turned off - steamON = 0; - steamQM_active = false; - lastTimeOptocouplerOn = 0; - } else { - steamON = 1; - } - } - } -} - void setEmergencyStopTemp() { if (machineState == kSteam || machineState == kCoolDown) { if (EmergencyStopTemp != 145) EmergencyStopTemp = 145; @@ -994,7 +959,7 @@ void handleMachineState() { } if ((timeBrewed > 0 && ONLYPID == 1) || // timeBrewed with Only PID - (ONLYPID == 0 && brewcounter > kBrewIdle && brewcounter <= kBrewFinished)) + (ONLYPID == 0 && brewCounter > kBrewIdle && brewCounter <= kBrewFinished)) { machineState = kBrew; @@ -1011,7 +976,7 @@ void handleMachineState() { } } - if (backflushON || backflushState > 10) { + if (backflushOn || backflushState > 10) { machineState = kBackflush; if (standbyModeOn) { @@ -1047,7 +1012,7 @@ void handleMachineState() { // is a brew running? (values are set in brewDetection() above) if ((timeBrewed > 0 && ONLYPID == 1) || - (ONLYPID == 0 && brewcounter > kBrewIdle && brewcounter <= kBrewFinished)) + (ONLYPID == 0 && brewCounter > kBrewIdle && brewCounter <= kBrewFinished)) { machineState = kBrew; @@ -1056,7 +1021,7 @@ void handleMachineState() { } } - if (backflushON || backflushState > 10) { + if (backflushOn || backflushState > 10) { machineState = kBackflush; if (standbyModeOn) { @@ -1095,7 +1060,7 @@ void handleMachineState() { brewDetection(); if ((timeBrewed > 0 && ONLYPID == 1) || // timeBrewed with Only PID - (ONLYPID == 0 && brewcounter > kBrewIdle && brewcounter <= kBrewFinished)) + (ONLYPID == 0 && brewCounter > kBrewIdle && brewCounter <= kBrewFinished)) { machineState = kBrew; @@ -1112,7 +1077,7 @@ void handleMachineState() { } } - if (backflushON || backflushState > 10) { + if (backflushOn || backflushState > 10) { machineState = kBackflush; if (standbyModeOn) { @@ -1153,11 +1118,11 @@ void handleMachineState() { } if ((timeBrewed == 0 && brewDetectionMode == 3 && ONLYPID == 1) || // OnlyPID+: Voltage sensor BD timeBrewed == 0 -> switch is off again - ((brewcounter == kBrewIdle || brewcounter == kWaitBrewOff) && ONLYPID == 0)) // Hardware BD + ((brewCounter == kBrewIdle || brewCounter == kWaitBrewOff) && ONLYPID == 0)) // Hardware BD { // delay shot timer display for voltage sensor or hw brew toggle switch (brew counter) machineState = kShotTimerAfterBrew; - lastbrewTimeMillis = millis(); // for delay + lastBrewTimeMillis = millis(); // for delay } else if (brewDetectionMode == 1 && ONLYPID == 1 && isBrewDetected == 0) { // SW BD, kBrew was active for set time // when Software brew is finished, direct to PID BD machineState = kBrewDetectionTrailing; @@ -1183,17 +1148,17 @@ void handleMachineState() { case kShotTimerAfterBrew: brewDetection(); - if (millis() - lastbrewTimeMillis > BREWSWITCHDELAY) { - debugPrintf("Shot time: %4.1f s\n", lastbrewTime / 1000); + if (millis() - lastBrewTimeMillis > SHOTTIMERDISPLAYDELAY) { + debugPrintf("Shot time: %4.1f s\n", lastBrewTime / 1000); machineState = kBrewDetectionTrailing; - lastbrewTime = 0; + lastBrewTime = 0; } if (steamON == 1) { machineState = kSteam; } - if (backflushON || backflushState > 10) { + if (backflushOn || backflushState > 10) { machineState = kBackflush; } @@ -1224,7 +1189,7 @@ void handleMachineState() { } if ((timeBrewed > 0 && ONLYPID == 1 && brewDetectionMode == 3) || // Allow brew directly after BD only when using OnlyPID AND hardware brew switch detection - (ONLYPID == 0 && brewcounter > kBrewIdle && brewcounter <= kBrewFinished)) + (ONLYPID == 0 && brewCounter > kBrewIdle && brewCounter <= kBrewFinished)) { machineState = kBrew; } @@ -1233,7 +1198,7 @@ void handleMachineState() { machineState = kSteam; } - if (backflushON || backflushState > 10) { + if (backflushOn || backflushState > 10) { machineState = kBackflush; } @@ -1263,7 +1228,7 @@ void handleMachineState() { machineState = kEmergencyStop; } - if (backflushON || backflushState > 10) { + if (backflushOn || backflushState > 10) { machineState = kBackflush; } @@ -1303,7 +1268,7 @@ void handleMachineState() { machineState = kSteam; } - if (backflushON || backflushState > 10) { + if (backflushOn || backflushState > 10) { machineState = kBackflush; } @@ -1325,7 +1290,7 @@ void handleMachineState() { break; case kBackflush: - if (backflushON == 0) { + if (backflushOn == 0) { machineState = kPidNormal; } @@ -1785,7 +1750,7 @@ void setup() { .show = [] { return true && ONLYPID == 0; }, .minValue = BREW_TIME_MIN, .maxValue = BREW_TIME_MAX, - .ptr = (void *)&brewtime + .ptr = (void *)&brewTime }; editableVars["BREW_PREINFUSIONPAUSE"] = { @@ -1798,7 +1763,7 @@ void setup() { .show = [] { return true && ONLYPID == 0; }, .minValue = PRE_INFUSION_PAUSE_MIN, .maxValue = PRE_INFUSION_PAUSE_MAX, - .ptr = (void *)&preinfusionpause + .ptr = (void *)&preinfusionPause }; editableVars["BREW_PREINFUSION"] = { @@ -1963,7 +1928,7 @@ void setup() { .show = [] { return false; }, .minValue = 0, .maxValue = 1, - .ptr = (void *)&backflushON + .ptr = (void *)&backflushOn }; editableVars["STANDBY_MODE_ON"] = { @@ -2014,7 +1979,7 @@ void setup() { mqttVars["steamON"] = []{ return &editableVars.at("STEAM_MODE"); }; mqttVars["steamSetpoint"] = []{ return &editableVars.at("STEAM_SETPOINT"); }; mqttVars["brewPidDelay"] = []{ return &editableVars.at("PID_BD_DELAY"); }; - mqttVars["backflushON"] = []{ return &editableVars.at("BACKFLUSH_ON"); }; + mqttVars["backflushOn"] = []{ return &editableVars.at("BACKFLUSH_ON"); }; mqttVars["startUsePonM"] = []{ return &editableVars.at("START_USE_PONM"); }; mqttVars["startKp"] = []{ return &editableVars.at("START_KP"); }; mqttVars["startTn"] = []{ return &editableVars.at("START_TN"); }; @@ -2064,12 +2029,12 @@ void setup() { // Define trigger type if (triggerType) { - relayON = HIGH; - relayOFF = LOW; + relayOn = HIGH; + relayOff = LOW; } else { - relayON = LOW; - relayOFF = HIGH; + relayOn = LOW; + relayOff = HIGH; } if (optocouplerType == HIGH) { @@ -2085,8 +2050,8 @@ void setup() { pinMode(PIN_VALVE, OUTPUT); pinMode(PIN_PUMP, OUTPUT); pinMode(PIN_HEATER, OUTPUT); - digitalWrite(PIN_VALVE, relayOFF); - digitalWrite(PIN_PUMP, relayOFF); + digitalWrite(PIN_VALVE, relayOff); + digitalWrite(PIN_PUMP, relayOff); digitalWrite(PIN_HEATER, LOW); // IF POWERSWITCH is connected @@ -2316,8 +2281,6 @@ void looppid() { checkPressure(); #endif - checkSteamON(); - if(machineState != kWaterEmpty) { brew(); } @@ -2403,16 +2366,16 @@ void looppid() { if (machineState >= kBrew && machineState <= kBrewDetectionTrailing) { if (brewPIDDelay > 0 && timeBrewed > 0 && timeBrewed < brewPIDDelay*1000) { //disable PID for brewPIDDelay seconds, enable PID again with new tunings after that - if (!brewPIDdisabled) { - brewPIDdisabled = true; + if (!brewPIDDisabled) { + brewPIDDisabled = true; bPID.SetMode(MANUAL); debugPrintf("disabled PID, waiting for %d seconds before enabling PID again\n", brewPIDDelay); } } else { - if (brewPIDdisabled) { + if (brewPIDDisabled) { //enable PID again bPID.SetMode(AUTOMATIC); - brewPIDdisabled = false; + brewPIDDisabled = false; debugPrintln("Enabled PID again after delay"); } @@ -2513,7 +2476,7 @@ void loopWater() { } void setBackflush(int backflush) { - backflushON = backflush; + backflushOn = backflush; } void setSteamMode(int steamMode) { diff --git a/src/powerswitchvoid.h b/src/powerswitchvoid.h index 53c18dcb8..04cde1cec 100644 --- a/src/powerswitchvoid.h +++ b/src/powerswitchvoid.h @@ -9,7 +9,7 @@ */ int lastPowerSwitchTrigger = LOW; // the previous reading from the input pin -int buttonStatePowerTrigger; // the current reading from the input pin +int lastStatePowerTrigger; // the current reading from the input pin unsigned long lastDebounceTimePowerTrigger = 0; // the last time the output pin was toggled unsigned long debounceDelayPowerTrigger = 50; // the debounce time; increase if the output flickers @@ -37,11 +37,11 @@ void checkPowerSwitch() { // delay, so take it as the actual current state: // if the button state has changed: - if (reading != buttonStatePowerTrigger) { - buttonStatePowerTrigger = reading; + if (reading != lastStatePowerTrigger) { + lastStatePowerTrigger = reading; // only toggle heating power if the new button state is HIGH - if (buttonStatePowerTrigger == HIGH) { + if (lastStatePowerTrigger == HIGH) { if (pidON == 0) { Serial.println("Turn Heating ON"); pidON = 1; diff --git a/src/scalevoid.h b/src/scalevoid.h index fae4802b1..313febfce 100644 --- a/src/scalevoid.h +++ b/src/scalevoid.h @@ -70,11 +70,11 @@ void initScale() { * @brief Scale with shot timer */ void shottimerscale() { - switch (shottimercounter) { + switch (shottimerCounter) { case 10: // waiting step for brew switch turning on if (timeBrewed > 0) { weightPreBrew = weight; - shottimercounter = 20; + shottimerCounter = 20; } break; @@ -83,7 +83,7 @@ void shottimerscale() { weightBrew = weight - weightPreBrew; if (timeBrewed == 0) { - shottimercounter = 10; + shottimerCounter = 10; } break; diff --git a/src/steamswitchvoid.h b/src/steamswitchvoid.h index 46df5ffda..22882ff08 100644 --- a/src/steamswitchvoid.h +++ b/src/steamswitchvoid.h @@ -9,9 +9,9 @@ */ int lastSteamSwitchTrigger = LOW; // the previous reading from the input pin -int buttonStateSteamTrigger; // the current reading from the input pin +int lastStateSteamTrigger; // the current reading from the input pin unsigned long lastDebounceTimeSteamTrigger = 0; // the last time the output pin was toggled -unsigned long debounceDelaySteamTrigger = 50; // the debounce time; increase if the output flickers +unsigned long debounceDelaySteamTrigger = 20; // the debounce time; increase if the output flickers void checkSteamSwitch() { #if STEAMSWITCHTYPE == 0 @@ -26,6 +26,20 @@ void checkSteamSwitch() { if (digitalRead(PIN_STEAMSWITCH) == LOW && steamFirstON == 0) { steamON = 0; } + + // monitor QuickMill thermoblock steam-mode + if (machine == QuickMill) { + if (steamQM_active == true) { + if (checkSteamOffQM() == true) { // if true: steam-mode can be turned off + steamON = 0; + steamQM_active = false; + lastTimePVSwasON = 0; + } else { + steamON = 1; + } + } + } + #elif STEAMSWITCHTYPE == 2 // TRIGGER int reading = digitalRead(PIN_STEAMSWITCH); @@ -39,11 +53,11 @@ void checkSteamSwitch() { // delay, so take it as the actual current state: // if the button state has changed: - if (reading != buttonStateSteamTrigger) { - buttonStateSteamTrigger = reading; + if (reading != lastStateSteamTrigger) { + lastStateSteamTrigger = reading; // only toggle heating power if the new button state is HIGH - if (buttonStateSteamTrigger == HIGH) { + if (lastStateSteamTrigger == HIGH) { if (steamON == 0) { Serial.println("Turn Steam ON"); steamON = 1; @@ -57,4 +71,5 @@ void checkSteamSwitch() { lastSteamSwitchTrigger = reading; #endif + } diff --git a/src/userConfig_sample.h b/src/userConfig_sample.h index 9560279fe..31c551d7a 100644 --- a/src/userConfig_sample.h +++ b/src/userConfig_sample.h @@ -38,7 +38,7 @@ enum MACHINE { #define SHOTTIMER 1 // 0 = deactivated, 1 = activated 2 = with scale #define HEATINGLOGO 0 // 0 = deactivated, 1 = Rancilio, 2 = Gaggia #define OFFLINEGLOGO 1 // 0 = deactivated, 1 = activated -#define BREWSWITCHDELAY 3000 // time in ms that the brew switch will be delayed (shot timer will show that much longer after switching off) +#define SHOTTIMERDISPLAYDELAY 3000 // time in ms that shot timer will be shown after brew finished #define VERBOSE 0 // 1 = Show verbose output (serial connection), 0 = show less #define LANGUAGE 0 // LANGUAGE = 0 (DE), LANGUAGE = 1 (EN), LANGUAGE = 2 (ES)