brew.ino 70.5 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;
João Lino's avatar
João Lino committed
96
double                  dWattage;
97

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

101
// ######################### INITIALIZE #########################
102
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
103
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);
104

João Lino's avatar
João Lino committed
105
// +++++++++++++++++++++++ Temperature +++++++++++++++++++++++
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
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);
121 122

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

127
  // 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
128
  if (diff > ROTARY_ENCODER_DEBOUNCE_TIME) {
João Lino's avatar
João Lino committed
129
    lastInterruptTime = interruptTime;
130 131

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

          break;
148
        }
149

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

          break;
181
        }
182

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

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

        }
203
    }
204

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

210
void xSetupRotaryEncoder( eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep ) {
211 212 213 214 215 216
  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;
217 218
}

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

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

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

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

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

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

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

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

251
void setup() {
João Lino's avatar
João Lino committed
252
  // ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
253
  pinMode                         (ROTARY_ENCODER_CLK_PIN, INPUT);
João Lino's avatar
João Lino committed
254 255 256 257 258 259 260
  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
261
  bStatusElement              =   false;
João Lino's avatar
João Lino committed
262 263
  windowStartTime             =   millis();
  dWattPerPulse               =   HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ;
João Lino's avatar
João Lino committed
264
  dWattage                    =   0.0;
João Lino's avatar
João Lino committed
265 266 267

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

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

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

João Lino's avatar
João Lino committed
276
  // ++++++++++++++++++++++++ Temperature Sensor PT100 ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
277

João Lino's avatar
João Lino committed
278 279 280 281 282
  // ++++++++++++++++++++++++ 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 ++++++++++++++++++++++++
283 284
  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
285 286 287 288 289 290
  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
291
  rotarySwDetectTime = 0;
João Lino's avatar
João Lino committed
292 293

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

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

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

João Lino's avatar
João Lino committed
302
  cooking                     =   false;
303

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

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

315 316 317 318 319 320 321 322 323 324 325
  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;
João Lino's avatar
Rel.3  
João Lino committed
326
  cleaningTime                =   SETTING_CLEANING_TIME;
João Lino's avatar
João Lino committed
327

328 329 330 331 332 333 334 335 336 337 338
  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
Rel.3  
João Lino committed
339
  cleaningTemperature         =   SETTING_CLEANING_TEMPERATURE;
João Lino's avatar
João Lino committed
340 341 342 343

  refresh                     =   true;
  repaint                     =   true;

João Lino's avatar
João Lino committed
344 345
  loggingTimeInterval         =   0;

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

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

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

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

  printBeerProfile();
359
}
360

361
// ######################### MAIN LOOP #########################
362 363

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

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

João Lino's avatar
Rel.3  
João Lino committed
377
  xManageMachineSystems();
378 379
}

380 381
// ######################### FUNCTIONS ########################

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

João Lino's avatar
João Lino committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398
  // 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
399

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

João Lino's avatar
João Lino committed
406
  // Calculate the remaining time on the clock
407 408 409
  //clockCounter = cookTime * 1000 - (lastWindowTime - clockIgnore);
  unsigned long elepsedTime = now - clockStartTime;
  clockCounter = cookTime * 1000 - (elepsedTime - clockIgnore);
410

João Lino's avatar
João Lino committed
411 412 413 414
  // Don't let clock get bellow 0
  if ( clockCounter < 0 ) {
    clockCounter = 0;
  }
João Lino's avatar
João Lino committed
415

João Lino's avatar
João Lino committed
416
  clockLastUpdate = now;
João Lino's avatar
João Lino committed
417

João Lino's avatar
João Lino committed
418 419 420 421 422 423 424 425 426
#ifdef DEBUG_OFF
  debugPrintFunction("xCountTheTime");
  debugPrintVar("millis()", now);
  debugPrintVar("cookTime", cookTime);
  debugPrintVar("clockStartTime", clockStartTime);
  debugPrintVar("clockIgnore", clockIgnore);
  debugPrintVar("clockCounter", clockCounter);
#endif
}
427

João Lino's avatar
João Lino committed
428 429 430 431 432 433
bool isTimeLeft() {
  if ( clockCounter > 0 ) {
    return true;
  }
  return false;
}
João Lino's avatar
João Lino committed
434

João Lino's avatar
João Lino committed
435 436 437 438 439
//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;
}
440

João Lino's avatar
João Lino committed
441 442 443
bool xRegulateTemperature( boolean bMaximumOfUpDown ) {
  double difference = 0;
  bool overTemperature = false;
João Lino's avatar
João Lino committed
444
  dWattage = 0.0;
445

João Lino's avatar
João Lino committed
446 447 448
  float tup = upPT100.getCurrentTemperature();
  float tdown = downPT100.getCurrentTemperature();
  float tbase = basePT100.getCurrentTemperature();
449

João Lino's avatar
João Lino committed
450 451 452 453 454 455 456
  if ( bMaximumOfUpDown ) {
    if (tup > tdown) {
      difference = cookTemperature - tup;
    }
    else {
      difference = cookTemperature - tdown;
    }
457

João Lino's avatar
João Lino committed
458 459 460
    if (tbase > cookTemperature && (tbase >= (PUMP_TEMPERATURE_MAX_OPERATION - 2.0) || difference >= 5.0)) {
      difference = cookTemperature - tbase;
    }
461

João Lino's avatar
João Lino committed
462 463 464 465 466 467
    if ( (tbase < cookTemperature) && (difference < (cookTemperature - tbase)) ) {
      difference = cookTemperature - tbase;
    }
  } else {
    difference = cookTemperature - tbase;
  }
468

João Lino's avatar
João Lino committed
469 470 471 472 473
  // Deviation between the cook temperature set and the cook temperature measured
  if ( difference < 0.0 ) {
    difference = difference * (-1.0);
    overTemperature = true;
  }
474

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

João Lino's avatar
João Lino committed
513 514 515 516
  // 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;
  }
