brew.ino 64.3 KB
Newer Older
João Lino's avatar
Rel.3  
João Lino committed
1 2 3 4 5
/*
  brew.ino - Main execution file.
  Created by João Lino, August 28, 2014.
  Released into the public domain.
*/
6

João Lino's avatar
João Lino committed
7 8
//#define DEBUG
#define INFO
9

10
// ######################### LIBRARIES #########################
João Lino's avatar
João Lino committed
11
#include "brew.h"
12

13
// ######################### VARIABLES #########################
14
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
15
eRotaryEncoderMode      rotaryEncoderMode;
João Lino's avatar
Rel.3  
João Lino committed
16 17 18 19 20 21

eCookingStages          cookingStage;
eBeerProfile            beerProfile;

eMenuType               eMenuType;

João Lino's avatar
João Lino committed
22 23 24 25 26 27
MenuData                mdMainMenu =            { ._title = MENU_MAIN_TITLE, ._dialog = MENU_MAIN_DIALOG, ._position = MENU_MAIN_INIT_POSITION, ._selection = MENU_MAIN_INIT_SELECTION, ._repaint = MENU_MAIN_INIT_REPAINT, ._selectionFunction = MENU_MAIN_FUNCTION };
MenuData                mdBeerProfileMenu =     { ._title = MENU_PROFILE_TITLE, ._dialog = MENU_PROFILE_DIALOG, ._position = MENU_PROFILE_INIT_POSITION, ._selection = MENU_PROFILE_INIT_SELECTION, ._repaint = MENU_PROFILE_INIT_REPAINT, ._selectionFunction = MENU_PROFILE_FUNCTION };
MenuData                mdStageMenu =           { ._title = MENU_STAGE_TITLE, ._dialog = MENU_STAGE_DIALOG, ._position = MENU_STAGE_INIT_POSITION, ._selection = MENU_STAGE_INIT_SELECTION, ._repaint = MENU_STAGE_INIT_REPAINT, ._selectionFunction = MENU_STAGE_FUNCTION };
MenuData                mdMaltMenu =            { ._title = MENU_MALT_TITLE, ._dialog = MENU_MALT_DIALOG, ._position = MENU_MALT_INIT_POSITION, ._selection = MENU_MALT_INIT_SELECTION, ._repaint = MENU_MALT_INIT_REPAINT, ._selectionFunction = MENU_MALT_FUNCTION };
MenuData                mdSettingsMenu =        { ._title = MENU_SETTINGS_TITLE, ._dialog = MENU_SETTINGS_DIALOG, ._position = MENU_SETTINGS_INIT_POSITION, ._selection = MENU_SETTINGS_INIT_SELECTION, ._repaint = MENU_SETTINGS_INIT_REPAINT, ._selectionFunction = MENU_SETTINGS_FUNCTION };
MenuData                mdStartFromStageMenu =  { ._title = MENU_START_TITLE, ._dialog = MENU_START_DIALOG, ._position = MENU_START_INIT_POSITION, ._selection = MENU_START_INIT_SELECTION, ._repaint = MENU_START_INIT_REPAINT, ._selectionFunction = MENU_START_FUNCTION };
João Lino's avatar
Rel.3  
João Lino committed
28

29
// ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
30 31
boolean                 cooking;

João Lino's avatar
Rel.3  
João Lino committed
32 33 34 35
unsigned long           clockStartTime;
unsigned long           clockLastUpdate;
long                    clockCounter;
unsigned long           clockIgnore;
João Lino's avatar
João Lino committed
36 37 38
boolean                 clockStart;
boolean                 clockEnd;

João Lino's avatar
Rel.3  
João Lino committed
39
unsigned long           cookTime;
João Lino's avatar
João Lino committed
40
int                     cookTemperature;
João Lino's avatar
Rel.3  
João Lino committed
41
int                     finalYield;
42

João Lino's avatar
Rel.3  
João Lino committed
43 44 45 46 47 48 49 50 51 52 53 54
unsigned long           startpointTime;
unsigned long           betaGlucanaseTime;
unsigned long           debranchingTime;
unsigned long           proteolyticTime;
unsigned long           betaAmylaseTime;
unsigned long           alphaAmylaseTime;
unsigned long           mashoutTime;
unsigned long           recirculationTime;
unsigned long           spargeTime;
unsigned long           boilTime;
unsigned long           coolingTime;
unsigned long           cleaningTime;
55

João Lino's avatar
João Lino committed
56 57 58 59 60 61 62 63 64 65 66
int                     startpointTemperature;
int                     betaGlucanaseTemperature;
int                     debranchingTemperature;
int                     proteolyticTemperature;
int                     betaAmylaseTemperature;
int                     alphaAmylaseTemperature;
int                     mashoutTemperature;
int                     recirculationTemperature;
int                     spargeTemperature;
int                     boilTemperature;
int                     coolingTemperature;
João Lino's avatar
Rel.3  
João Lino committed
67
int                     cleaningTemperature;
João Lino's avatar
João Lino committed
68 69 70

boolean                 refresh;
boolean                 repaint;
João Lino's avatar
João Lino committed
71
boolean                 cancel;
72

João Lino's avatar
Rel.3  
João Lino committed
73 74
boolean                 bStatusElement;

João Lino's avatar
João Lino committed
75 76 77
unsigned long           loggingTimeInterval;


78
// ++++++++++++++++++++++++ Interrupts ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
79
static unsigned long    lastInterruptTime;
80

81
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
82 83 84 85 86
volatile int            rotaryEncoderVirtualPosition = 0;
volatile int            rotaryEncoderMaxPosition = 1;
volatile int            rotaryEncoderMinPosition = 0;
volatile int            rotaryEncoderSingleStep = 1;
volatile int            rotaryEncoderMultiStep = 1;
87

João Lino's avatar
João Lino committed
88 89
volatile boolean        onISR = false;

João Lino's avatar
João Lino committed
90 91
unsigned long           rotarySwDetectTime;

92
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
93 94 95
int                     iWindowSize;             // Time frame to operate in
unsigned long           windowStartTime;
double                  dWattPerPulse;
96

97 98 99
// ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
int                     iPumpSpeed;             // Time frame to operate in

100
// ######################### INITIALIZE #########################
101
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
102
LiquidCrystal_I2C       lcd(LCD_I2C_ADDR, LCD_EN_PIN, LCD_RW_PIN, LCD_RS_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN);
103

João Lino's avatar
João Lino committed
104
// +++++++++++++++++++++++ Temperature +++++++++++++++++++++++
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
Temperature                   basePT100("base",
                                        PT100_BASE_OUTPUT_PIN,
                                        PT100_BASE_INPUT_PIN,
                                        PT100_BASE_TIME_BETWEEN_READINGS,
                                        2.0298, 2.0259, 665.24, 662.17);
Temperature                   upPT100("up",
                                      PT100_UP_OUTPUT_PIN,
                                      PT100_UP_INPUT_PIN,
                                      PT100_UP_TIME_BETWEEN_READINGS,
                                      2.0274, 2.0245, 659.43, 656.72);
Temperature                   downPT100("down",
                                        PT100_DOWN_OUTPUT_PIN,
                                        PT100_DOWN_INPUT_PIN,
                                        PT100_DOWN_TIME_BETWEEN_READINGS,
                                        2.0309, 2.0288, 658.15, 655.35);
120 121

// ######################### INTERRUPTS #########################
João Lino's avatar
João Lino committed
122
void isr ()  {    // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
123
  unsigned long interruptTime = millis();
João Lino's avatar
João Lino committed
124
  unsigned long diff = interruptTime - lastInterruptTime;
125

126
  // If interrupts come faster than [ROTARY_ENCODER_DEBOUNCE_TIME]ms, assume it's a bounce and ignore
João Lino's avatar
João Lino committed
127
  if (diff > ROTARY_ENCODER_DEBOUNCE_TIME) {
João Lino's avatar
João Lino committed
128
    lastInterruptTime = interruptTime;
129 130

    switch (rotaryEncoderMode) {
131 132
      // Input of rotary encoder controling menus
      case eRotaryEncoderMode_Menu: {
133
          if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
134
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
135 136
          }
          else {
137
            rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - rotaryEncoderSingleStep;
138 139
          }
          if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
João Lino's avatar
João Lino committed
140
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
141 142
          }
          if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
João Lino's avatar
João Lino committed
143
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
144 145 146
          }

          break;
147
        }
148

149 150
      // Input of rotary encoder controling time variables
      case eRotaryEncoderMode_Time: {
151 152 153 154 155 156 157
          if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
            if (rotaryEncoderVirtualPosition >= 60) {
              rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderMultiStep);
            }
            else {
              rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
            }
158 159
          }
          else {
160 161
            if (rotaryEncoderVirtualPosition == rotaryEncoderMinPosition) {
              rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 60);
162 163
            }
            else {
164 165 166 167 168 169
              if (rotaryEncoderVirtualPosition >= (60 + rotaryEncoderMultiStep)) {
                rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - rotaryEncoderMultiStep);
              }
              else {
                rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - rotaryEncoderSingleStep;
              }
170 171
            }
          }
172
          if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
173
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
174 175
          }
          if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
176
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
177 178 179
          }

          break;
180
        }
181

182 183
      // Input of rotary encoder controling generic integer variables within a range between rotaryEncoderMinPosition and rotaryEncoderMaxPosition
      case eRotaryEncoderMode_Generic: {
184 185 186 187 188 189 190
          if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
          }
          else {
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - rotaryEncoderSingleStep);
          }
          if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
service-config's avatar
Mixer  
service-config committed
191
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
192 193
          }
          if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
194
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
195 196 197
          }

          break;
service-config's avatar
Mixer  
service-config committed
198
        }
199
      default: {
200 201

        }
202
    }
203

João Lino's avatar
João Lino committed
204 205
    repaint = true;
    refresh = true;
206 207 208
  }
}

209
void xSetupRotaryEncoder( eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep ) {
210 211 212 213 214 215
  if ( newMode >= 0 ) rotaryEncoderMode = newMode;
  if ( newPosition >= 0 ) rotaryEncoderVirtualPosition = newPosition;
  if ( newMaxPosition >= 0 ) rotaryEncoderMaxPosition = newMaxPosition;
  if ( newMinPosition >= 0 ) rotaryEncoderMinPosition = newMinPosition;
  if ( newSingleStep >= 0 ) rotaryEncoderSingleStep = newSingleStep;
  if ( newMultiStep >= 0 ) rotaryEncoderMultiStep = newMultiStep;
216 217
}

218
// ######################### START #########################
219
void xSafeHardwarePowerOff() {
220
  // Turn off gracefully
João Lino's avatar
João Lino committed
221
  iPumpSpeed = PUMP_SPEED_STOP_MOSFET;
222 223 224
  xRegulatePumpSpeed();

  // Force shutdown
João Lino's avatar
João Lino committed
225
  analogWrite(PUMP_PIN, PUMP_SPEED_STOP_MOSFET);  // analogWrite values from 0 to 255
João Lino's avatar
João Lino committed
226
  digitalWrite(HEATING_ELEMENT_OUTPUT_PIN, LOW);  // Turn heading element OFF for safety
João Lino's avatar
Rel.3  
João Lino committed
227 228
  bStatusElement = false;

João Lino's avatar
João Lino committed
229 230 231
  basePT100.setPumpStatus( false );
  upPT100.setPumpStatus( false );
  downPT100.setPumpStatus( false );
232

João Lino's avatar
Rel.3  
João Lino committed
233
  //analogWrite(MIXER_PIN, 0);        // Turn mixer OFF for safety
234 235
}

