diff --git a/brew.h b/brew.h index d0e137e293828a38a03cb9be0ba08299705b2ed2..692db347db56880b6c4c589245aa55ddebf9ecc0 100644 --- a/brew.h +++ b/brew.h @@ -94,17 +94,18 @@ void backToStatus(); // #################################################### Set Variables ################################################################## -int getTimer( int initialValue, int defaultValue ); +boolean checkForEncoderSwitchPush( bool cancelable ); int getTimer( int initialValue ); +int getTimer( int initialValue, int defaultValue ); -int getTemperature(int initialValue); - -int xSetGenericValue(int initialValue, int minimumValue, int maximumValue, const char *valueName, const char *unit); +int getTemperature( int initialValue ); +int getTemperature( int initialValue, int defaultValue ); -int xSetTemperature( int initialValue ); +int getFinalYield( int initialValue ); +int getFinalYield( int initialValue, int defaultValue ); -int xSetFinalYield( int initialValue ); +int xSetGenericValue( int initialValue, int defaultValue, int minimumValue, int maximumValue, const char *valueName, const char *unit ); unsigned long getInactivityTime(); @@ -112,6 +113,4 @@ unsigned long getInactivityTime(); void xWaitForAction(String title, String message); -boolean gotButtonPress(int iPin); - #endif //__BREW diff --git a/brew.ino b/brew.ino index 4c306e1a52576938f4773300b7d20a65c8d2b777..6cd690116d956ef57e481b20d113b8ee14f131e3 100644 --- a/brew.ino +++ b/brew.ino @@ -37,8 +37,6 @@ boolean clockEnd; unsigned long cookTime; int cookTemperature; -//cook_mode_list cookMode; -//int cookMixerSpeed; int finalYield; unsigned long startpointTime; @@ -69,6 +67,7 @@ int cleaningTemperature; boolean refresh; boolean repaint; +boolean cancel; boolean bStatusElement; @@ -286,10 +285,12 @@ void setup() { rotarySwDetectTime = 0; // ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++ - eMenuType = eMenuType_Main; + eMenuType = MENU_INIT; - cookingStage = eCookingStage_Startpoint; - beerProfile = eBeerProfile_Basic; + cookingStage = SETTING_COOKING_STAGE_INIT; + beerProfile = SETTING_BEER_PROFILE_INIT; + + cancel = false; // ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++ cooking = false; @@ -303,9 +304,7 @@ void setup() { cookTime = 3600; cookTemperature = 25; - //cookMode = quick_start; - //cookMixerSpeed = 120; - finalYield = 25; + finalYield = SETTING_MACHINE_YIELD_DEFAULT; startpointTime = PROFILE_BASIC_STARTPOINT_TIME; betaGlucanaseTime = PROFILE_BASIC_BETAGLUCANASE_TIME; @@ -1375,8 +1374,12 @@ void runStageSelection() { void runSettingsSelection() { switch (mdSettingsMenu._selection) { case eSettingsMenu_Pump: { - // Stuff - if ( xSetGenericValue( iPumpSpeed ? 0 : 1, 0, 1, "pump", "bool" ) ) { + bool bNewPumpStatus = xSetGenericValue( iPumpSpeed ? 0 : 1, PUMP_SPEED_DEFAULT, 0, 1, "pump", "bool" ); + if( cancel ) { + cancel = false; + return; + } + if ( bNewPumpStatus ) { iPumpSpeed = PUMP_SPEED_MAX_MOSFET; } else { iPumpSpeed = PUMP_SPEED_STOP_MOSFET; @@ -1432,16 +1435,30 @@ void runMenuProcessor( MenuData *data ) { data->_repaint = repaint; // Request repaint repaint = displayGenericMenu( &lcd, data ); // Display menu - if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) { // Read selection + if ( checkForEncoderSwitchPush( false ) ) { // Read selection data->_selection = data->_position; } - (data->_selectionFunction)(); // Run selection function + (data->_selectionFunction)(); // Run selection function } void runStageSelection_Generic( unsigned long * selectedStageTime, int *selectedStageTemperature) { - (*selectedStageTime) = getTimer( (*selectedStageTime) ); - (*selectedStageTemperature) = xSetGenericValue( (*selectedStageTemperature), TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, MENU_GLOBAL_STR_TEMPERATURE, MENU_GLOBAL_STR_CELSIUS ); + unsigned long selectedStageTimeStorage = *selectedStageTime; + int selectedStageTemperatureStorage = *selectedStageTemperature; + + *selectedStageTime = getTimer( *selectedStageTime ); + if( cancel ) { + *selectedStageTime = selectedStageTimeStorage; + cancel = false; + return; + } + *selectedStageTemperature = getTemperature( *selectedStageTemperature ); + if( cancel ) { + *selectedStageTime = selectedStageTimeStorage; + *selectedStageTemperature = selectedStageTemperatureStorage; + cancel = false; + return; + } backToStatus(); } @@ -1454,17 +1471,38 @@ void xStartStageInteractive( unsigned long *stageTime, int *stageTemperature, eC } void xStartStage( unsigned long *stageTime, int *stageTemperature, eCookingStages nextStage, bool bPurgePump, bool bSetFinalYield, bool bSetTime, bool bSetTemperature ) { - xSafeHardwarePowerOff(); // Stop anything that might be still going on - + int finalYieldStorage = finalYield; + unsigned long stageTimeStorage = *stageTime; + int stageTemperatureStorage = *stageTemperature; + if (bSetFinalYield) { - finalYield = xSetFinalYield( finalYield ); + finalYield = getFinalYield( finalYield, SETTING_MACHINE_YIELD_DEFAULT ); + if( cancel ) { + finalYield = finalYieldStorage; + cancel = false; + return; + } } if (bSetTime) { - (*stageTime) = getTimer( clockCounter / 1000, (*stageTime) ); + (*stageTime) = getTimer( clockCounter / 1000, *stageTime ); + if( cancel ) { + finalYield = finalYieldStorage; + *stageTime = stageTimeStorage; + cancel = false; + return; + } } if (bSetTemperature) { - (*stageTemperature) = xSetTemperature( (*stageTemperature) ); + (*stageTemperature) = getTemperature( cookTemperature, *stageTemperature ); + if( cancel ) { + finalYield = finalYieldStorage; + *stageTime = stageTimeStorage; + *stageTemperature = stageTemperatureStorage; + cancel = false; + return; + } } + xSafeHardwarePowerOff(); // Stop anything that might be still going on if (bPurgePump) { xPurgePump(); } @@ -1494,8 +1532,7 @@ void resetMenu( boolean requestRepaintPaint ) { repaint = true; } - // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep - xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdMainMenu._position, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 ); + xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdMainMenu._position, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 ); // reset operation state } void backToStatus() { @@ -1505,6 +1542,68 @@ void backToStatus() { // #################################################### Helpers ################################################################## // #################################################### Set Variables ################################################################## + +int getTemperature(int initialValue ) { return getTemperature( initialValue, initialValue ); } +int getTemperature(int initialValue, int defaultValue ) { return xSetGenericValue( initialValue, defaultValue, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, MENU_GLOBAL_STR_TEMPERATURE, MENU_GLOBAL_STR_CELSIUS ); } + +int getFinalYield( int initialValue ) { return getFinalYield( initialValue, SETTING_MACHINE_YIELD_DEFAULT ); } +int getFinalYield( int initialValue, int defaultValue ) { return xSetGenericValue( initialValue, defaultValue, SETTING_MACHINE_YIELD_CAPACITY_MIN, SETTING_MACHINE_YIELD_CAPACITY_MAX, "Final Yield", "l" ); } + +int xSetGenericValue(int initialValue, int defaultValue, int minimumValue, int maximumValue, const char *valueName, const char *unit) { + xSetupRotaryEncoder( eRotaryEncoderMode_Generic, initialValue, maximumValue, minimumValue, 1, 5 ); + + // initialize variables + int rotaryEncoderPreviousPosition = 0; + + // Setup Screen + lcd.clear(); + lcd.home(); + lcd.print( "Set " ); + lcd.print( valueName ); + lcd.print( "(" ); + lcd.print( defaultValue ); + lcd.print( ")" ); + lcd.setCursor ( 0 , LCD_VERTICAL_RESOLUTION - 1 ); + lcd.print( " 0 " ); + lcd.print( unit ); + + while (true) { + if( checkForEncoderSwitchPush( true ) ) { // Check if pushbutton is pressed + if( cancel ) return rotaryEncoderVirtualPosition; + break; + } + else { + xManageMachineSystems(); // Don't forget to keep an eye on the cooking + } + + // Check if there was an update by the rotary encoder + if ( rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition ) { + rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition; + + lcd.setCursor( 0, LCD_VERTICAL_RESOLUTION - 1 ); + lcd.print( " " ); + if ( rotaryEncoderVirtualPosition < 10 ) { + lcd.print( " " ); + } + else { + if ( rotaryEncoderVirtualPosition < 100 ) { + lcd.print( " " ); + } + } + lcd.print( rotaryEncoderVirtualPosition ); + lcd.print( " " ); + lcd.print( unit ); + lcd.println( " " ); + } + } + + return rotaryEncoderVirtualPosition; +} + +int getTimer( int initialValue ) { + return getTimer( initialValue, initialValue ); +} + int getTimer( int initialValue, int defaultValue ) { // set operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep xSetupRotaryEncoder( eRotaryEncoderMode_Time, initialValue, 7200, 0, 1, 30 ); @@ -1531,19 +1630,12 @@ int getTimer( int initialValue, int defaultValue ) { lcd.print(" 0:00"); while (true) { - // Check if pushbutton is pressed - if ((digitalRead(ROTARY_ENCODER_SW_PIN))) { - // Wait until switch is released - while (digitalRead(ROTARY_ENCODER_SW_PIN)) {} - - // debounce - delay(10); - - // Job is done, break the circle + if( checkForEncoderSwitchPush( true ) ) { + if( cancel ) return rotaryEncoderVirtualPosition; break; - } else { - // Don't forget to keep an eye on the cooking - xManageMachineSystems(); + } + else { + xManageMachineSystems(); // Don't forget to keep an eye on the cooking } // display current timer @@ -1574,129 +1666,24 @@ int getTimer( int initialValue, int defaultValue ) { return rotaryEncoderVirtualPosition; } -int getTimer( int initialValue ) { - return getTimer( initialValue, initialValue ); -} - -int getTemperature(int initialValue) { - - // set operation state - rotaryEncoderMode = eRotaryEncoderMode_Generic; - rotaryEncoderVirtualPosition = initialValue; - - // initialize variables - int rotaryEncoderPreviousPosition = 0; - - // Setup Screen - lcd.clear(); - lcd.home(); - lcd.print("Set Temperature"); - lcd.setCursor (0, LCD_VERTICAL_RESOLUTION - 1); - lcd.print(" 0 *C"); - - rotaryEncoderMaxPosition = TEMPERATURE_SETTING_MAX_VALUE; - - while (true) { - // Check if pushbutton is pressed - if ((digitalRead(ROTARY_ENCODER_SW_PIN))) { - // Wait until switch is released - while (digitalRead(ROTARY_ENCODER_SW_PIN)) {} - - // debounce - delay(10); - - // Job is done, break the circle - break; - } else { - // Don't forget to keep an eye on the cooking - xManageMachineSystems(); - } - - // display current timer - if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) { - rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition; - - lcd.setCursor (0, LCD_VERTICAL_RESOLUTION - 1); - lcd.print(" "); - if (rotaryEncoderVirtualPosition < 10) { - lcd.print(" "); - } - else { - if (rotaryEncoderVirtualPosition < 100) { - lcd.print(" "); - } +boolean checkForEncoderSwitchPush( bool cancelable ) { + boolean gotPush = digitalRead(ROTARY_ENCODER_SW_PIN); + if (gotPush) { // Check if pushbutton is pressed + unsigned long cancleTimer = millis(); + while (digitalRead(ROTARY_ENCODER_SW_PIN)) { // Wait until switch is released + delay(ROTARY_ENCODER_SW_DEBOUNCE_TIME); // debounce + + if( ((millis() - cancleTimer) >= (SETTING_CANCEL_TIMER/2) ) && cancelable ) { + sing(BUZZ_1, PIEZO_PIN); } - lcd.print(rotaryEncoderVirtualPosition); - lcd.print(" *C"); - lcd.println(" "); } - } - - return rotaryEncoderVirtualPosition; -} - -int xSetGenericValue(int initialValue, int minimumValue, int maximumValue, const char *valueName, const char *unit) { - // set operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep - xSetupRotaryEncoder( eRotaryEncoderMode_Generic, initialValue, maximumValue, minimumValue, 1, 5 ); - - // initialize variables - int rotaryEncoderPreviousPosition = 0; - - // Setup Screen - lcd.clear(); - lcd.home(); - lcd.print( "Set " ); - lcd.print( valueName ); - lcd.setCursor ( 0 , LCD_VERTICAL_RESOLUTION - 1 ); - lcd.print( " 0 " ); - lcd.print( unit ); - - while (true) { - // Check if pushbutton is pressed - if ( digitalRead(ROTARY_ENCODER_SW_PIN) ) { - // Wait until switch is released - while ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {} - // debounce - delay( 10 ); - - // Job is done, break the circle - break; - } else { - // Don't forget to keep an eye on the cooking - xManageMachineSystems(); - } - - // Check if there was an update by the rotary encoder - if ( rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition ) { - rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition; - - lcd.setCursor( 0, LCD_VERTICAL_RESOLUTION - 1 ); - lcd.print( " " ); - if ( rotaryEncoderVirtualPosition < 10 ) { - lcd.print( " " ); - } - else { - if ( rotaryEncoderVirtualPosition < 100 ) { - lcd.print( " " ); - } - } - lcd.print( rotaryEncoderVirtualPosition ); - lcd.print( " " ); - lcd.print( unit ); - lcd.println( " " ); + if( ((millis() - cancleTimer) >= SETTING_CANCEL_TIMER) && cancelable ) { + cancel = true; } } - return rotaryEncoderVirtualPosition; -} - -int xSetTemperature( int initialValue ) { - return xSetGenericValue( initialValue, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" ); -} - -int xSetFinalYield( int initialValue ) { - return xSetGenericValue( initialValue, SETTING_MACHINE_YIELD_CAPACITY_MIN, SETTING_MACHINE_YIELD_CAPACITY_MAX, "Final Yield", "l" ); + return gotPush; } unsigned long getInactivityTime() { @@ -1704,11 +1691,7 @@ unsigned long getInactivityTime() { unsigned long rotaryEncoderInactivityTime = now - lastInterruptTime; if (rotaryEncoderInactivityTime > SETTING_MAX_INACTIVITY_TIME) { - if (digitalRead(ROTARY_ENCODER_SW_PIN)) { - while (digitalRead(ROTARY_ENCODER_SW_PIN)) { - delay(ROTARY_ENCODER_SW_DEBOUNCE_TIME); - } - + if (checkForEncoderSwitchPush( false )) { now = millis(); rotaryEncoderInactivityTime = now - lastInterruptTime; rotarySwDetectTime = now; @@ -1726,17 +1709,10 @@ unsigned long getInactivityTime() { void xWaitForAction(String title, String message) { while (true) { - // Check if pushbutton is pressed - if ( digitalRead(ROTARY_ENCODER_SW_PIN) ) { - // Wait until switch is released - while ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {} - - // debounce - delay( 10 ); - - // Job is done, break the circle + if( checkForEncoderSwitchPush( false ) ) { // Check if pushbutton is pressed break; - } else { + } + else { sing(BUZZ_1, PIEZO_PIN); // Print the message @@ -1746,15 +1722,3 @@ void xWaitForAction(String title, String message) { } } } - -boolean gotButtonPress(int iPin) { - boolean ret = false; - - if ((digitalRead(iPin))) { // check if pushbutton is pressed - ret = true; - while (digitalRead(iPin)) {} // wait til switch is released - delay(10); // debounce - } - - return ret; -} diff --git a/config.h b/config.h index 862701fd461a3843a9cef056a2ee900795a923af..508bc8e13293a294fb9aff9ac4e65271f88ab5bd 100644 --- a/config.h +++ b/config.h @@ -12,6 +12,7 @@ #define SETTING_CLEANING_TEMPERATURE 45 //45 #define SETTING_MACHINE_YIELD_CAPACITY_MIN 0 #define SETTING_MACHINE_YIELD_CAPACITY_MAX 50 +#define SETTING_MACHINE_YIELD_DEFAULT 25 // ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++ #define HEATING_ELEMENT_DEFAULT_WINDOW_SIZE 1000 @@ -26,33 +27,13 @@ #define TEMPERATURE_MAX_VALUE 120 #define PT100_BASE_INPUT_PIN A4 #define PT100_BASE_OUTPUT_PIN 32 -//#define PT100_BASE_INPUT_R_PIN A7 -//#define PT100_BASE_OUTPUT_R_PIN 7 #define PT100_BASE_TIME_BETWEEN_READINGS 1 #define PT100_UP_INPUT_PIN A5 #define PT100_UP_OUTPUT_PIN 30 -//#define PT100_UP_INPUT_R_PIN A8 -//#define PT100_UP_OUTPUT_R_PIN 9 #define PT100_UP_TIME_BETWEEN_READINGS 1 #define PT100_DOWN_INPUT_PIN A6 #define PT100_DOWN_OUTPUT_PIN 31 -//#define PT100_DOWN_INPUT_R_PIN A9 -//#define PT100_DOWN_OUTPUT_R_PIN 8 #define PT100_DOWN_TIME_BETWEEN_READINGS 1 -/* -#define PT100_BASE_DEFAULT_ADC_VMAX 1.1 -#define PT100_BASE_DEFAULT_VS 5.0 -#define PT100_BASE_DEFAULT_R1_RESISTENCE 605.2 -#define PT100_BASE_DEFAULT_R2_RESISTENCE 605.2 -#define PT100_UP_DEFAULT_ADC_VMAX 1.1 -#define PT100_UP_DEFAULT_VS 5.0 -#define PT100_UP_DEFAULT_R1_RESISTENCE 630.0 //620.0 -#define PT100_UP_DEFAULT_R2_RESISTENCE 610.0 -#define PT100_DOWN_DEFAULT_ADC_VMAX 1.1 -#define PT100_DOWN_DEFAULT_VS 5.0 -#define PT100_DOWN_DEFAULT_R1_RESISTENCE 616.0 -#define PT100_DOWN_DEFAULT_R2_RESISTENCE 611.0 -*/ // ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++ //#define MIXER_PIN 12 @@ -71,6 +52,7 @@ #define PUMP_SPEED_FAST 192 #define PUMP_SPEED_MAX_MOSFET 0 #define PUMP_SPEED_MAX 255 +#define PUMP_SPEED_DEFAULT 0 // ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++ #define ROTARY_ENCODER_INTERRUPT_NUMBER 1 // On Mega2560 boards, interrupt 1 is on pin 3 @@ -83,11 +65,15 @@ // ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++ #define SETTING_WELCOME_TIMEOUT 100 #define SETTING_MAX_INACTIVITY_TIME 3000 +#define SETTING_CANCEL_TIMER 2000 +#define MENU_INIT eMenuType_Main #define MENU_SIZE_MAIN_MENU 13 #define MENU_SIZE_PROFILES_MENU 9 #define MENU_SIZE_STAGE_MENU 13 #define MENU_SIZE_MALT_MENU 4 #define MENU_SIZE_SETTINGS_MENU 6 +#define SETTING_COOKING_STAGE_INIT eCookingStage_Startpoint +#define SETTING_BEER_PROFILE_INIT eBeerProfile_Basic // ++++++++++++++++++++++++ Menus ++++++++++++++++++++++++ #define MENU_MAIN_TITLE "Brewery Menu"