517

João Lino's avatar
João Lino committed
518
  // Apply wattage to the element at the right time
João Lino's avatar
João Lino committed
519
  if ( ulWattToWindowTime( dWattage ) > (millis() - windowStartTime) ) {
João Lino's avatar
João Lino committed
520 521 522 523 524
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN, HIGH);
    bStatusElement = true;
  } else {
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN, LOW);
    bStatusElement = false;
525
  }
João Lino's avatar
Rel.3  
João Lino committed
526

527
#ifdef DEBUG_OFF
João Lino's avatar
João Lino committed
528 529 530
  //debugPrintFunction("xRegulateTemperature");
  debugPrintVar("difference", difference);
  //debugPrintVar("overTemperature", overTemperature);
João Lino's avatar
João Lino committed
531 532
  debugPrintVar("dWattage", dWattage);
  //debugPrintVar("ulWattToWindowTime( dWattage )", ulWattToWindowTime( dWattage ) );
João Lino's avatar
João Lino committed
533 534
  //debugPrintVar("millis()", millis());
  //debugPrintVar("windowStartTime", windowStartTime);
João Lino's avatar
João Lino committed
535
  //debugPrintVar("test", ulWattToWindowTime( dWattage ) > (millis() - windowStartTime) );
536
#endif
537 538
}

João Lino's avatar
João Lino committed
539
void xPurgePump() {
540
  lcdPrint(&lcd, "    Purging", "     Pump!");    // Write welcome
João Lino's avatar
João Lino committed
541 542 543 544 545 546 547
  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
548

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

João Lino's avatar
João Lino committed
552 553
  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
554

João Lino's avatar
João Lino committed
555 556 557 558 559 560
    basePT100.setPumpStatus( false );
    upPT100.setPumpStatus( false );
    downPT100.setPumpStatus( false );
  }
  else {
    analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255
561

João Lino's avatar
João Lino committed
562 563 564
    basePT100.setPumpStatus( true );
    upPT100.setPumpStatus( true );
    downPT100.setPumpStatus( true );
João Lino's avatar
Rel.3  
João Lino committed
565
  }
João Lino's avatar
João Lino committed
566
}
João Lino's avatar
Rel.3  
João Lino committed
567

João Lino's avatar
João Lino committed
568 569
void xWarnClockEnded() {
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);
João Lino's avatar
Rel.3  
João Lino committed
570 571
}

João Lino's avatar
João Lino committed
572 573 574
void xWarnCookEnded() {
  sing(MELODY_UNDERWORLD_SHORT, PIEZO_PIN);
}
João Lino's avatar
Rel.3  
João Lino committed
575

João Lino's avatar
João Lino committed
576 577 578 579 580
void xPrepareForStage( int stageTime, int stageTemperature, int stagePumpSpeed, eCookingStages stage ) {
#ifdef DEBUG_OFF
  debugPrintFunction("xPrepareForStage");
  debugPrintVar("cookingStage", stage);
#endif
581

João Lino's avatar
João Lino committed
582 583 584 585 586
  // Reset the clock
  unsigned long now = millis();
  clockStartTime  = now;
  clockLastUpdate = now;
  clockIgnore     = 0;
João Lino's avatar
Rel.3  
João Lino committed
587

João Lino's avatar
João Lino committed
588 589 590 591
  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
592 593
}

João Lino's avatar
João Lino committed
594 595 596 597 598
void xSetupStage(eCookingStages nextStage) {
#ifdef DEBUG_OFF
  debugPrintFunction("xSetupStage");
  debugPrintVar("cookingStage", nextStage);
#endif
João Lino's avatar
Rel.3  
João Lino committed
599

João Lino's avatar
João Lino committed
600 601 602 603 604 605 606 607 608 609 610 611
  // 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.");
612

João Lino's avatar
João Lino committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
              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.");
631

João Lino's avatar
João Lino committed
632 633 634 635 636
              xWaitForAction("Malt", say);
              repaint = true;
              break;
            }
          default: {
João Lino's avatar
Rel.3  
João Lino committed
637

João Lino's avatar
João Lino committed
638 639
            }
        }
640

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

João Lino's avatar
João Lino committed
644 645
        repaint = true;
        xPrepareForStage( startpointTime, startpointTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Startpoint );
646 647
        break;
      }
João Lino's avatar
João Lino committed
648 649 650 651 652 653 654 655 656 657
    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.");
658

João Lino's avatar
João Lino committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
              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.");
677

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

João Lino's avatar
João Lino committed
724 725
              float hopAmount = 0.8 * ((float) finalYield);
              say += String(hopAmount);
726

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

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

João Lino's avatar
João Lino committed
731 732 733 734
              break;
            }
          case eBeerProfile_IPA: {
              String say = "Get ";
João Lino's avatar
Rel.3  
João Lino committed
735

João Lino's avatar
João Lino committed
736 737
              float hopAmount = 0.8 * ((float) finalYield);
              say += String(hopAmount);
João Lino's avatar
Rel.3  
João Lino committed
738

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

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

João Lino's avatar
João Lino committed
743 744 745 746
              break;
            }
          default: {
              xWaitForAction("Hops", "Add the hops in the right order, at the right time.");
747

João Lino's avatar
João Lino committed
748 749
            }
        }