João Lino's avatar
Rel.3  
João Lino committed
236 237 238
void xWelcomeUser() {
  //#ifndef DEBUG
  lcdPrint(&lcd, "  Let's start", "    Brewing!");    // Write welcome
239

240 241 242
  // Play Melody;
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);

243
  //termometerCalibration();
João Lino's avatar
João Lino committed
244
  delay(SETTING_WELCOME_TIMEOUT);      // pause for effect
João Lino's avatar
Rel.3  
João Lino committed
245
  //#endif
246 247
}

248 249
const char *_dialogs[] = {"", "b"};

250
void setup() {
João Lino's avatar
João Lino committed
251
  // ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
252
  pinMode                         (ROTARY_ENCODER_CLK_PIN, INPUT);
João Lino's avatar
João Lino committed
253 254 255 256 257 258 259
  pinMode                         (ROTARY_ENCODER_DT_PIN, INPUT);
  pinMode                         (ROTARY_ENCODER_SW_PIN, INPUT);
  attachInterrupt                 (ROTARY_ENCODER_INTERRUPT_NUMBER, isr, FALLING);

  // ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
  pinMode                         (HEATING_ELEMENT_OUTPUT_PIN, OUTPUT);
  digitalWrite                    (HEATING_ELEMENT_OUTPUT_PIN, LOW);
João Lino's avatar
Rel.3  
João Lino committed
260
  bStatusElement              =   false;
João Lino's avatar
João Lino committed
261 262 263 264 265
  windowStartTime             =   millis();
  dWattPerPulse               =   HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ;

  // ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++

266 267
  // ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
  pinMode(PUMP_PIN, OUTPUT);   // sets the pin as output
João Lino's avatar
João Lino committed
268
  iPumpSpeed                  =   PUMP_SPEED_STOP_MOSFET;             // Time frame to operate in
269 270
  analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255

João Lino's avatar
João Lino committed
271
  // ++++++++++++++++++++++++ Piezo ++++++++++++++++++++++++
272 273
  pinMode(PIEZO_PIN, OUTPUT);

João Lino's avatar
João Lino committed
274
  // ++++++++++++++++++++++++ Temperature Sensor PT100 ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
275

João Lino's avatar
João Lino committed
276 277 278 279 280
  // ++++++++++++++++++++++++ Serial Monitor ++++++++++++++++++++++++
  Serial.begin                    (SETTING_SERIAL_MONITOR_BAUD_RATE);    // setup terminal baud rate
  Serial.println                  (SETTING_SERIAL_MONITOR_WELCOME_MESSAGE);  // print a start message to the terminal

  // ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
281 282
  lcd.begin                       (LCD_HORIZONTAL_RESOLUTION, LCD_VERTICAL_RESOLUTION);   //  <<----- My LCD was 16x2
  lcd.setBacklightPin             (LCD_BACKLIGHT_PIN, POSITIVE);       // Setup backlight pin
João Lino's avatar
João Lino committed
283 284 285 286 287 288
  lcd.setBacklight                (HIGH);              // Switch on the backlight

  // ######################### INITIALIZE #########################
  // ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
  // set operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
  xSetupRotaryEncoder             ( eRotaryEncoderMode_Disabled, 0, 0, 0, 0, 0 );
João Lino's avatar
João Lino committed
289
  rotarySwDetectTime = 0;
João Lino's avatar
João Lino committed
290 291

  // ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
292
  eMenuType                   =   MENU_INIT;
João Lino's avatar
João Lino committed
293

João Lino's avatar
João Lino committed
294 295 296 297
  cookingStage                =   SETTING_COOKING_STAGE_INIT;
  beerProfile                 =   SETTING_BEER_PROFILE_INIT;

  cancel                      =   false;
João Lino's avatar
João Lino committed
298
  // ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
João Lino's avatar
Rel.3  
João Lino committed
299

João Lino's avatar
João Lino committed
300
  cooking                     =   false;
301

João Lino's avatar
João Lino committed
302
  clockStartTime              =   0;
João Lino's avatar
Rel.3  
João Lino committed
303
  clockLastUpdate             =   0;
João Lino's avatar
João Lino committed
304 305 306 307
  clockCounter                =   0;
  clockIgnore                 =   0;
  clockStart                  =   false;
  clockEnd                    =   false;
308

João Lino's avatar
João Lino committed
309 310
  cookTime                    =   3600;
  cookTemperature             =   25;
João Lino's avatar
João Lino committed
311
  finalYield                  =   SETTING_MACHINE_YIELD_DEFAULT;
João Lino's avatar
João Lino committed
312

João Lino's avatar
João Lino committed
313 314 315 316 317 318 319 320 321 322 323
  startpointTime              =   PROFILE_BASIC_STARTPOINT_TIME;
  betaGlucanaseTime           =   PROFILE_BASIC_BETAGLUCANASE_TIME;
  debranchingTime             =   PROFILE_BASIC_DEBRANCHING_TIME;
  proteolyticTime             =   PROFILE_BASIC_PROTEOLYTIC_TIME;
  betaAmylaseTime             =   PROFILE_BASIC_BETAAMYLASE_TIME;
  alphaAmylaseTime            =   PROFILE_BASIC_ALPHAAMYLASE_TIME;
  mashoutTime                 =   PROFILE_BASIC_MASHOUT_TIME;
  recirculationTime           =   PROFILE_BASIC_RECIRCULATION_TIME;
  spargeTime                  =   PROFILE_BASIC_SPARGE_TIME;
  boilTime                    =   PROFILE_BASIC_BOIL_TIME;
  coolingTime                 =   PROFILE_BASIC_COOLING_TIME;
João Lino's avatar
Rel.3  
João Lino committed
324
  cleaningTime                =   SETTING_CLEANING_TIME;
João Lino's avatar
João Lino committed
325

João Lino's avatar
João Lino committed
326 327 328 329 330 331 332 333 334 335 336
  startpointTemperature       =   PROFILE_BASIC_STARTPOINT_TEMPERATURE;
  betaGlucanaseTemperature    =   PROFILE_BASIC_BETAGLUCANASE_TEMPERATURE;
  debranchingTemperature      =   PROFILE_BASIC_DEBRANCHING_TEMPERATURE;
  proteolyticTemperature      =   PROFILE_BASIC_PROTEOLYTIC_TEMPERATURE;
  betaAmylaseTemperature      =   PROFILE_BASIC_BETAAMYLASE_TEMPERATURE;
  alphaAmylaseTemperature     =   PROFILE_BASIC_ALPHAAMYLASE_TEMPERATURE;
  mashoutTemperature          =   PROFILE_BASIC_MASHOUT_TEMPERATURE;
  recirculationTemperature    =   PROFILE_BASIC_RECIRCULATION_TEMPERATURE;
  spargeTemperature           =   PROFILE_BASIC_SPARGE_TEMPERATURE;
  boilTemperature             =   PROFILE_BASIC_BOIL_TEMPERATURE;
  coolingTemperature          =   PROFILE_BASIC_COOLING_TEMPERATURE;
João Lino's avatar
Rel.3  
João Lino committed
337
  cleaningTemperature         =   SETTING_CLEANING_TEMPERATURE;
João Lino's avatar
João Lino committed
338 339 340 341

  refresh                     =   true;
  repaint                     =   true;

João Lino's avatar
João Lino committed
342 343
  loggingTimeInterval         =   0;

João Lino's avatar
João Lino committed
344 345 346 347 348
  // ++++++++++++++++++++++++ Interrupts ++++++++++++++++++++++++
  lastInterruptTime           =   0;

  // ++++++++++++++++++++++++ PID  ++++++++++++++++++++++++
  iWindowSize                 =   HEATING_ELEMENT_DEFAULT_WINDOW_SIZE;    // Time frame to operate in
349

João Lino's avatar
Rel.3  
João Lino committed
350
  // ######################### Code - Run Once #########################
João Lino's avatar
João Lino committed
351
  xSafeHardwarePowerOff           ();
João Lino's avatar
Rel.3  
João Lino committed
352
  xWelcomeUser                    ();
353

João Lino's avatar
Rel.3  
João Lino committed
354
  xSetupRotaryEncoder             ( eRotaryEncoderMode_Menu, eMainMenu_GO, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );
355
}
356

357
// ######################### MAIN LOOP #########################
358 359

void loop() {
João Lino's avatar
João Lino committed
360
  unsigned long inactivityTime = getInactivityTime();
361

362 363
  if ( inactivityTime > SETTING_MAX_INACTIVITY_TIME ) {   // Inactivity check
    if (refresh) {
364 365 366
      repaint = true;
      refresh = false;
    }
João Lino's avatar
Rel.3  
João Lino committed
367
    repaint = displayStatus( &lcd, cooking, cookTemperature, basePT100.getCurrentTemperature(), upPT100.getCurrentTemperature(), downPT100.getCurrentTemperature(), clockCounter, repaint );
368 369
  }
  else {
João Lino's avatar
Rel.3  
João Lino committed
370
    runMenu();
371
  }
372

João Lino's avatar
Rel.3  
João Lino committed
373
  xManageMachineSystems();
374 375
}

376 377
// ######################### FUNCTIONS ########################

João Lino's avatar
João Lino committed
378 379
void xCountTheTime( int temperatureMarginRange, boolean bMaximumOfUpDown ) {
  unsigned long now = millis();
João Lino's avatar
João Lino committed
380

João Lino's avatar
João Lino committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394
  // Get current maximum sensed temperaure
  double temperatureCount = 0;
  if ( bMaximumOfUpDown ) {
    float tup = upPT100.getCurrentTemperature();
    float tdown = downPT100.getCurrentTemperature();
    if (tup > tdown) {
      temperatureCount = tdown;
    }
    else {
      temperatureCount = tup;
    }
  } else {
    temperatureCount = basePT100.getCurrentTemperature();
  }
João Lino's avatar
João Lino committed
395

João Lino's avatar
João Lino committed
396 397 398 399 400
  // Ignote time ticks if temperature is not within the acceptable margin for this stage
  unsigned long elapsedTime = now - clockLastUpdate;
  if ( temperatureCount < (cookTemperature - temperatureMarginRange) ) {
    clockIgnore += elapsedTime;
  }
João Lino's avatar
João Lino committed
401

João Lino's avatar
João Lino committed
402 403
  // Calculate the remaining time on the clock
  clockCounter = cookTime * 1000 - (elapsedTime - clockIgnore);
404

João Lino's avatar
João Lino committed
405 406 407 408
  // Don't let clock get bellow 0
  if ( clockCounter < 0 ) {
    clockCounter = 0;
  }
João Lino's avatar
João Lino committed
409

João Lino's avatar
João Lino committed
410
  clockLastUpdate = now;
João Lino's avatar
João Lino committed
411

João Lino's avatar
João Lino committed
412 413 414 415 416 417 418 419 420
#ifdef DEBUG_OFF
  debugPrintFunction("xCountTheTime");
  debugPrintVar("millis()", now);
  debugPrintVar("cookTime", cookTime);
  debugPrintVar("clockStartTime", clockStartTime);
  debugPrintVar("clockIgnore", clockIgnore);
  debugPrintVar("clockCounter", clockCounter);
#endif
}
421

João Lino's avatar
João Lino committed
422 423 424 425 426 427
bool isTimeLeft() {
  if ( clockCounter > 0 ) {
    return true;
  }
  return false;
}
João Lino's avatar
João Lino committed
428

João Lino's avatar
João Lino committed
429 430 431 432 433
//HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ
double ulWattToWindowTime( double ulAppliedWatts ) {
  double ulPulsesRequired = ulAppliedWatts / dWattPerPulse;
  return (double)iWindowSize / 1000.0 * ulPulsesRequired * 1000.0 / HEATING_ELEMENT_AC_FREQUENCY_HZ;
}
434

João Lino's avatar
João Lino committed
435 436 437 438
bool xRegulateTemperature( boolean bMaximumOfUpDown ) {
  double difference = 0;
  bool overTemperature = false;
  double wattage = 0.0;
439

João Lino's avatar
João Lino committed
440 441 442
  float tup = upPT100.getCurrentTemperature();
  float tdown = downPT100.getCurrentTemperature();
  float tbase = basePT100.getCurrentTemperature();
443

João Lino's avatar
João Lino committed
444 445 446 447 448 449 450
  if ( bMaximumOfUpDown ) {
    if (tup > tdown) {
      difference = cookTemperature - tup;
    }
    else {
      difference = cookTemperature - tdown;
    }
451

João Lino's avatar
João Lino committed
452 453 454
    if (tbase > cookTemperature && (tbase >= (PUMP_TEMPERATURE_MAX_OPERATION - 2.0) || difference >= 5.0)) {
      difference = cookTemperature - tbase;
    }
455

João Lino's avatar
João Lino committed
456 457 458 459 460 461
    if ( (tbase < cookTemperature) && (difference < (cookTemperature - tbase)) ) {
      difference = cookTemperature - tbase;
    }
  } else {
    difference = cookTemperature - tbase;
  }
462

João Lino's avatar
João Lino committed
463 464 465 466 467
  // Deviation between the cook temperature set and the cook temperature measured
  if ( difference < 0.0 ) {
    difference = difference * (-1.0);
    overTemperature = true;
  }
468

João Lino's avatar
João Lino committed
469 470
  // Calculate applied wattage, based on the distance from the target temperature
  if ( overTemperature ) {
João Lino's avatar
João Lino committed
471 472 473 474 475 476
    wattage = 0.0;                      // turn it off
  }
  else {
    if ( difference <= 0.5 ) {
      if ( cookTemperature > 99.0 ) {
        wattage = 2000.0;               // pulse hardly at 2000 watt
João Lino's avatar
João Lino committed
477 478
      }
      else {
João Lino's avatar
João Lino committed
479 480
        if ( cookTemperature > 70.0 ) {
          wattage = 1000.0;             // pulse moderately at 1000 watt
João Lino's avatar
João Lino committed
481 482
        }
        else {
João Lino's avatar
João Lino committed
483
          wattage = 500.0;              // pulse lightly at 500 watt
484
        }
João Lino's avatar
Rel.3  
João Lino committed
485
      }
João Lino's avatar
João Lino committed
486 487 488 489 490
    }
    else {
      if ( difference <= 1.0 ) {
        if ( cookTemperature > 99.0 ) {
          wattage = 2000.0;             // pulse hardly at 2000 watt
João Lino's avatar
João Lino committed
491 492
        }
        else {
João Lino's avatar
João Lino committed
493
          wattage = 1000.0;             // pulse moderately at 1000 watt
João Lino's avatar
João Lino committed
494
        }
João Lino's avatar
João Lino committed
495 496 497 498 499 500 501
      }
      else {
        if ( difference <= 3.0 ) {
          wattage = 2000.0;             // pulse hardly at 2000 watt
        }
        else {
          wattage = HEATING_ELEMENT_MAX_WATTAGE;  // pulse constantly at HEATING_ELEMENT_MAX_WATTAGE watt
502
        }
João Lino's avatar
João Lino committed
503 504 505
      }
    }
  }
506

João Lino's avatar
João Lino committed
507 508 509 510
  // Update the recorded time for the begining of the window, if the previous window has passed
  while ((millis() - windowStartTime) > iWindowSize) { // Check if it's time to vary the pulse width modulation and if so do it by shifting the "Relay in ON" Window
    windowStartTime += iWindowSize;
  }
511

João Lino's avatar
João Lino committed
512 513 514 515 516 517 518
  // Apply wattage to the element at the right time
  if ( ulWattToWindowTime( wattage ) > (millis() - windowStartTime) ) {
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN, HIGH);
    bStatusElement = true;
  } else {
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN, LOW);
    bStatusElement = false;
519
  }
João Lino's avatar
Rel.3  
João Lino committed
520

521
#ifdef DEBUG_OFF
João Lino's avatar
João Lino committed
522 523 524 525 526 527 528 529
  //debugPrintFunction("xRegulateTemperature");
  debugPrintVar("difference", difference);
  //debugPrintVar("overTemperature", overTemperature);
  debugPrintVar("wattage", wattage);
  //debugPrintVar("ulWattToWindowTime( wattage )", ulWattToWindowTime( wattage ) );
  //debugPrintVar("millis()", millis());
  //debugPrintVar("windowStartTime", windowStartTime);
  //debugPrintVar("test", ulWattToWindowTime( wattage ) > (millis() - windowStartTime) );
530
#endif
531 532
}

João Lino's avatar
João Lino committed
533 534 535 536 537 538 539 540
void xPurgePump() {
  for (int i = 0; i < 2; i++) {
    analogWrite(PUMP_PIN, PUMP_SPEED_MAX_MOSFET);  // analogWrite values from 0 to 255
    delay(1000);
    analogWrite(PUMP_PIN, PUMP_SPEED_STOP_MOSFET);  // analogWrite values from 0 to 255
    delay(1500);
  }
}
João Lino's avatar
Rel.3  
João Lino committed
541

João Lino's avatar
João Lino committed
542 543
bool xRegulatePumpSpeed() {
  //  analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255
544

João Lino's avatar
João Lino committed
545 546
  if (basePT100.getCurrentTemperature() > PUMP_TEMPERATURE_MAX_OPERATION) {
    analogWrite(PUMP_PIN, PUMP_SPEED_STOP_MOSFET);  // analogWrite values from 0 to 255
João Lino's avatar
Rel.3  
João Lino committed
547

João Lino's avatar
João Lino committed
548 549 550 551 552 553
    basePT100.setPumpStatus( false );
    upPT100.setPumpStatus( false );
    downPT100.setPumpStatus( false );
  }
  else {
    analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255
554

João Lino's avatar
João Lino committed
555 556 557
    basePT100.setPumpStatus( true );
    upPT100.setPumpStatus( true );
    downPT100.setPumpStatus( true );
João Lino's avatar
Rel.3  
João Lino committed
558
  }
João Lino's avatar
João Lino committed
559
}
João Lino's avatar
Rel.3  
João Lino committed
560

João Lino's avatar
João Lino committed
561 562
void xWarnClockEnded() {
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);
João Lino's avatar
Rel.3  
João Lino committed
563 564
}

João Lino's avatar
João Lino committed
565 566 567
void xWarnCookEnded() {
  sing(MELODY_UNDERWORLD_SHORT, PIEZO_PIN);
}
João Lino's avatar
Rel.3  
João Lino committed
568

João Lino's avatar
João Lino committed
569 570 571 572 573
void xPrepareForStage( int stageTime, int stageTemperature, int stagePumpSpeed, eCookingStages stage ) {
#ifdef DEBUG_OFF
  debugPrintFunction("xPrepareForStage");
  debugPrintVar("cookingStage", stage);
#endif
574

João Lino's avatar
João Lino committed
575 576 577 578 579
  // Reset the clock
  unsigned long now = millis();
  clockStartTime  = now;
  clockLastUpdate = now;
  clockIgnore     = 0;
João Lino's avatar
Rel.3  
João Lino committed
580

João Lino's avatar
João Lino committed
581 582 583 584
  iPumpSpeed      = stagePumpSpeed;         // Set the pump speed
  cookingStage    = stage;                  // Set Stage
  cookTime        = stageTime;              // Set the clock
  cookTemperature = stageTemperature;       // Set the target temperature
João Lino's avatar
Rel.3  
João Lino committed
585 586
}