750

João Lino's avatar
João Lino committed
751
        repaint = true;
752

João Lino's avatar
João Lino committed
753 754
        // 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
755

756 757
        break;
      }
João Lino's avatar
João Lino committed
758 759 760
    case eCookingStage_Cooling: {
        // Make sure there is water
        xWaitForAction("Coil", "Add the coil and connect it to the main water supply.");
761

João Lino's avatar
João Lino committed
762 763 764 765
        repaint = true;

        // A basic operation for a basic stage
        xPrepareForStage( coolingTime, coolingTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Cooling );
766 767 768

        break;
      }
João Lino's avatar
João Lino committed
769 770 771
    case eCookingStage_Clean: {
        // Make sure there is water
        xWaitForAction("Water", "Add 13 liters.");
772

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

João Lino's avatar
João Lino committed
776
        repaint = true;
777

João Lino's avatar
João Lino committed
778 779
        // A basic operation for a basic stage
        xPrepareForStage( cleaningTime, cleaningTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Clean );
780 781 782

        break;
      }
João Lino's avatar
João Lino committed
783 784 785
    case eCookingStage_Purge: {
        // A basic operation for a basic stage
        xPrepareForStage( 0, 0, PUMP_SPEED_MAX_MOSFET, eCookingStage_Purge );
786

João Lino's avatar
João Lino committed
787
        xRegulatePumpSpeed();
788 789 790

        break;
      }
João Lino's avatar
João Lino committed
791 792 793
    case eCookingStage_Done: {
        // A basic operation for a basic stage
        xPrepareForStage( 0, 0, PUMP_SPEED_STOP_MOSFET, eCookingStage_Done );
794 795 796

        break;
      }
797
  }
798 799
}

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

João Lino's avatar
João Lino committed
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 829 830 831 832 833 834 835
  // 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;
836
  }
João Lino's avatar
João Lino committed
837 838 839 840 841 842 843 844 845

  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
846

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
#ifdef INFO
  unsigned long now = millis();
  if( now - loggingTimeInterval > SETTING_MACHINE_LOGGING_INTERVAL ) {
    loggingTimeInterval = now;
    Serial.print("|");
    Serial.print(now);
    Serial.print("|");
    Serial.print(clockCounter);
    Serial.print("|");
    if (cooking) {
      Serial.print("1");
    }
    else {
      Serial.print("0");
    }
    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("|");
João Lino's avatar
João Lino committed
871 872
    Serial.print(dWattage);
    Serial.print("|");
873 874 875 876 877 878 879 880 881 882
    if (bStatusElement) {
      Serial.print("1");
    }
    else {
      Serial.print("0");
    }
    Serial.println("|");
  } 
#endif

João Lino's avatar
João Lino committed
883 884 885
  // 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
886

João Lino's avatar
João Lino committed
887 888
    return;
  }
João Lino's avatar
João Lino committed
889

João Lino's avatar
João Lino committed
890 891 892 893
  // Operate the machine according to the current mode
  switch (cookingStage) {
    case eCookingStage_Startpoint: {
        xBasicStageOperation( startpointTime, startpointTemperature, 0, eCookingStage_BetaGlucanase, false);
894 895
        break;
      }
João Lino's avatar
João Lino committed
896 897
    case eCookingStage_BetaGlucanase: {
        xBasicStageOperation( betaGlucanaseTime, betaGlucanaseTemperature, 3, eCookingStage_Debranching, true );
898 899
        break;
      }
João Lino's avatar
João Lino committed
900 901
    case eCookingStage_Debranching: {
        xBasicStageOperation( debranchingTime, debranchingTemperature, 3, eCookingStage_Proteolytic, true );
902 903
        break;
      }
João Lino's avatar
João Lino committed
904 905
    case eCookingStage_Proteolytic: {
        xBasicStageOperation( proteolyticTime, proteolyticTemperature, 3, eCookingStage_BetaAmylase, true );
906 907
        break;
      }
João Lino's avatar
João Lino committed
908 909
    case eCookingStage_BetaAmylase: {
        xBasicStageOperation( betaAmylaseTime, betaAmylaseTemperature, 4, eCookingStage_AlphaAmylase, true );
910 911
        break;
      }
João Lino's avatar
João Lino committed
912 913
    case eCookingStage_AlphaAmylase: {
        xBasicStageOperation( alphaAmylaseTime, alphaAmylaseTemperature, 2, eCookingStage_Mashout, true );
914 915
        break;
      }
João Lino's avatar
João Lino committed
916 917
    case eCookingStage_Mashout: {
        xBasicStageOperation( mashoutTime, mashoutTemperature, 1, eCookingStage_Recirculation, true );
918 919
        break;
      }
João Lino's avatar
João Lino committed
920 921
    case eCookingStage_Recirculation: {
        xBasicStageOperation( recirculationTime, recirculationTemperature, 1, eCookingStage_Sparge, true );
922 923
        break;
      }
João Lino's avatar
João Lino committed
924 925
    case eCookingStage_Sparge: {
        xBasicStageOperation( spargeTime, spargeTemperature, 3, eCookingStage_Boil, false );
926 927
        break;
      }
João Lino's avatar
João Lino committed
928 929
    case eCookingStage_Boil: {
        xBasicStageOperation( boilTime, boilTemperature, 2, eCookingStage_Cooling, false );
930 931
        break;
      }
João Lino's avatar
João Lino committed
932 933
    case eCookingStage_Cooling: {
        xBasicStageOperation( coolingTime, coolingTemperature, 0, eCookingStage_Done, false );
934 935
        break;
      }
João Lino's avatar
João Lino committed
936 937
    case eCookingStage_Clean: {
        xBasicStageOperation( cleaningTime, cleaningTemperature, 0, eCookingStage_Done, false );
938 939
        break;
      }
João Lino's avatar
João Lino committed
940
    case eCookingStage_Purge: {
João Lino's avatar
João Lino committed
941 942 943 944
        iPumpSpeed = PUMP_SPEED_MAX_MOSFET;
        xRegulatePumpSpeed();
        break;
      }
João Lino's avatar
João Lino committed
945
    case eCookingStage_Done: {
João Lino's avatar
João Lino committed
946 947 948 949 950 951
        stopBrewing();                  // Update cooking state
        repaint = true;                 // Ask for screen refresh
        xWarnCookEnded();               // Warn the user that the cooking is done
        break;
      }
  }
João Lino's avatar
João Lino committed
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
}

// ##################################################### 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
970 971 972
        runMenuProcessor( &mdMainMenu );
        break;
      }
João Lino's avatar
João Lino committed
973
    case eMenuType_BeerProfile: {
João Lino's avatar
João Lino committed
974 975 976
        runMenuProcessor( &mdBeerProfileMenu );
        break;
      }
João Lino's avatar
João Lino committed
977
    case eMenuType_Stage: {
João Lino's avatar
João Lino committed
978 979 980
        runMenuProcessor( &mdStageMenu );
        break;
      }
João Lino's avatar
João Lino committed
981
    case eMenuType_Malt: {
João Lino's avatar
João Lino committed
982 983 984
        runMenuProcessor( &mdMaltMenu );
        break;
      }
João Lino's avatar
João Lino committed
985
    case eMenuType_Settings: {
João Lino's avatar
João Lino committed
986 987 988
        runMenuProcessor( &mdSettingsMenu );
        break;
      }
João Lino's avatar
João Lino committed
989
    case eMenuType_StartFromStage: {
João Lino's avatar
João Lino committed
990 991 992
        runMenuProcessor( &mdStartFromStageMenu );
        break;
      }
João Lino's avatar
João Lino committed
993 994 995 996 997
  }

#ifdef DEBUG_OFF
  if (debug_go) {
    debugPrintVar("repaint", repaint);
João Lino's avatar
João Lino committed
998
  }
João Lino's avatar
João Lino committed
999
#endif
João Lino's avatar
João Lino committed
1000 1001
}

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

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

João Lino's avatar
João Lino committed
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 1117 1118 1119 1120 1121 1122 1123 1124
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
1125
  }
João Lino's avatar
João Lino committed
1126 1127
  mdStartFromStageMenu._selection = eStageMenu_NULL;
}
1128