João Lino's avatar
João Lino committed
587 588 589 590 591
void xSetupStage(eCookingStages nextStage) {
#ifdef DEBUG_OFF
  debugPrintFunction("xSetupStage");
  debugPrintVar("cookingStage", nextStage);
#endif
João Lino's avatar
Rel.3  
João Lino committed
592

João Lino's avatar
João Lino committed
593 594 595 596 597 598 599 600 601 602 603 604
  // Operate the machine according to the current mode
  switch (nextStage) {
    case eCookingStage_Startpoint: {
        switch (beerProfile) {
          case eBeerProfile_Trigo: {
              float wheatAmount = 0.05 * ((float) finalYield);
              float pilsnerAmount = 0.2 * ((float) finalYield);
              String say = "Cruch ";
              say += String(wheatAmount);
              say += String("Kg of Wheat and ");
              say += String(pilsnerAmount);
              say += String("Kg of Pilsner Malt into a pot.");
605

João Lino's avatar
João Lino committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
              xWaitForAction("Malt", say);
              repaint = true;
              break;
            }
          case eBeerProfile_IPA: {
              float caramelAmount = 0.013157895 * ((float) finalYield);
              float wheatAmount = 0.060526316 * ((float) finalYield);
              float pilsnerAmount = 0.115789474 * ((float) finalYield);
              float munichAmount = 0.028947368 * ((float) finalYield);
              String say = "Cruch ";
              say += String(caramelAmount);
              say += String("Kg of Caramel 120, ");
              say += String(wheatAmount);
              say += String("Kg of Wheat, ");
              say += String(pilsnerAmount);
              say += String("Kg of Pilsner, ");
              say += String(munichAmount);
              say += String("Kg of Munich into a pot.");
624

João Lino's avatar
João Lino committed
625 626 627 628 629
              xWaitForAction("Malt", say);
              repaint = true;
              break;
            }
          default: {
João Lino's avatar
Rel.3  
João Lino committed
630

João Lino's avatar
João Lino committed
631 632
            }
        }
633

João Lino's avatar
João Lino committed
634 635
        // Make sure there is water
        xWaitForAction("Water", "Make sure there is water in the machine before start cooking.");
636

João Lino's avatar
João Lino committed
637 638
        repaint = true;
        xPrepareForStage( startpointTime, startpointTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Startpoint );
639 640
        break;
      }
João Lino's avatar
João Lino committed
641 642 643 644 645 646 647 648 649 650
    case eCookingStage_BetaGlucanase: {
        switch (beerProfile) {
          case eBeerProfile_Trigo: {
              float wheatAmount = 0.05 * ((float) finalYield);
              float pilsnerAmount = 0.2 * ((float) finalYield);
              String say = "Put ";
              say += String(wheatAmount);
              say += String("Kg of Wheat and ");
              say += String(pilsnerAmount);
              say += String("Kg of Pilsner Malt in.");
651

João Lino's avatar
João Lino committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
              xWaitForAction("Malt", say);
              repaint = true;
              break;
            }
          case eBeerProfile_IPA: {
              float caramelAmount = 0.013157895 * ((float) finalYield);
              float wheatAmount = 0.060526316 * ((float) finalYield);
              float pilsnerAmount = 0.115789474 * ((float) finalYield);
              float munichAmount = 0.028947368 * ((float) finalYield);
              String say = "Cruch ";
              say += String(caramelAmount);
              say += String("Kg of Caramel 120, ");
              say += String(wheatAmount);
              say += String("Kg of Wheat, ");
              say += String(pilsnerAmount);
              say += String("Kg of Pilsner, ");
              say += String(munichAmount);
              say += String("Kg of Munich into a pot.");
670

João Lino's avatar
João Lino committed
671 672 673 674 675 676 677
              xWaitForAction("Malt", say);
              repaint = true;
              break;
            }
          default: {}
        }
        xPrepareForStage( betaGlucanaseTime, betaGlucanaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_BetaGlucanase );
678 679
        break;
      }
João Lino's avatar
João Lino committed
680 681
    case eCookingStage_Debranching: {
        xPrepareForStage( debranchingTime, debranchingTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Debranching );
682 683
        break;
      }
João Lino's avatar
João Lino committed
684 685
    case eCookingStage_Proteolytic: {
        xPrepareForStage( proteolyticTime, proteolyticTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Proteolytic );
686 687
        break;
      }
João Lino's avatar
João Lino committed
688 689
    case eCookingStage_BetaAmylase: {
        xPrepareForStage( betaAmylaseTime, betaAmylaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_BetaAmylase );
690 691
        break;
      }
João Lino's avatar
João Lino committed
692 693
    case eCookingStage_AlphaAmylase: {
        xPrepareForStage( alphaAmylaseTime, alphaAmylaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_AlphaAmylase );
694 695
        break;
      }
João Lino's avatar
João Lino committed
696 697
    case eCookingStage_Mashout: {
        xPrepareForStage( mashoutTime, mashoutTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Mashout );
698 699
        break;
      }
João Lino's avatar
João Lino committed
700 701 702 703
    case eCookingStage_Recirculation: {
        xWaitForAction("Sparge Water", "Start heating your sparge water.");
        repaint = true;
        xPrepareForStage( recirculationTime, recirculationTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Recirculation );
704 705
        break;
      }
João Lino's avatar
João Lino committed
706 707 708 709
    case eCookingStage_Sparge: {
        xWaitForAction("Sparge Water", "Start pouring the sparge water.");
        repaint = true;
        xPrepareForStage( spargeTime, spargeTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Sparge );
710 711
        break;
      }
João Lino's avatar
João Lino committed
712 713 714 715
    case eCookingStage_Boil: {
        switch (beerProfile) {
          case eBeerProfile_Trigo: {
              String say = "Get ";
716

João Lino's avatar
João Lino committed
717 718
              float hopAmount = 0.8 * ((float) finalYield);
              say += String(hopAmount);
719

João Lino's avatar
João Lino committed
720
              say += String("g of Magnum 9.4\% and Styrian Golding 5\% ready.");
João Lino's avatar
Rel.3  
João Lino committed
721

João Lino's avatar
João Lino committed
722
              xWaitForAction("Hops", say);
723

João Lino's avatar
João Lino committed
724 725 726 727
              break;
            }
          case eBeerProfile_IPA: {
              String say = "Get ";
João Lino's avatar
Rel.3  
João Lino committed
728

João Lino's avatar
João Lino committed
729 730
              float hopAmount = 0.8 * ((float) finalYield);
              say += String(hopAmount);
João Lino's avatar
Rel.3  
João Lino committed
731

João Lino's avatar
João Lino committed
732
              say += String("g of Chinook, Cascade and Styrian Golding ready.");
733

João Lino's avatar
João Lino committed
734
              xWaitForAction("Hops", say);
735

João Lino's avatar
João Lino committed
736 737 738 739
              break;
            }
          default: {
              xWaitForAction("Hops", "Add the hops in the right order, at the right time.");
740

João Lino's avatar
João Lino committed
741 742
            }
        }
743

João Lino's avatar
João Lino committed
744
        repaint = true;
745

João Lino's avatar
João Lino committed
746 747
        // A basic operation for a basic stage
        xPrepareForStage( boilTime, boilTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Boil );
João Lino's avatar
Rel.3  
João Lino committed
748

749 750
        break;
      }
João Lino's avatar
João Lino committed
751 752 753
    case eCookingStage_Cooling: {
        // Make sure there is water
        xWaitForAction("Coil", "Add the coil and connect it to the main water supply.");
754

João Lino's avatar
João Lino committed
755 756 757 758
        repaint = true;

        // A basic operation for a basic stage
        xPrepareForStage( coolingTime, coolingTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Cooling );
759 760 761

        break;
      }
João Lino's avatar
João Lino committed
762 763 764
    case eCookingStage_Clean: {
        // Make sure there is water
        xWaitForAction("Water", "Add 13 liters.");
765

João Lino's avatar
João Lino committed
766 767
        // Make sure there is water
        xWaitForAction("Star San HB", "Add 0.89oz/26ml.");
768

João Lino's avatar
João Lino committed
769
        repaint = true;
770

João Lino's avatar
João Lino committed
771 772
        // A basic operation for a basic stage
        xPrepareForStage( cleaningTime, cleaningTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Clean );
773 774 775

        break;
      }
João Lino's avatar
João Lino committed
776 777 778
    case eCookingStage_Purge: {
        // A basic operation for a basic stage
        xPrepareForStage( 0, 0, PUMP_SPEED_MAX_MOSFET, eCookingStage_Purge );
779

João Lino's avatar
João Lino committed
780
        xRegulatePumpSpeed();
781 782 783

        break;
      }
João Lino's avatar
João Lino committed
784 785 786
    case eCookingStage_Done: {
        // A basic operation for a basic stage
        xPrepareForStage( 0, 0, PUMP_SPEED_STOP_MOSFET, eCookingStage_Done );
787 788 789

        break;
      }
790
  }
791 792
}

João Lino's avatar
João Lino committed
793 794 795
void xTransitionIntoStage(eCookingStages nextStage) {
  // Turn off all hardware that can damage itself if the machine is not cooking
  xSafeHardwarePowerOff();
796

João Lino's avatar
João Lino committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  // Warn the user a stage has ended
  xWarnClockEnded();

  // Reset global stage variables
  xSetupStage( nextStage );
}

void xBasicStageOperation( int iStageTime, int iStageTemperature, int iStageTemperatureRange, eCookingStages nextStage, boolean bMaximumOfUpDown ) {

  // Account for time spent at the target temperature | Input 1: range in ºC within which the target temperature is considered to be reached
#ifdef DEBUG_OFF
  xCountTheTime( iStageTemperatureRange, false );
#else
  xCountTheTime( iStageTemperatureRange, bMaximumOfUpDown );
#endif

  if ( isTimeLeft() ) {
    // Do temperature control
    xRegulateTemperature( bMaximumOfUpDown );

    // Do flow control
    xRegulatePumpSpeed();

  } else {
#ifdef DEBUG_OFF
    debugPrintFunction("xBasicStageOperation");
    debugPrintVar("clockCounter", clockCounter);
#endif

    // Continue to the next stage, there is nothing to do, in this stage
    xTransitionIntoStage( nextStage );
    return;
829
  }
João Lino's avatar
João Lino committed
830 831 832 833 834 835 836 837 838

  return;
}

void xManageMachineSystems() {
  // Measure temperature, for effect
  basePT100.measure(false);
  upPT100.measure(false);
  downPT100.measure(true);
João Lino's avatar
João Lino committed
839

João Lino's avatar
João Lino committed
840 841 842
  // If cooking is done, return (this is a nice place to double check safety and ensure the cooking parts aren't on.
  if (!cooking) {
    xSafeHardwarePowerOff();
João Lino's avatar
João Lino committed
843

João Lino's avatar
João Lino committed
844 845
    return;
  }
João Lino's avatar
João Lino committed
846

João Lino's avatar
João Lino committed
847 848 849 850
  // Operate the machine according to the current mode
  switch (cookingStage) {
    case eCookingStage_Startpoint: {
        xBasicStageOperation( startpointTime, startpointTemperature, 0, eCookingStage_BetaGlucanase, false);
851 852
        break;
      }
João Lino's avatar
João Lino committed
853 854
    case eCookingStage_BetaGlucanase: {
        xBasicStageOperation( betaGlucanaseTime, betaGlucanaseTemperature, 3, eCookingStage_Debranching, true );
855 856
        break;
      }
João Lino's avatar
João Lino committed
857 858
    case eCookingStage_Debranching: {
        xBasicStageOperation( debranchingTime, debranchingTemperature, 3, eCookingStage_Proteolytic, true );
859 860
        break;
      }
João Lino's avatar
João Lino committed
861 862
    case eCookingStage_Proteolytic: {
        xBasicStageOperation( proteolyticTime, proteolyticTemperature, 3, eCookingStage_BetaAmylase, true );
863 864
        break;
      }
João Lino's avatar
João Lino committed
865 866
    case eCookingStage_BetaAmylase: {
        xBasicStageOperation( betaAmylaseTime, betaAmylaseTemperature, 4, eCookingStage_AlphaAmylase, true );
867 868
        break;
      }
João Lino's avatar
João Lino committed
869 870
    case eCookingStage_AlphaAmylase: {
        xBasicStageOperation( alphaAmylaseTime, alphaAmylaseTemperature, 2, eCookingStage_Mashout, true );
871 872
        break;
      }
João Lino's avatar
João Lino committed
873 874
    case eCookingStage_Mashout: {
        xBasicStageOperation( mashoutTime, mashoutTemperature, 1, eCookingStage_Recirculation, true );
875 876
        break;
      }
João Lino's avatar
João Lino committed
877 878
    case eCookingStage_Recirculation: {
        xBasicStageOperation( recirculationTime, recirculationTemperature, 1, eCookingStage_Sparge, true );
879 880
        break;
      }
João Lino's avatar
João Lino committed
881 882
    case eCookingStage_Sparge: {
        xBasicStageOperation( spargeTime, spargeTemperature, 3, eCookingStage_Boil, false );
883 884
        break;
      }
João Lino's avatar
João Lino committed
885 886
    case eCookingStage_Boil: {
        xBasicStageOperation( boilTime, boilTemperature, 2, eCookingStage_Cooling, false );
887 888
        break;
      }
João Lino's avatar
João Lino committed
889 890
    case eCookingStage_Cooling: {
        xBasicStageOperation( coolingTime, coolingTemperature, 0, eCookingStage_Done, false );
891 892
        break;
      }
João Lino's avatar
João Lino committed
893 894
    case eCookingStage_Clean: {
        xBasicStageOperation( cleaningTime, cleaningTemperature, 0, eCookingStage_Done, false );
895 896
        break;
      }
João Lino's avatar
João Lino committed
897
    case eCookingStage_Purge: {
João Lino's avatar
João Lino committed
898 899 900 901
        iPumpSpeed = PUMP_SPEED_MAX_MOSFET;
        xRegulatePumpSpeed();
        break;
      }
João Lino's avatar
João Lino committed
902
    case eCookingStage_Done: {
João Lino's avatar
João Lino committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
        stopBrewing();                  // Update cooking state
        repaint = true;                 // Ask for screen refresh
        xWarnCookEnded();               // Warn the user that the cooking is done
        break;
      }
  }

#ifdef INFO
  unsigned long now = millis();
  if( now - loggingTimeInterval > SETTING_MACHINE_LOGGING_INTERVAL ) {
    loggingTimeInterval = now;
    Serial.print(now);
    Serial.print("|");
    Serial.print(clockCounter);
    Serial.print("|");
    if (cooking) {
      Serial.print("1");
    }
    else {
      Serial.print("0");
João Lino's avatar
João Lino committed
923
    }
João Lino's avatar
João Lino committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    Serial.print("|");
    Serial.print(cookTemperature);
    Serial.print("|");
    Serial.print(basePT100.getCurrentTemperature());
    Serial.print("|");
    Serial.print(upPT100.getCurrentTemperature());
    Serial.print("|");
    Serial.print(downPT100.getCurrentTemperature());
    Serial.print("|");
    if (bStatusElement) {
      Serial.print("1");
    }
    else {
      Serial.print("0");
    }
    Serial.print("|");
João Lino's avatar
João Lino committed
940
  }
João Lino's avatar
João Lino committed
941 942 943

  
#endif
João Lino's avatar
João Lino committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
}

// ##################################################### Menus ###################################################################

// *************************** MENU BASE *********************************
void runMenu() {
#ifdef DEBUG_OFF
  boolean debug_go = repaint;
  if (debug_go) {
    debugPrintFunction("runMenu");
    debugPrintVar("repaint", repaint);
    debugPrintVar("eMenuType", eMenuType);
    debugPrintVar("rotaryEncoderVirtualPosition", rotaryEncoderVirtualPosition);
  }
#endif

  switch (eMenuType) {
    case eMenuType_Main: {
João Lino's avatar
João Lino committed
962 963 964
        runMenuProcessor( &mdMainMenu );
        break;
      }
João Lino's avatar
João Lino committed
965
    case eMenuType_BeerProfile: {
João Lino's avatar
João Lino committed
966 967 968
        runMenuProcessor( &mdBeerProfileMenu );
        break;
      }
João Lino's avatar
João Lino committed
969
    case eMenuType_Stage: {
João Lino's avatar
João Lino committed
970 971 972
        runMenuProcessor( &mdStageMenu );
        break;
      }
João Lino's avatar
João Lino committed
973
    case eMenuType_Malt: {
João Lino's avatar
João Lino committed
974 975 976
        runMenuProcessor( &mdMaltMenu );
        break;
      }
João Lino's avatar
João Lino committed
977
    case eMenuType_Settings: {
João Lino's avatar
João Lino committed
978 979 980
        runMenuProcessor( &mdSettingsMenu );
        break;
      }
João Lino's avatar
João Lino committed
981
    case eMenuType_StartFromStage: {
João Lino's avatar
João Lino committed
982 983 984
        runMenuProcessor( &mdStartFromStageMenu );
        break;
      }
João Lino's avatar
João Lino committed
985 986 987 988 989
  }

#ifdef DEBUG_OFF
  if (debug_go) {
    debugPrintVar("repaint", repaint);
João Lino's avatar
João Lino committed
990
  }
João Lino's avatar
João Lino committed
991
#endif
João Lino's avatar
João Lino committed
992 993
}