João Lino's avatar
João Lino committed
1129 1130 1131
void runBeerProfileSelection() {
  switch (mdBeerProfileMenu._selection) {
    case eBeerProfileMenu_Basic: {
João Lino's avatar
João Lino committed
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
        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;

1156
        printBeerProfile();
João Lino's avatar
João Lino committed
1157 1158 1159
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1160
    case eBeerProfileMenu_Trigo: {
João Lino's avatar
João Lino committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
        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
1184

1185
        printBeerProfile();
João Lino's avatar
João Lino committed
1186 1187 1188
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1189
    case eBeerProfileMenu_IPA: {
João Lino's avatar
João Lino committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
        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
1213

1214
        printBeerProfile();
João Lino's avatar
João Lino committed
1215 1216 1217
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1218
    case eBeerProfileMenu_Belga: {
João Lino's avatar
João Lino committed
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
        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
1242

1243
        printBeerProfile();
João Lino's avatar
João Lino committed
1244 1245 1246
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1247
    case eBeerProfileMenu_Red: {
João Lino's avatar
João Lino committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
        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;
1271 1272
        
        printBeerProfile();
João Lino's avatar
João Lino committed
1273 1274 1275
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1276
    case eBeerProfileMenu_APA: {
João Lino's avatar
João Lino committed
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
        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
1300

1301
        printBeerProfile();
João Lino's avatar
João Lino committed
1302 1303 1304
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1305
    case eBeerProfileMenu_Custom: {
João Lino's avatar
João Lino committed
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
        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
1329

1330
        printBeerProfile();
João Lino's avatar
João Lino committed
1331 1332 1333
        backToStatus();
        break;
      }
João Lino's avatar
João Lino committed
1334
    case eBeerProfileMenu_Back: {
João Lino's avatar
João Lino committed
1335 1336 1337
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1338
    default: {}
João Lino's avatar
João Lino committed
1339
  }
João Lino's avatar
João Lino committed
1340
  mdBeerProfileMenu._selection = eBeerProfileMenu_NULL;
1341 1342
}

João Lino's avatar
João Lino committed
1343 1344 1345
void runStageSelection() {
  switch (mdStageMenu._selection) {
    case eStageMenu_Startpoint: {
João Lino's avatar
João Lino committed
1346 1347 1348
        runStageSelection_Generic( &startpointTime, &startpointTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1349
    case eStageMenu_BetaGlucanase: {
João Lino's avatar
João Lino committed
1350 1351 1352
        runStageSelection_Generic( &betaGlucanaseTime, &betaGlucanaseTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1353
    case eStageMenu_Debranching: {
João Lino's avatar
João Lino committed
1354 1355 1356
        runStageSelection_Generic( &debranchingTime, &debranchingTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1357
    case eStageMenu_Proteolytic: {
João Lino's avatar
João Lino committed
1358 1359 1360
        runStageSelection_Generic( &proteolyticTime, &proteolyticTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1361
    case eStageMenu_BetaAmylase: {
João Lino's avatar
João Lino committed
1362 1363 1364
        runStageSelection_Generic( &betaAmylaseTime, &betaAmylaseTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1365
    case eStageMenu_AlphaAmylase: {
João Lino's avatar
João Lino committed
1366 1367 1368
        runStageSelection_Generic( &alphaAmylaseTime, &alphaAmylaseTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1369
    case eStageMenu_Mashout: {
João Lino's avatar
João Lino committed
1370 1371 1372
        runStageSelection_Generic( &mashoutTime, &mashoutTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1373
    case eStageMenu_Recirculation: {
João Lino's avatar
João Lino committed
1374 1375 1376
        runStageSelection_Generic( &recirculationTime, &recirculationTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1377
    case eStageMenu_Sparge: {
João Lino's avatar
João Lino committed
1378 1379 1380
        runStageSelection_Generic( &spargeTime, &spargeTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1381
    case eStageMenu_Boil: {
João Lino's avatar
João Lino committed
1382 1383 1384
        runStageSelection_Generic( &boilTime, &boilTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1385
    case eStageMenu_Cooling: {
João Lino's avatar
João Lino committed
1386 1387 1388
        runStageSelection_Generic( &coolingTime, &coolingTemperature );
        break;
      }
João Lino's avatar
João Lino committed
1389
    case eStageMenu_Back: {
João Lino's avatar
João Lino committed
1390 1391 1392
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1393
    default: {}
João Lino's avatar
João Lino committed
1394
  }
João Lino's avatar
João Lino committed
1395 1396
  mdStageMenu._selection = eStageMenu_NULL;
}
1397

João Lino's avatar
João Lino committed
1398 1399 1400
void runSettingsSelection() {
  switch (mdSettingsMenu._selection) {
    case eSettingsMenu_Pump: {
João Lino's avatar
João Lino committed
1401
        bool bNewPumpStatus = xSetGenericValue( iPumpSpeed ? 0 : 1, PUMP_SPEED_DEFAULT, 0, 1, "pump", "bool" );
João Lino's avatar
João Lino committed
1402
        if ( cancel ) {
João Lino's avatar
João Lino committed
1403 1404
          cancel = false;
        }
João Lino's avatar
João Lino committed
1405 1406 1407 1408 1409 1410 1411
        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
1412
        }
João Lino's avatar
João Lino committed
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
        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
1427
      }
João Lino's avatar
João Lino committed
1428
    case eSettingsMenu_Back: {
João Lino's avatar
João Lino committed
1429 1430 1431
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1432
    default: {}
João Lino's avatar
João Lino committed
1433
  }
João Lino's avatar
João Lino committed
1434
  mdSettingsMenu._selection = eSettingsMenu_NULL;
João Lino's avatar
Rel.3  
João Lino committed
1435 1436
}

João Lino's avatar
João Lino committed
1437 1438 1439 1440 1441
void runMaltSelection() {
  switch (mdMaltMenu._selection) {
    case eMaltMenu_CastleMalting_Chteau_Pilsen_2RS: {
        backToStatus();
        break;
João Lino's avatar
João Lino committed
1442
      }
João Lino's avatar
João Lino committed
1443 1444 1445
    case eMaltMenu_CastleMalting_Wheat_Blanc: {
        backToStatus();
        break;
João Lino's avatar
João Lino committed
1446
      }
João Lino's avatar
João Lino committed
1447
    case eMaltMenu_Back: {
João Lino's avatar
João Lino committed
1448 1449 1450
        resetMenu( true );
        break;
      }
João Lino's avatar
João Lino committed
1451
    default: {}
João Lino's avatar
Rel.3  
João Lino committed
1452
  }
João Lino's avatar
João Lino committed
1453
  mdMaltMenu._selection = eMaltMenu_NULL;
1454 1455
}

João Lino's avatar
João Lino committed
1456 1457 1458
// ************************ MENU HELPERS ******************************
void runMenuProcessor( MenuData *data ) {
  data->_position = rotaryEncoderVirtualPosition;         // Read position
1459

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

João Lino's avatar
João Lino committed
1463
  if ( checkForEncoderSwitchPush( true ) ) {              // Read selection
João Lino's avatar
João Lino committed
1464
    if ( cancel ) {
João Lino's avatar
João Lino committed
1465 1466 1467
      resetMenu( true );
      return;
    }
João Lino's avatar
João Lino committed
1468
    data->_selection = data->_position;
1469
  }
João Lino's avatar
João Lino committed
1470

João Lino's avatar
João Lino committed
1471
  (data->_selectionFunction)();                           // Run selection function
1472 1473
}

João Lino's avatar
João Lino committed
1474
void runStageSelection_Generic( unsigned long * selectedStageTime, int *selectedStageTemperature) {
João Lino's avatar
João Lino committed
1475 1476
  unsigned long selectedStageTimeStorage = *selectedStageTime;
  int selectedStageTemperatureStorage = *selectedStageTemperature;
João Lino's avatar
João Lino committed
1477

João Lino's avatar
João Lino committed
1478
  *selectedStageTime = getTimer( *selectedStageTime );
João Lino's avatar
João Lino committed
1479
  if ( cancel ) {
João Lino's avatar
João Lino committed
1480 1481 1482
    *selectedStageTime = selectedStageTimeStorage;
    cancel = false;
  }
João Lino's avatar
João Lino committed
1483 1484
  else {
    *selectedStageTemperature = getTemperature( *selectedStageTemperature );
João Lino's avatar
João Lino committed
1485
    if ( cancel ) {
João Lino's avatar
João Lino committed
1486 1487 1488 1489
      *selectedStageTime = selectedStageTimeStorage;
      *selectedStageTemperature = selectedStageTemperatureStorage;
      cancel = false;
    }
João Lino's avatar
João Lino committed
1490
  }
João Lino's avatar
João Lino committed
1491
  backToStatus();
1492 1493
}

João Lino's avatar
João Lino committed
1494 1495
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
1496 1497
}

João Lino's avatar
João Lino committed
1498 1499
void xStartStageInteractive( unsigned long *stageTime, int *stageTemperature, eCookingStages nextStage ) {
  xStartStage( stageTime, stageTemperature, nextStage, true, true, true, true );
1500 1501
}

João Lino's avatar
João Lino committed
1502
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
1503 1504 1505
  int finalYieldStorage = finalYield;
  unsigned long stageTimeStorage = *stageTime;
  int stageTemperatureStorage = *stageTemperature;
João Lino's avatar
João Lino committed
1506

João Lino's avatar
João Lino committed
1507
  if (bSetFinalYield) {
João Lino's avatar
João Lino committed
1508
    finalYield = getFinalYield( finalYield, SETTING_MACHINE_YIELD_DEFAULT );
João Lino's avatar
João Lino committed
1509
    if ( cancel ) {
João Lino's avatar
João Lino committed
1510
      finalYield = finalYieldStorage;
João Lino's avatar
João Lino committed
1511

João Lino's avatar
João Lino committed
1512
      cancel = false;
João Lino's avatar
João Lino committed
1513
      backToStatus();
João Lino's avatar
João Lino committed
1514 1515
      return;
    }
João Lino's avatar
Rel.3  
João Lino committed
1516
  }
João Lino's avatar
João Lino committed
1517
  if (bSetTime) {
João Lino's avatar
João Lino committed
1518
    (*stageTime) = getTimer( clockCounter / 1000, *stageTime );
João Lino's avatar
João Lino committed
1519
    if ( cancel ) {
João Lino's avatar
João Lino committed
1520 1521
      finalYield = finalYieldStorage;
      *stageTime = stageTimeStorage;
João Lino's avatar
João Lino committed
1522

João Lino's avatar
João Lino committed
1523
      cancel = false;
João Lino's avatar
João Lino committed
1524
      backToStatus();
João Lino's avatar
João Lino committed
1525 1526
      return;
    }
João Lino's avatar
Rel.3  
João Lino committed
1527
  }
João Lino's avatar
João Lino committed
1528
  if (bSetTemperature) {
João Lino's avatar
João Lino committed
1529
    (*stageTemperature) = getTemperature( cookTemperature, *stageTemperature );
João Lino's avatar
João Lino committed
1530
    if ( cancel ) {
João Lino's avatar
João Lino committed
1531 1532 1533
      finalYield = finalYieldStorage;
      *stageTime = stageTimeStorage;
      *stageTemperature = stageTemperatureStorage;
João Lino's avatar
João Lino committed
1534

João Lino's avatar
João Lino committed
1535
      cancel = false;
João Lino's avatar
João Lino committed
1536
      backToStatus();
João Lino's avatar
João Lino committed
1537 1538
      return;
    }
João Lino's avatar
Rel.3  
João Lino committed
1539
  }
João Lino's avatar
João Lino committed
1540

João Lino's avatar
João Lino committed
1541
  xSafeHardwarePowerOff();                      // Stop anything that might be still going on
João Lino's avatar
João Lino committed
1542 1543
  if (bPurgePump) {
    xPurgePump();
1544
  }
João Lino's avatar
João Lino committed
1545 1546 1547
  startBrewing();
  xSetupStage( nextStage );
  backToStatus();
1548
}
João Lino's avatar
João Lino committed
1549
// ##################################################### Menus ###################################################################
1550 1551 1552 1553

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

void startBrewing() {
1554 1555
  //sing(MELODY_SUPER_MARIO, PIEZO_PIN);

1556 1557 1558 1559 1560
  cooking = true;
}

void stopBrewing() {
  cooking = false;
1561
  xSafeHardwarePowerOff();
1562 1563
}

João Lino's avatar
João Lino committed
1564 1565 1566
void resetMenu( boolean requestRepaintPaint ) {
  eMenuType = eMenuType_Main;

1567
  if ( requestRepaintPaint ) {
João Lino's avatar
João Lino committed
1568 1569
    repaint = true;
  }
1570

João Lino's avatar
João Lino committed
1571
  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
1572 1573
}

1574 1575
void backToStatus() {
  lastInterruptTime = millis() - SETTING_MAX_INACTIVITY_TIME - 1;
João Lino's avatar
João Lino committed
1576
  resetMenu(true);
1577 1578 1579 1580
}
// #################################################### Helpers ##################################################################

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

João Lino's avatar
João Lino committed
1582 1583 1584 1585 1586 1587
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
1588

João Lino's avatar
João Lino committed
1589 1590 1591 1592 1593 1594
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
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614

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
1615 1616
    if ( checkForEncoderSwitchPush( true ) ) {                  // Check if pushbutton is pressed
      if ( cancel ) return rotaryEncoderVirtualPosition;
João Lino's avatar
João Lino committed
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
      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
1651
int getTimer( int initialValue, int defaultValue ) {
João Lino's avatar
João Lino committed
1652
  // 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
1653
  xSetupRotaryEncoder( eRotaryEncoderMode_Time, initialValue, 7200, 0, 1, 30 );
1654 1655

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1656
  int rotaryEncoderPreviousPosition = 0;
1657 1658
  int minutes = 0;
  int seconds = 0;
1659

1660 1661
  // Setup Screen
  lcd.clear();
1662
  lcd.home();
João Lino's avatar
João Lino committed
1663
  lcd.print("Set Time (");
1664
  minutes = defaultValue / 60;
João Lino's avatar
João Lino committed
1665
  lcd.print(minutes);
1666
  seconds = defaultValue - minutes * 60;
João Lino's avatar
João Lino committed
1667
  lcd.print(":");
1668
  if (seconds < 10) {
João Lino's avatar
João Lino committed
1669 1670 1671 1672
    lcd.print("0");
  }
  lcd.print(seconds);
  lcd.print(")");
1673
  lcd.setCursor (0, LCD_VERTICAL_RESOLUTION - 1);
1674
  lcd.print("      0:00");
1675 1676

  while (true) {
João Lino's avatar
João Lino committed
1677 1678
    if ( checkForEncoderSwitchPush( true ) ) {
      if ( cancel ) return rotaryEncoderVirtualPosition;
1679
      break;
João Lino's avatar
João Lino committed
1680 1681 1682
    }
    else {
      xManageMachineSystems();                            // Don't forget to keep an eye on the cooking
João Lino's avatar
João Lino committed
1683
    }
1684

1685
    // display current timer
service-config's avatar
Mixer  
service-config committed
1686 1687
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
1688 1689 1690 1691 1692 1693
      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
1694 1695
        lcd.print(" ");
      }
1696
      if (minutes < 10) {
João Lino's avatar
João Lino committed
1697 1698 1699
        lcd.print(" ");
      }
      lcd.print("    ");
1700 1701
      lcd.print(minutes);
      lcd.print(":");
1702
      if (seconds < 10) {
1703 1704 1705 1706 1707 1708
        lcd.print("0");
      }
      lcd.print(seconds);
      lcd.println("                ");
    }
  }
1709

service-config's avatar
Mixer  
service-config committed
1710
  return rotaryEncoderVirtualPosition;
1711 1712
}

João Lino's avatar
João Lino committed
1713 1714 1715 1716 1717 1718
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
1719 1720

      if ( ((millis() - cancleTimer) >= SETTING_CANCEL_TIMER ) && cancelable ) {
João Lino's avatar
João Lino committed
1721
        sing(BUZZ_1, PIEZO_PIN);
1722 1723
      }
    }
1724

João Lino's avatar
João Lino committed
1725
    if ( ((millis() - cancleTimer) >= SETTING_CANCEL_TIMER) && cancelable ) {
João Lino's avatar
João Lino committed
1726
      cancel = true;
1727 1728
    }
  }
1729

João Lino's avatar
João Lino committed
1730
  return gotPush;
João Lino's avatar
João Lino committed
1731 1732
}

João Lino's avatar
João Lino committed
1733 1734 1735 1736
unsigned long getInactivityTime() {
  unsigned long now = millis();
  unsigned long rotaryEncoderInactivityTime = now - lastInterruptTime;

1737
  if (rotaryEncoderInactivityTime > SETTING_MAX_INACTIVITY_TIME) {
João Lino's avatar
João Lino committed
1738
    if (checkForEncoderSwitchPush( false )) {
1739 1740 1741 1742 1743 1744
      now = millis();
      rotaryEncoderInactivityTime = now - lastInterruptTime;
      rotarySwDetectTime = now;

      repaint = true;
      refresh = true;
João Lino's avatar
João Lino committed
1745 1746
    }
  }
1747

João Lino's avatar
João Lino committed
1748 1749 1750 1751
  unsigned long switchInactivityTime = now - rotarySwDetectTime;
  return rotaryEncoderInactivityTime > switchInactivityTime ? switchInactivityTime : rotaryEncoderInactivityTime ;
}

1752
// ###################### Set Variables ##################################################
1753

João Lino's avatar
Rel.3  
João Lino committed
1754
void xWaitForAction(String title, String message) {
1755 1756
  unsigned long now = millis();
  unsigned long warningBeepTimeInterval = 0;
1757
  while (true) {
1758 1759
    now = millis();
    
João Lino's avatar
João Lino committed
1760
    if ( checkForEncoderSwitchPush( false ) ) {                  // Check if pushbutton is pressed
João Lino's avatar
Rel.3  
João Lino committed
1761
      break;
João Lino's avatar
João Lino committed
1762 1763
    }
    else {
João Lino's avatar
Rel.3  
João Lino committed
1764
      // Print the message
1765
      if (! lcdPrint(&lcd, title, message)) {
João Lino's avatar
Rel.3  
João Lino committed
1766 1767
        break;
      }
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863

      unsigned long now = millis();
      if( now - warningBeepTimeInterval > SETTING_WARNING_BEEP_INTERVAL ) {
        warningBeepTimeInterval = now;
        sing(BUZZ_2, PIEZO_PIN);
      }
    }
  }
}

void printBeerProfile( void ) {
  #ifdef INFO
  Serial.println("############# Beer Profile #############");
  Serial.print("#  beerProfile       ");
  Serial.print( mdBeerProfileMenu._dialog[beerProfile+1] );
  Serial.println("  #");
  Serial.println("############ Time per Stage ############");
  Serial.print("#  startpointTime              ");
  printTime(startpointTime           );
  Serial.println("  #");
  Serial.print("#  betaGlucanaseTime           ");
  printTime(betaGlucanaseTime        );
  Serial.println("  #");
  Serial.print("#  debranchingTime             ");
  printTime(debranchingTime          );
  Serial.println("  #");
  Serial.print("#  proteolyticTime             ");
  printTime(proteolyticTime          );
  Serial.println("  #");
  Serial.print("#  betaAmylaseTime             ");
  printTime(betaAmylaseTime          );
  Serial.println("  #");
  Serial.print("#  alphaAmylaseTime            ");
  printTime(alphaAmylaseTime         );
  Serial.println("  #");
  Serial.print("#  mashoutTime                 ");
  printTime(mashoutTime              );
  Serial.println("  #");
  Serial.print("#  recirculationTime           ");
  printTime(recirculationTime        );
  Serial.println("  #");
  Serial.print("#  spargeTime                  ");
  printTime(spargeTime               );
  Serial.println("  #");
  Serial.print("#  boilTime                    ");
  printTime(boilTime                 );
  Serial.println("  #");
  Serial.print("#  coolingTime                 ");
  printTime(coolingTime              );
  Serial.println("  #");
  Serial.println("######## Teperature per Stage ##########");
  Serial.print("#  startpointTemperature       ");
  printTemperature(startpointTemperature    );
  Serial.println("  #");
  Serial.print("#  betaGlucanaseTemperature    ");
  printTemperature(betaGlucanaseTemperature );
  Serial.println("  #");
  Serial.print("#  debranchingTemperature      ");
  printTemperature(debranchingTemperature   );
  Serial.println("  #");
  Serial.print("#  proteolyticTemperature      ");
  printTemperature(proteolyticTemperature   );
  Serial.println("  #");
  Serial.print("#  betaAmylaseTemperature      ");
  printTemperature(betaAmylaseTemperature   );
  Serial.println("  #");
  Serial.print("#  alphaAmylaseTemperature     ");
  printTemperature(alphaAmylaseTemperature  );
  Serial.println("  #");
  Serial.print("#  mashoutTemperature          ");
  printTemperature(mashoutTemperature       );
  Serial.println("  #");
  Serial.print("#  recirculationTemperature    ");
  printTemperature(recirculationTemperature );
  Serial.println("  #");
  Serial.print("#  spargeTemperature           ");
  printTemperature(spargeTemperature        );
  Serial.println("  #");
  Serial.print("#  boilTemperature             ");
  printTemperature(boilTemperature          );
  Serial.println("  #");
  Serial.print("#  coolingTemperature          ");
  printTemperature(coolingTemperature       );
  Serial.println("  #");
  Serial.println("########################################");
  if( beerProfile == eBeerProfile_Trigo) {
    Serial.print("#  finalYield                    ");
    if(finalYield < 10) {
      Serial.print(" ");
    }
    Serial.print(finalYield);
    Serial.println(" l  #");
    float wheatAmount = PROFILE_TRIGO_WHEAT_MULTIPLIER * ((float) finalYield);
    Serial.print("#  wheatAmount               ");
    if(wheatAmount < 10) {
      Serial.print(" ");
1864
    }
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
    Serial.print(wheatAmount);
    Serial.println(" Kg  #");
    float pilsnerAmount = PROFILE_TRIGO_PILSNER_MULTIPLIER * ((float) finalYield);
    Serial.print("#  pilsnerAmount             ");
    if(pilsnerAmount < 10) {
      Serial.print(" ");
    }
    Serial.print(pilsnerAmount);
    Serial.println(" Kg  #");
    float strikeWaterAmount = (pilsnerAmount + wheatAmount) * PROFILE_TRIGO_MASH_THICKNESS;
    Serial.print("#  strikeWaterAmount          ");
    if(strikeWaterAmount < 10) {
      Serial.print(" ");
    }
    Serial.print(strikeWaterAmount);
    Serial.println(" l  #");
    float spargeWaterAmount = PROFILE_TRIGO_SPARGE_WATER_MULTIPLIER * ((float) finalYield);
    Serial.print("#  spargeWaterAmount          ");
    if(spargeWaterAmount < 10) {
      Serial.print(" ");
    }
    Serial.print(spargeWaterAmount);
    Serial.println(" l  #");
    float herkulesAmount = PROFILE_TRIGO_HERKULES_MULTIPLIER * ((float) finalYield);
    Serial.print("#  herkulesAmount             ");
    if(herkulesAmount < 10) {
      Serial.print(" ");
    }
    Serial.print(herkulesAmount);
    Serial.println(" g  #");
    float traditionAmount = PROFILE_TRIGO_TRADITION_MULTIPLIER * ((float) finalYield);
    Serial.print("#  traditionAmount            ");
    if(traditionAmount < 10) {
      Serial.print(" ");
    }
    Serial.print(traditionAmount);
    Serial.println(" g  #");
    Serial.println("########################################");
  }
#endif
}

void printTime( unsigned long timeToPrint ) {
  unsigned long minutes = timeToPrint /60;
  unsigned long seconds = timeToPrint %60;

  if (minutes < 100) {
    Serial.print(" ");
  }
  if (minutes < 10) {
    Serial.print(" ");
  }
  Serial.print(minutes);
  Serial.print(":");
  if (seconds < 10) {
    Serial.print("0");
  }
  Serial.print(seconds);
}

void printTemperature( int temparatureToPrint ) {
  if (temparatureToPrint < 100) {
    Serial.print(" ");
  }
  if (temparatureToPrint < 10) {
    Serial.print(" ");
1931
  }
1932 1933 1934 1935
  Serial.print(temparatureToPrint);
  Serial.print(" ");
  Serial.write(176);
  Serial.print("C");
1936
}
1937