João Lino's avatar
João Lino committed
994
// ************************ MENU SELECTIONS ******************************
João Lino's avatar
Rel.3  
João Lino committed
995
void runMainMenuSelection() {
996
  switch (mdMainMenu._selection) {
João Lino's avatar
João Lino committed
997
    //switch (eMainMenuSelection) {
João Lino's avatar
João Lino committed
998
    case eMainMenu_GO: {
999 1000 1001
        xStartStage( NULL, NULL, eCookingStage_Startpoint, true, true, false, false );
        break;
      }
João Lino's avatar
João Lino committed
1002
    case eMainMenu_GO_FROM_STAGE: {
1003 1004
        eMenuType = eMenuType_StartFromStage;
        repaint = true;
João Lino's avatar
João Lino committed
1005
        xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdBeerProfileMenu._position, MENU_SIZE_PROFILES_MENU - 1, 1, 1, 0 );
1006 1007
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1008
    case eMainMenu_STOP: {
1009 1010 1011 1012
        stopBrewing();
        backToStatus();
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1013
    case eMainMenu_SKIP: {
1014 1015 1016 1017
        cookTime = 0;
        backToStatus();
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1018
    case eMainMenu_BeerProfile: {
1019 1020
        eMenuType = eMenuType_BeerProfile;
        repaint = true;
João Lino's avatar
João Lino committed
1021
        xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdBeerProfileMenu._position, MENU_SIZE_PROFILES_MENU - 1, 1, 1, 0 );
1022 1023 1024 1025 1026
        break;
      }
    case eMainMenu_Stage: {
        eMenuType = eMenuType_Stage;
        repaint = true;
João Lino's avatar
João Lino committed
1027
        xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdStageMenu._position, MENU_SIZE_STAGE_MENU - 1, 1, 1, 0 );
1028 1029
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1030
    case eMainMenu_Malt: {
1031 1032
        eMenuType = eMenuType_Malt;
        repaint = true;
João Lino's avatar
João Lino committed
1033
        xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdMaltMenu._position, MENU_SIZE_MALT_MENU - 1, 1, 1, 0 );
1034 1035
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1036
    case eMainMenu_Hops: {
1037 1038 1039
        backToStatus();
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1040
    case eMainMenu_Clean: {
1041 1042 1043
        xStartStageHeadless( eCookingStage_Clean, true );
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1044
    case eMainMenu_Purge: {
1045 1046 1047
        xStartStageHeadless( eCookingStage_Purge, true );
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1048
    case eMainMenu_Settings: {
1049 1050
        eMenuType = eMenuType_Settings;
        repaint = true;
João Lino's avatar
João Lino committed
1051
        xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdSettingsMenu._position, MENU_SIZE_SETTINGS_MENU - 1, 1, 1, 0 );
1052 1053
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1054
    case eMainMenu_Back: {
1055 1056 1057
        backToStatus();
        break;
      }
João Lino's avatar
Rel.3  
João Lino committed
1058
    default: {
1059
      }
João Lino's avatar
Rel.3  
João Lino committed
1060
  }
1061
  mdMainMenu._selection = eMainMenu_NULL;
1062 1063
}

João Lino's avatar
João Lino committed
1064

João Lino's avatar
João Lino committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
void runStartFromStageSelection() {
  switch (mdStartFromStageMenu._selection) {
    case eStageMenu_Startpoint: {
        xStartStageInteractive( &startpointTime, &startpointTemperature, eCookingStage_Startpoint );
        break;
      }
    case eStageMenu_BetaGlucanase: {
        xStartStageInteractive( &betaGlucanaseTime, &betaGlucanaseTemperature, eCookingStage_BetaGlucanase );
        break;
      }
    case eStageMenu_Debranching: {
        xStartStageInteractive( &debranchingTime, &debranchingTemperature, eCookingStage_Debranching );
        break;
      }
    case eStageMenu_Proteolytic: {
        xStartStageInteractive( &proteolyticTime, &proteolyticTemperature, eCookingStage_Proteolytic );
        break;
      }
    case eStageMenu_BetaAmylase: {
        xStartStageInteractive( &betaAmylaseTime, &betaAmylaseTemperature, eCookingStage_BetaAmylase );
        break;
      }
    case eStageMenu_AlphaAmylase: {
        xStartStageInteractive( &alphaAmylaseTime, &alphaAmylaseTemperature, eCookingStage_AlphaAmylase );
        break;
      }
    case eStageMenu_Mashout: {
        xStartStageInteractive( &mashoutTime, &mashoutTemperature, eCookingStage_Mashout );
        break;
      }
    case eStageMenu_Recirculation: {
        xStartStageInteractive( &recirculationTime, &recirculationTemperature, eCookingStage_Recirculation );
        break;
      }
    case eStageMenu_Sparge: {
        xStartStageInteractive( &spargeTime, &spargeTemperature, eCookingStage_Sparge );
        break;
      }
    case eStageMenu_Boil: {
        xStartStageInteractive( &boilTime, &boilTemperature, eCookingStage_Boil );
        break;
      }
    case eStageMenu_Cooling: {
        xStartStageInteractive( &coolingTime, &coolingTemperature, eCookingStage_Cooling );
        break;
      }
    case eStageMenu_Back: {
        resetMenu( true );
        break;
      }
    default: {
      }
João Lino's avatar
João Lino committed
1117
  }
João Lino's avatar
João Lino committed
1118 1119
  mdStartFromStageMenu._selection = eStageMenu_NULL;
}
1120

João Lino's avatar
João Lino committed
1121 1122 1123
void runBeerProfileSelection() {
  switch (mdBeerProfileMenu._selection) {
    case eBeerProfileMenu_Basic: {
João Lino's avatar
João Lino committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
        beerProfile                 =   eBeerProfile_Basic;
        startpointTime              =   PROFILE_BASIC_STARTPOINT_TIME;
        betaGlucanaseTime           =   PROFILE_BASIC_BETAGLUCANASE_TIME;
        debranchingTime             =   PROFILE_BASIC_DEBRANCHING_TIME;
        proteolyticTime             =   PROFILE_BASIC_PROTEOLYTIC_TIME;
        betaAmylaseTime             =   PROFILE_BASIC_BETAAMYLASE_TIME;
        alphaAmylaseTime            =   PROFILE_BASIC_ALPHAAMYLASE_TIME;
        mashoutTime                 =   PROFILE_BASIC_MASHOUT_TIME;
        recirculationTime           =   PROFILE_BASIC_RECIRCULATION_TIME;
        spargeTime                  =   PROFILE_BASIC_SPARGE_TIME;
        boilTime                    =   PROFILE_BASIC_BOIL_TIME;
        coolingTime                 =   PROFILE_BASIC_COOLING_TIME;
        startpointTemperature       =   PROFILE_BASIC_STARTPOINT_TEMPERATURE;
        betaGlucanaseTemperature    =   PROFILE_BASIC_BETAGLUCANASE_TEMPERATURE;
        debranchingTemperature      =   PROFILE_BASIC_DEBRANCHING_TEMPERATURE;
        proteolyticTemperature      =   PROFILE_BASIC_PROTEOLYTIC_TEMPERATURE;
        betaAmylaseTemperature      =   PROFILE_BASIC_BETAAMYLASE_TEMPERATURE;
        alphaAmylaseTemperature     =   PROFILE_BASIC_ALPHAAMYLASE_TEMPERATURE;
        mashoutTemperature          =   PROFILE_BASIC_MASHOUT_TEMPERATURE;
        recirculationTemperature    =   PROFILE_BASIC_RECIRCULATION_TEMPERATURE;
        spargeTemperature           =   PROFILE_BASIC_SPARGE_TEMPERATURE;
        boilTemperature             =   PROFILE_BASIC_BOIL_TEMPERATURE;
        coolingTemperature          =   PROFILE_BASIC_COOLING_TEMPERATURE;

        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1151
    case eBeerProfileMenu_Trigo: {
João Lino's avatar
João Lino committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
        beerProfile                 =   eBeerProfile_Trigo;
        startpointTime              =   PROFILE_TRIGO_STARTPOINT_TIME;
        betaGlucanaseTime           =   PROFILE_TRIGO_BETAGLUCANASE_TIME;
        debranchingTime             =   PROFILE_TRIGO_DEBRANCHING_TIME;
        proteolyticTime             =   PROFILE_TRIGO_PROTEOLYTIC_TIME;
        betaAmylaseTime             =   PROFILE_TRIGO_BETAAMYLASE_TIME;
        alphaAmylaseTime            =   PROFILE_TRIGO_ALPHAAMYLASE_TIME;
        mashoutTime                 =   PROFILE_TRIGO_MASHOUT_TIME;
        recirculationTime           =   PROFILE_TRIGO_RECIRCULATION_TIME;
        spargeTime                  =   PROFILE_TRIGO_SPARGE_TIME;
        boilTime                    =   PROFILE_TRIGO_BOIL_TIME;
        coolingTime                 =   PROFILE_TRIGO_COOLING_TIME;
        startpointTemperature       =   PROFILE_TRIGO_STARTPOINT_TEMPERATURE;
        betaGlucanaseTemperature    =   PROFILE_TRIGO_BETAGLUCANASE_TEMPERATURE;
        debranchingTemperature      =   PROFILE_TRIGO_DEBRANCHING_TEMPERATURE;
        proteolyticTemperature      =   PROFILE_TRIGO_PROTEOLYTIC_TEMPERATURE;
        betaAmylaseTemperature      =   PROFILE_TRIGO_BETAAMYLASE_TEMPERATURE;
        alphaAmylaseTemperature     =   PROFILE_TRIGO_ALPHAAMYLASE_TEMPERATURE;
        mashoutTemperature          =   PROFILE_TRIGO_MASHOUT_TEMPERATURE;
        recirculationTemperature    =   PROFILE_TRIGO_RECIRCULATION_TEMPERATURE;
        spargeTemperature           =   PROFILE_TRIGO_SPARGE_TEMPERATURE;
        boilTemperature             =   PROFILE_TRIGO_BOIL_TEMPERATURE;
        coolingTemperature          =   PROFILE_TRIGO_COOLING_TEMPERATURE;
João Lino's avatar
João Lino committed
1175

João Lino's avatar
João Lino committed
1176 1177 1178
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1179
    case eBeerProfileMenu_IPA: {
João Lino's avatar
João Lino committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
        beerProfile                 =   eBeerProfile_IPA;
        startpointTime              =   PROFILE_IPA_STARTPOINT_TIME;
        betaGlucanaseTime           =   PROFILE_IPA_BETAGLUCANASE_TIME;
        debranchingTime             =   PROFILE_IPA_DEBRANCHING_TIME;
        proteolyticTime             =   PROFILE_IPA_PROTEOLYTIC_TIME;
        betaAmylaseTime             =   PROFILE_IPA_BETAAMYLASE_TIME;
        alphaAmylaseTime            =   PROFILE_IPA_ALPHAAMYLASE_TIME;
        mashoutTime                 =   PROFILE_IPA_MASHOUT_TIME;
        recirculationTime           =   PROFILE_IPA_RECIRCULATION_TIME;
        spargeTime                  =   PROFILE_IPA_SPARGE_TIME;
        boilTime                    =   PROFILE_IPA_BOIL_TIME;
        coolingTime                 =   PROFILE_IPA_COOLING_TIME;
        startpointTemperature       =   PROFILE_IPA_STARTPOINT_TEMPERATURE;
        betaGlucanaseTemperature    =   PROFILE_IPA_BETAGLUCANASE_TEMPERATURE;
        debranchingTemperature      =   PROFILE_IPA_DEBRANCHING_TEMPERATURE;
        proteolyticTemperature      =   PROFILE_IPA_PROTEOLYTIC_TEMPERATURE;
        betaAmylaseTemperature      =   PROFILE_IPA_BETAAMYLASE_TEMPERATURE;
        alphaAmylaseTemperature     =   PROFILE_IPA_ALPHAAMYLASE_TEMPERATURE;
        mashoutTemperature          =   PROFILE_IPA_MASHOUT_TEMPERATURE;
        recirculationTemperature    =   PROFILE_IPA_RECIRCULATION_TEMPERATURE;
        spargeTemperature           =   PROFILE_IPA_SPARGE_TEMPERATURE;
        boilTemperature             =   PROFILE_IPA_BOIL_TEMPERATURE;
        coolingTemperature          =   PROFILE_IPA_COOLING_TEMPERATURE;
João Lino's avatar
João Lino committed
1203

João Lino's avatar
João Lino committed
1204 1205 1206
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1207
    case eBeerProfileMenu_Belga: {
João Lino's avatar
João Lino committed
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
        beerProfile                 =   eBeerProfile_Belga;
        startpointTime              =   PROFILE_BELGA_STARTPOINT_TIME;
        betaGlucanaseTime           =   PROFILE_BELGA_BETAGLUCANASE_TIME;
        debranchingTime             =   PROFILE_BELGA_DEBRANCHING_TIME;
        proteolyticTime             =   PROFILE_BELGA_PROTEOLYTIC_TIME;
        betaAmylaseTime             =   PROFILE_BELGA_BETAAMYLASE_TIME;
        alphaAmylaseTime            =   PROFILE_BELGA_ALPHAAMYLASE_TIME;
        mashoutTime                 =   PROFILE_BELGA_MASHOUT_TIME;
        recirculationTime           =   PROFILE_BELGA_RECIRCULATION_TIME;
        spargeTime                  =   PROFILE_BELGA_SPARGE_TIME;
        boilTime                    =   PROFILE_BELGA_BOIL_TIME;
        coolingTime                 =   PROFILE_BELGA_COOLING_TIME;
        startpointTemperature       =   PROFILE_BELGA_STARTPOINT_TEMPERATURE;
        betaGlucanaseTemperature    =   PROFILE_BELGA_BETAGLUCANASE_TEMPERATURE;
        debranchingTemperature      =   PROFILE_BELGA_DEBRANCHING_TEMPERATURE;
        proteolyticTemperature      =   PROFILE_BELGA_PROTEOLYTIC_TEMPERATURE;
        betaAmylaseTemperature      =   PROFILE_BELGA_BETAAMYLASE_TEMPERATURE;
        alphaAmylaseTemperature     =   PROFILE_BELGA_ALPHAAMYLASE_TEMPERATURE;
        mashoutTemperature          =   PROFILE_BELGA_MASHOUT_TEMPERATURE;
        recirculationTemperature    =   PROFILE_BELGA_RECIRCULATION_TEMPERATURE;
        spargeTemperature           =   PROFILE_BELGA_SPARGE_TEMPERATURE;
        boilTemperature             =   PROFILE_BELGA_BOIL_TEMPERATURE;
        coolingTemperature          =   PROFILE_BELGA_COOLING_TEMPERATURE;
João Lino's avatar
João Lino committed
1231

João Lino's avatar
João Lino committed
1232 1233 1234
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1235
    case eBeerProfileMenu_Red: {
João Lino's avatar
João Lino committed
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
        beerProfile                 =   eBeerProfile_Red;
        startpointTime              =   PROFILE_RED_STARTPOINT_TIME;
        betaGlucanaseTime           =   PROFILE_RED_BETAGLUCANASE_TIME;
        debranchingTime             =   PROFILE_RED_DEBRANCHING_TIME;
        proteolyticTime             =   PROFILE_RED_PROTEOLYTIC_TIME;
        betaAmylaseTime             =   PROFILE_RED_BETAAMYLASE_TIME;
        alphaAmylaseTime            =   PROFILE_RED_ALPHAAMYLASE_TIME;
        mashoutTime                 =   PROFILE_RED_MASHOUT_TIME;
        recirculationTime           =   PROFILE_RED_RECIRCULATION_TIME;
        spargeTime                  =   PROFILE_RED_SPARGE_TIME;
        boilTime                    =   PROFILE_RED_BOIL_TIME;
        coolingTime                 =   PROFILE_RED_COOLING_TIME;
        startpointTemperature       =   PROFILE_RED_STARTPOINT_TEMPERATURE;
        betaGlucanaseTemperature    =   PROFILE_RED_BETAGLUCANASE_TEMPERATURE;
        debranchingTemperature      =   PROFILE_RED_DEBRANCHING_TEMPERATURE;
        proteolyticTemperature      =   PROFILE_RED_PROTEOLYTIC_TEMPERATURE;
        betaAmylaseTemperature      =   PROFILE_RED_BETAAMYLASE_TEMPERATURE;
        alphaAmylaseTemperature     =   PROFILE_RED_ALPHAAMYLASE_TEMPERATURE;
        mashoutTemperature          =   PROFILE_RED_MASHOUT_TEMPERATURE;
        recirculationTemperature    =   PROFILE_RED_RECIRCULATION_TEMPERATURE;
        spargeTemperature           =   PROFILE_RED_SPARGE_TEMPERATURE;
        boilTemperature             =   PROFILE_RED_BOIL_TEMPERATURE;
        coolingTemperature          =   PROFILE_RED_COOLING_TEMPERATURE;
João Lino's avatar
João Lino committed
1259

João Lino's avatar
João Lino committed
1260 1261 1262
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1263
    case eBeerProfileMenu_APA: {
João Lino's avatar
João Lino committed
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
        beerProfile                 =   eBeerProfile_APA;
        startpointTime              =   PROFILE_APA_STARTPOINT_TIME;
        betaGlucanaseTime           =   PROFILE_APA_BETAGLUCANASE_TIME;
        debranchingTime             =   PROFILE_APA_DEBRANCHING_TIME;
        proteolyticTime             =   PROFILE_APA_PROTEOLYTIC_TIME;
        betaAmylaseTime             =   PROFILE_APA_BETAAMYLASE_TIME;
        alphaAmylaseTime            =   PROFILE_APA_ALPHAAMYLASE_TIME;
        mashoutTime                 =   PROFILE_APA_MASHOUT_TIME;
        recirculationTime           =   PROFILE_APA_RECIRCULATION_TIME;
        spargeTime                  =   PROFILE_APA_SPARGE_TIME;
        boilTime                    =   PROFILE_APA_BOIL_TIME;
        coolingTime                 =   PROFILE_APA_COOLING_TIME;
        startpointTemperature       =   PROFILE_APA_STARTPOINT_TEMPERATURE;
        betaGlucanaseTemperature    =   PROFILE_APA_BETAGLUCANASE_TEMPERATURE;
        debranchingTemperature      =   PROFILE_APA_DEBRANCHING_TEMPERATURE;
        proteolyticTemperature      =   PROFILE_APA_PROTEOLYTIC_TEMPERATURE;
        betaAmylaseTemperature      =   PROFILE_APA_BETAAMYLASE_TEMPERATURE;
        alphaAmylaseTemperature     =   PROFILE_APA_ALPHAAMYLASE_TEMPERATURE;
        mashoutTemperature          =   PROFILE_APA_MASHOUT_TEMPERATURE;
        recirculationTemperature    =   PROFILE_APA_RECIRCULATION_TEMPERATURE;
        spargeTemperature           =   PROFILE_APA_SPARGE_TEMPERATURE;
        boilTemperature             =   PROFILE_APA_BOIL_TEMPERATURE;
        coolingTemperature          =   PROFILE_APA_COOLING_TEMPERATURE;
João Lino's avatar
João Lino committed
1287

João Lino's avatar
João Lino committed
1288 1289 1290
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1291
    case eBeerProfileMenu_Custom: {
João Lino's avatar
João Lino committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
        beerProfile                 =   eBeerProfile_Custom;
        startpointTime              =   PROFILE_CUSTOM_STARTPOINT_TIME;
        betaGlucanaseTime           =   PROFILE_CUSTOM_BETAGLUCANASE_TIME;
        debranchingTime             =   PROFILE_CUSTOM_DEBRANCHING_TIME;
        proteolyticTime             =   PROFILE_CUSTOM_PROTEOLYTIC_TIME;
        betaAmylaseTime             =   PROFILE_CUSTOM_BETAAMYLASE_TIME;
        alphaAmylaseTime            =   PROFILE_CUSTOM_ALPHAAMYLASE_TIME;
        mashoutTime                 =   PROFILE_CUSTOM_MASHOUT_TIME;
        recirculationTime           =   PROFILE_CUSTOM_RECIRCULATION_TIME;
        spargeTime                  =   PROFILE_CUSTOM_SPARGE_TIME;
        boilTime                    =   PROFILE_CUSTOM_BOIL_TIME;
        coolingTime                 =   PROFILE_CUSTOM_COOLING_TIME;
        startpointTemperature       =   PROFILE_CUSTOM_STARTPOINT_TEMPERATURE;
        betaGlucanaseTemperature    =   PROFILE_CUSTOM_BETAGLUCANASE_TEMPERATURE;
        debranchingTemperature      =   PROFILE_CUSTOM_DEBRANCHING_TEMPERATURE;
        proteolyticTemperature      =   PROFILE_CUSTOM_PROTEOLYTIC_TEMPERATURE;
        betaAmylaseTemperature      =   PROFILE_CUSTOM_BETAAMYLASE_TEMPERATURE;
        alphaAmylaseTemperature     =   PROFILE_CUSTOM_ALPHAAMYLASE_TEMPERATURE;
        mashoutTemperature          =   PROFILE_CUSTOM_MASHOUT_TEMPERATURE;
        recirculationTemperature    =   PROFILE_CUSTOM_RECIRCULATION_TEMPERATURE;
        spargeTemperature           =   PROFILE_CUSTOM_SPARGE_TEMPERATURE;
        boilTemperature             =   PROFILE_CUSTOM_BOIL_TEMPERATURE;
        coolingTemperature          =   PROFILE_CUSTOM_COOLING_TEMPERATURE;
João Lino's avatar
João Lino committed
1315

João Lino's avatar
João Lino committed
1316 1317 1318
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1319
    case eBeerProfileMenu_Back: {
João Lino's avatar
João Lino committed
1320 1321 1322
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1323
    default: {}
João Lino's avatar
João Lino committed
1324
  }
João Lino's avatar
João Lino committed
1325
  mdBeerProfileMenu._selection = eBeerProfileMenu_NULL;
1326 1327
}

João Lino's avatar
João Lino committed
1328 1329 1330
void runStageSelection() {
  switch (mdStageMenu._selection) {
    case eStageMenu_Startpoint: {
João Lino's avatar
João Lino committed
1331 1332 1333
        runStageSelection_Generic( &startpointTime, &startpointTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1334
    case eStageMenu_BetaGlucanase: {
João Lino's avatar
João Lino committed
1335 1336 1337
        runStageSelection_Generic( &betaGlucanaseTime, &betaGlucanaseTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1338
    case eStageMenu_Debranching: {
João Lino's avatar
João Lino committed
1339 1340 1341
        runStageSelection_Generic( &debranchingTime, &debranchingTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1342
    case eStageMenu_Proteolytic: {
João Lino's avatar
João Lino committed
1343 1344 1345
        runStageSelection_Generic( &proteolyticTime, &proteolyticTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1346
    case eStageMenu_BetaAmylase: {
João Lino's avatar
João Lino committed
1347 1348 1349
        runStageSelection_Generic( &betaAmylaseTime, &betaAmylaseTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1350
    case eStageMenu_AlphaAmylase: {
João Lino's avatar
João Lino committed
1351 1352 1353
        runStageSelection_Generic( &alphaAmylaseTime, &alphaAmylaseTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1354
    case eStageMenu_Mashout: {
João Lino's avatar
João Lino committed
1355 1356 1357
        runStageSelection_Generic( &mashoutTime, &mashoutTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1358
    case eStageMenu_Recirculation: {
João Lino's avatar
João Lino committed
1359 1360 1361
        runStageSelection_Generic( &recirculationTime, &recirculationTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1362
    case eStageMenu_Sparge: {
João Lino's avatar
João Lino committed
1363 1364 1365
        runStageSelection_Generic( &spargeTime, &spargeTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1366
    case eStageMenu_Boil: {
João Lino's avatar
João Lino committed
1367 1368 1369
        runStageSelection_Generic( &boilTime, &boilTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1370
    case eStageMenu_Cooling: {
João Lino's avatar
João Lino committed
1371 1372 1373
        runStageSelection_Generic( &coolingTime, &coolingTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1374
    case eStageMenu_Back: {
João Lino's avatar
João Lino committed
1375 1376 1377
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1378
    default: {}
João Lino's avatar
João Lino committed
1379
  }
João Lino's avatar
João Lino committed
1380 1381
  mdStageMenu._selection = eStageMenu_NULL;
}
1382

João Lino's avatar
João Lino committed
1383 1384 1385
void runSettingsSelection() {
  switch (mdSettingsMenu._selection) {
    case eSettingsMenu_Pump: {
João Lino's avatar
João Lino committed
1386
        bool bNewPumpStatus = xSetGenericValue( iPumpSpeed ? 0 : 1, PUMP_SPEED_DEFAULT, 0, 1, "pump", "bool" );
João Lino's avatar
João Lino committed
1387
        if ( cancel ) {
João Lino's avatar
João Lino committed
1388 1389
          cancel = false;
        }
João Lino's avatar
João Lino committed
1390 1391 1392 1393 1394 1395 1396
        else {
          if ( bNewPumpStatus ) {
            iPumpSpeed = PUMP_SPEED_MAX_MOSFET;
          } else {
            iPumpSpeed = PUMP_SPEED_STOP_MOSFET;
          }
          analogWrite(PUMP_PIN, iPumpSpeed);
João Lino's avatar
João Lino committed
1397
        }
João Lino's avatar
João Lino committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
        backToStatus();
        break;
      }
    case eSettingsMenu_PT100_Element: {
        backToStatus();
        break;
      }
    case eSettingsMenu_PT100_Up: {
        backToStatus();
        break;
      }
    case eSettingsMenu_PT100_Down: {
        backToStatus();
        break;
João Lino's avatar
João Lino committed
1412
      }
João Lino's avatar
João Lino committed
1413
    case eSettingsMenu_Back: {
João Lino's avatar
João Lino committed
1414 1415 1416
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1417
    default: {}
João Lino's avatar
João Lino committed
1418
  }
João Lino's avatar
João Lino committed
1419
  mdSettingsMenu._selection = eSettingsMenu_NULL;
João Lino's avatar
Rel.3  
João Lino committed
1420 1421
}

João Lino's avatar
João Lino committed
1422 1423 1424 1425 1426
void runMaltSelection() {
  switch (mdMaltMenu._selection) {
    case eMaltMenu_CastleMalting_Chteau_Pilsen_2RS: {
        backToStatus();
        break;
João Lino's avatar
João Lino committed
1427
      }
João Lino's avatar
João Lino committed
1428 1429 1430
    case eMaltMenu_CastleMalting_Wheat_Blanc: {
        backToStatus();
        break;
João Lino's avatar
João Lino committed
1431
      }
João Lino's avatar
João Lino committed
1432
    case eMaltMenu_Back: {
João Lino's avatar
João Lino committed
1433 1434 1435
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1436
    default: {}
João Lino's avatar
Rel.3  
João Lino committed
1437
  }
João Lino's avatar
João Lino committed
1438
  mdMaltMenu._selection = eMaltMenu_NULL;
1439 1440
}

João Lino's avatar
João Lino committed
1441 1442 1443
// ************************ MENU HELPERS ******************************
void runMenuProcessor( MenuData *data ) {
  data->_position = rotaryEncoderVirtualPosition;         // Read position
1444

João Lino's avatar
João Lino committed
1445 1446
  data->_repaint = repaint;                               // Request repaint
  repaint = displayGenericMenu( &lcd, data );             // Display menu
João Lino's avatar
Rel.3  
João Lino committed
1447

João Lino's avatar
João Lino committed
1448
  if ( checkForEncoderSwitchPush( true ) ) {              // Read selection
João Lino's avatar
João Lino committed
1449
    if ( cancel ) {
João Lino's avatar
João Lino committed
1450 1451 1452
      resetMenu( true );
      return;
    }
João Lino's avatar
João Lino committed
1453
    data->_selection = data->_position;
1454
  }
João Lino's avatar
João Lino committed
1455

João Lino's avatar
João Lino committed
1456
  (data->_selectionFunction)();                           // Run selection function
1457 1458
}

João Lino's avatar
João Lino committed
1459
void runStageSelection_Generic( unsigned long * selectedStageTime, int *selectedStageTemperature) {
João Lino's avatar
João Lino committed
1460 1461
  unsigned long selectedStageTimeStorage = *selectedStageTime;
  int selectedStageTemperatureStorage = *selectedStageTemperature;
João Lino's avatar
João Lino committed
1462

João Lino's avatar
João Lino committed
1463
  *selectedStageTime = getTimer( *selectedStageTime );
João Lino's avatar
João Lino committed
1464
  if ( cancel ) {
João Lino's avatar
João Lino committed
1465 1466 1467
    *selectedStageTime = selectedStageTimeStorage;
    cancel = false;
  }
João Lino's avatar
João Lino committed
1468 1469
  else {
    *selectedStageTemperature = getTemperature( *selectedStageTemperature );
João Lino's avatar
João Lino committed
1470
    if ( cancel ) {
João Lino's avatar
João Lino committed
1471 1472 1473 1474
      *selectedStageTime = selectedStageTimeStorage;
      *selectedStageTemperature = selectedStageTemperatureStorage;
      cancel = false;
    }
João Lino's avatar
João Lino committed
1475
  }
João Lino's avatar
João Lino committed
1476
  backToStatus();
1477 1478
}

João Lino's avatar
João Lino committed
1479 1480
void xStartStageHeadless( eCookingStages nextStage, bool bPurgePump ) {
  xStartStage( NULL, NULL, nextStage, bPurgePump, false, false, false );
João Lino's avatar
Rel.3  
João Lino committed
1481 1482
}

João Lino's avatar
João Lino committed
1483 1484
void xStartStageInteractive( unsigned long *stageTime, int *stageTemperature, eCookingStages nextStage ) {
  xStartStage( stageTime, stageTemperature, nextStage, true, true, true, true );
1485 1486
}

João Lino's avatar
João Lino committed
1487
void xStartStage( unsigned long *stageTime, int *stageTemperature, eCookingStages nextStage, bool bPurgePump, bool bSetFinalYield, bool bSetTime, bool bSetTemperature ) {
João Lino's avatar
João Lino committed
1488 1489 1490
  int finalYieldStorage = finalYield;
  unsigned long stageTimeStorage = *stageTime;
  int stageTemperatureStorage = *stageTemperature;
João Lino's avatar
João Lino committed
1491

João Lino's avatar
João Lino committed
1492
  if (bSetFinalYield) {
João Lino's avatar
João Lino committed
1493
    finalYield = getFinalYield( finalYield, SETTING_MACHINE_YIELD_DEFAULT );
João Lino's avatar
João Lino committed
1494
    if ( cancel ) {
João Lino's avatar
João Lino committed
1495
      finalYield = finalYieldStorage;
João Lino's avatar
João Lino committed
1496

João Lino's avatar
João Lino committed
1497
      cancel = false;
João Lino's avatar
João Lino committed
1498
      backToStatus();
João Lino's avatar
João Lino committed
1499 1500
      return;
    }
João Lino's avatar
Rel.3  
João Lino committed
1501
  }
João Lino's avatar
João Lino committed
1502
  if (bSetTime) {
João Lino's avatar
João Lino committed
1503
    (*stageTime) = getTimer( clockCounter / 1000, *stageTime );
João Lino's avatar
João Lino committed
1504
    if ( cancel ) {
João Lino's avatar
João Lino committed
1505 1506
      finalYield = finalYieldStorage;
      *stageTime = stageTimeStorage;
João Lino's avatar
João Lino committed
1507

João Lino's avatar
João Lino committed
1508
      cancel = false;
João Lino's avatar
João Lino committed
1509
      backToStatus();
João Lino's avatar
João Lino committed
1510 1511
      return;
    }
João Lino's avatar
Rel.3  
João Lino committed
1512
  }
João Lino's avatar
João Lino committed
1513
  if (bSetTemperature) {
João Lino's avatar
João Lino committed
1514
    (*stageTemperature) = getTemperature( cookTemperature, *stageTemperature );
João Lino's avatar
João Lino committed
1515
    if ( cancel ) {
João Lino's avatar
João Lino committed
1516 1517 1518
      finalYield = finalYieldStorage;
      *stageTime = stageTimeStorage;
      *stageTemperature = stageTemperatureStorage;
João Lino's avatar
João Lino committed
1519

João Lino's avatar
João Lino committed
1520
      cancel = false;
João Lino's avatar
João Lino committed
1521
      backToStatus();
João Lino's avatar
João Lino committed
1522 1523
      return;
    }
João Lino's avatar
Rel.3  
João Lino committed
1524
  }
João Lino's avatar
João Lino committed
1525

João Lino's avatar
João Lino committed
1526
  xSafeHardwarePowerOff();                      // Stop anything that might be still going on
João Lino's avatar
João Lino committed
1527 1528
  if (bPurgePump) {
    xPurgePump();
1529
  }
João Lino's avatar
João Lino committed
1530 1531 1532
  startBrewing();
  xSetupStage( nextStage );
  backToStatus();
1533
}
João Lino's avatar
João Lino committed
1534
// ##################################################### Menus ###################################################################
1535 1536 1537 1538

// #################################################### Helpers ##################################################################

void startBrewing() {
1539 1540
  //sing(MELODY_SUPER_MARIO, PIEZO_PIN);

1541 1542 1543 1544 1545 1546 1547
  cooking = true;
}

void stopBrewing() {
  cooking = false;
}

João Lino's avatar
João Lino committed
1548 1549 1550
void resetMenu( boolean requestRepaintPaint ) {
  eMenuType = eMenuType_Main;

1551
  if ( requestRepaintPaint ) {
João Lino's avatar
João Lino committed
1552 1553
    repaint = true;
  }
1554

João Lino's avatar
João Lino committed
1555
  xSetupRotaryEncoder( eRotaryEncoderMode_Menu, mdMainMenu._position, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );       // reset operation state
João Lino's avatar
João Lino committed
1556 1557
}

1558 1559
void backToStatus() {
  lastInterruptTime = millis() - SETTING_MAX_INACTIVITY_TIME - 1;
João Lino's avatar
João Lino committed
1560
  resetMenu(true);
1561 1562 1563 1564
}
// #################################################### Helpers ##################################################################

// #################################################### Set Variables ##################################################################
João Lino's avatar
João Lino committed
1565

João Lino's avatar
João Lino committed
1566 1567 1568 1569 1570 1571
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 );
}
João Lino's avatar
João Lino committed
1572

João Lino's avatar
João Lino committed
1573 1574 1575 1576 1577 1578
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" );
}
João Lino's avatar
João Lino committed
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598

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) {
João Lino's avatar
João Lino committed
1599 1600
    if ( checkForEncoderSwitchPush( true ) ) {                  // Check if pushbutton is pressed
      if ( cancel ) return rotaryEncoderVirtualPosition;
João Lino's avatar
João Lino committed
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
      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 );
}

João Lino's avatar
João Lino committed
1635
int getTimer( int initialValue, int defaultValue ) {
João Lino's avatar
João Lino committed
1636
  // set operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
João Lino's avatar
João Lino committed
1637
  xSetupRotaryEncoder( eRotaryEncoderMode_Time, initialValue, 7200, 0, 1, 30 );
1638 1639

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1640
  int rotaryEncoderPreviousPosition = 0;
1641 1642
  int minutes = 0;
  int seconds = 0;
1643

1644 1645
  // Setup Screen
  lcd.clear();
1646
  lcd.home();
João Lino's avatar
João Lino committed
1647
  lcd.print("Set Time (");
1648
  minutes = defaultValue / 60;
João Lino's avatar
João Lino committed
1649
  lcd.print(minutes);
1650
  seconds = defaultValue - minutes * 60;
João Lino's avatar
João Lino committed
1651
  lcd.print(":");
1652
  if (seconds < 10) {
João Lino's avatar
João Lino committed
1653 1654 1655 1656
    lcd.print("0");
  }
  lcd.print(seconds);
  lcd.print(")");
1657
  lcd.setCursor (0, LCD_VERTICAL_RESOLUTION - 1);
1658
  lcd.print("      0:00");
1659 1660

  while (true) {
João Lino's avatar
João Lino committed
1661 1662
    if ( checkForEncoderSwitchPush( true ) ) {
      if ( cancel ) return rotaryEncoderVirtualPosition;
1663
      break;
João Lino's avatar
João Lino committed
1664 1665 1666
    }
    else {
      xManageMachineSystems();                            // Don't forget to keep an eye on the cooking
João Lino's avatar
João Lino committed
1667
    }
1668

1669
    // display current timer
service-config's avatar
Mixer  
service-config committed
1670 1671
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
1672 1673 1674 1675 1676 1677
      minutes = rotaryEncoderVirtualPosition / 60;
      seconds = rotaryEncoderVirtualPosition - minutes * 60;

      lcd.setCursor (0, LCD_VERTICAL_RESOLUTION - 1);

      if (minutes < 100) {
João Lino's avatar
João Lino committed
1678 1679
        lcd.print(" ");
      }
1680
      if (minutes < 10) {
João Lino's avatar
João Lino committed
1681 1682 1683
        lcd.print(" ");
      }
      lcd.print("    ");
1684 1685
      lcd.print(minutes);
      lcd.print(":");
1686
      if (seconds < 10) {
1687 1688 1689 1690 1691 1692
        lcd.print("0");
      }
      lcd.print(seconds);
      lcd.println("                ");
    }
  }
1693

service-config's avatar
Mixer  
service-config committed
1694
  return rotaryEncoderVirtualPosition;
1695 1696
}

João Lino's avatar
João Lino committed
1697 1698 1699 1700 1701 1702
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
João Lino's avatar
João Lino committed
1703 1704

      if ( ((millis() - cancleTimer) >= SETTING_CANCEL_TIMER ) && cancelable ) {
João Lino's avatar
João Lino committed
1705
        sing(BUZZ_1, PIEZO_PIN);
1706 1707
      }
    }
1708

João Lino's avatar
João Lino committed
1709
    if ( ((millis() - cancleTimer) >= SETTING_CANCEL_TIMER) && cancelable ) {
João Lino's avatar
João Lino committed
1710
      cancel = true;
1711 1712
    }
  }
1713

João Lino's avatar
João Lino committed
1714
  return gotPush;
João Lino's avatar
João Lino committed
1715 1716
}

João Lino's avatar
João Lino committed
1717 1718 1719 1720
unsigned long getInactivityTime() {
  unsigned long now = millis();
  unsigned long rotaryEncoderInactivityTime = now - lastInterruptTime;

1721
  if (rotaryEncoderInactivityTime > SETTING_MAX_INACTIVITY_TIME) {
João Lino's avatar
João Lino committed
1722
    if (checkForEncoderSwitchPush( false )) {
1723 1724 1725 1726 1727 1728
      now = millis();
      rotaryEncoderInactivityTime = now - lastInterruptTime;
      rotarySwDetectTime = now;

      repaint = true;
      refresh = true;
João Lino's avatar
João Lino committed
1729 1730
    }
  }
1731

João Lino's avatar
João Lino committed
1732 1733 1734 1735
  unsigned long switchInactivityTime = now - rotarySwDetectTime;
  return rotaryEncoderInactivityTime > switchInactivityTime ? switchInactivityTime : rotaryEncoderInactivityTime ;
}

1736
// ###################### Set Variables ##################################################
1737

João Lino's avatar
Rel.3  
João Lino committed
1738
void xWaitForAction(String title, String message) {
1739
  while (true) {
João Lino's avatar
João Lino committed
1740
    if ( checkForEncoderSwitchPush( false ) ) {                  // Check if pushbutton is pressed
João Lino's avatar
Rel.3  
João Lino committed
1741
      break;
João Lino's avatar
João Lino committed
1742 1743
    }
    else {
João Lino's avatar
Rel.3  
João Lino committed
1744 1745 1746
      sing(BUZZ_1, PIEZO_PIN);

      // Print the message
1747
      if (! lcdPrint(&lcd, title, message)) {
João Lino's avatar
Rel.3  
João Lino committed
1748 1749
        break;
      }
1750 1751 1752
    }
  }
}