brew.ino 70.7 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
Rel.3  
João Lino committed
7
#define DEBUG
8

9
// ######################### LIBRARIES #########################
João Lino's avatar
Rel.3  
João Lino committed
10

11 12 13 14
// ++++++++++++++++++++++++ LiquidCrystal_I2C ++++++++++++++++++++++++
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
15

16
// ++++++++++++++++++++++++ PT100 +++++++++++++++++++++++++++++++++
João Lino's avatar
João Lino committed
17
//#include <PT100.h>
18

João Lino's avatar
Rel.3  
João Lino committed
19 20 21 22 23
// ++++++++++++++++++++++++ OTHER +++++++++++++++++++++++++++++++++
#include "debug.h"

#include "config.h"

24
#include "CustomDataStructures.h"
service-config's avatar
Mixer  
service-config committed
25

João Lino's avatar
Rel.3  
João Lino committed
26 27
#include "Melody.h"
#include "Display.h"
João Lino's avatar
João Lino committed
28 29 30 31
#include "Temperature.h"
#include "Profiles.h"

#include "brew.h"
32

33
// ######################### VARIABLES #########################
34
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
35
eRotaryEncoderMode      rotaryEncoderMode;
João Lino's avatar
Rel.3  
João Lino committed
36 37 38 39 40 41 42 43

eCookingStages          cookingStage;
eBeerProfile            beerProfile;

eMenuType               eMenuType;

eMainMenuOptions        eMainMenuPosition;
eMainMenuOptions        eMainMenuSelection;
João Lino's avatar
João Lino committed
44 45
eStageMenuOptions       eStartFromStageMenuPosition;
eStageMenuOptions       eStartFromStageMenuSelection;
João Lino's avatar
Rel.3  
João Lino committed
46 47 48 49 50 51 52 53 54
eBeerProfileMenuOptions eBeerProfileMenuPosition;
eBeerProfileMenuOptions eBeerProfileMenuSelection;
eStageMenuOptions       eStageMenuPosition;
eStageMenuOptions       eStageMenuSelection;
eMaltMenuOptions        eMaltMenuPosition;
eMaltMenuOptions        eMaltMenuSelection; 
eSettingsMenuOptions    eSettingsMenuPosition;
eSettingsMenuOptions    eSettingsMenuSelection;

55 56 57
eMaltMenuOptions        maltMenuOption;
eSettingsMenuOptions    settingsMenuOption;

58
// ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
59 60 61
boolean                 cooking;
boolean                 bStageFirstRun;

João Lino's avatar
Rel.3  
João Lino committed
62 63 64 65
unsigned long           clockStartTime;
unsigned long           clockLastUpdate;
long                    clockCounter;
unsigned long           clockIgnore;
João Lino's avatar
João Lino committed
66 67 68
boolean                 clockStart;
boolean                 clockEnd;

João Lino's avatar
Rel.3  
João Lino committed
69
unsigned long           cookTime;
João Lino's avatar
João Lino committed
70 71 72
int                     cookTemperature;
//cook_mode_list        cookMode;
//int                   cookMixerSpeed;
João Lino's avatar
Rel.3  
João Lino committed
73
int                     finalYield;
João Lino's avatar
João Lino committed
74
        
João Lino's avatar
Rel.3  
João Lino committed
75 76 77 78 79 80 81 82 83 84 85 86
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;
João Lino's avatar
João Lino committed
87 88 89 90 91 92 93 94 95 96 97 98
        
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
99
int                     cleaningTemperature;
João Lino's avatar
João Lino committed
100 101 102

boolean                 refresh;
boolean                 repaint;
103

João Lino's avatar
Rel.3  
João Lino committed
104 105
boolean                 bStatusElement;

106
// ++++++++++++++++++++++++ Interrupts ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
107
static unsigned long    lastInterruptTime;
108

109
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
110 111 112 113 114
volatile int            rotaryEncoderVirtualPosition = 0;
volatile int            rotaryEncoderMaxPosition = 1;
volatile int            rotaryEncoderMinPosition = 0;
volatile int            rotaryEncoderSingleStep = 1;
volatile int            rotaryEncoderMultiStep = 1;
115

João Lino's avatar
João Lino committed
116 117
volatile boolean        onISR = false;

118
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
119 120 121
int                     iWindowSize;             // Time frame to operate in
unsigned long           windowStartTime;
double                  dWattPerPulse;
122

123 124 125
// ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
int                     iPumpSpeed;             // Time frame to operate in

126
// ######################### INITIALIZE #########################
127
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
128
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);
129

João Lino's avatar
João Lino committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/* +++++++++++++++++++++++ PT100 +++++++++++++++++++++++
PT100                   basePT100("base", 
                          PT100_BASE_OUTPUT_PIN, 
                          PT100_BASE_INPUT_PIN, 
                          PT100_BASE_TIME_BETWEEN_READINGS, 
                          PT100_BASE_DEFAULT_ADC_VMAX, 
                          PT100_BASE_DEFAULT_VS, 
                          PT100_BASE_DEFAULT_R1_RESISTENCE);
PT100                   upPT100("up", 
                          PT100_UP_OUTPUT_PIN, 
                          PT100_UP_INPUT_PIN,  
                          PT100_UP_TIME_BETWEEN_READINGS, 
                          PT100_UP_DEFAULT_ADC_VMAX, 
                          PT100_UP_DEFAULT_VS, 
                          PT100_UP_DEFAULT_R1_RESISTENCE);
PT100                   downPT100("down", 
                          PT100_DOWN_OUTPUT_PIN,  
                          PT100_DOWN_INPUT_PIN, 
                          PT100_DOWN_TIME_BETWEEN_READINGS, 
                          PT100_DOWN_DEFAULT_ADC_VMAX, 
                          PT100_DOWN_DEFAULT_VS, 
                          PT100_DOWN_DEFAULT_R1_RESISTENCE);
*/

// +++++++++++++++++++++++ Temperature +++++++++++++++++++++++
Temperature                   basePT100("base", 
                          PT100_BASE_OUTPUT_PIN, 
                          PT100_BASE_INPUT_PIN, 
                          PT100_BASE_TIME_BETWEEN_READINGS, 
                          //2.1028, 2.0907, 659.91, 662.88);
                          //2.0998, 2.0998, 660.02, 662.02);
                          //2.0986, 2.0898, 660.06, 662.91);
                          //2.1353, 2.1043, 661.93, 659.7);
                          //2.0608, 2.058, 664.26, 661.15);
                          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.0949, 2.0835, 654.67, 657.57);
                          //2.079, 2.079, 655.52, 657.52);
                          //2.0893, 2.0832, 654.84, 657.58);
                          //2.1239, 2.1288, 654.89, 653.44);
                          //2.0564, 2.0539, 658.51, 655.78);
                          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.1016, 2.09, 653.02, 656.00);
                          //2.0998, 2.0998, 653.02, 655.02);
                          //2.0974, 2.0894, 653.17, 656.02);
                          //2.1347, 2.1407, 654.89, 651.84);
                          //2.0618, 2.0605, 657.16, 654.35);
                          2.0309, 2.0288, 658.15, 655.35);
185 186

// ######################### INTERRUPTS #########################
João Lino's avatar
João Lino committed
187
void isr ()  {    // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
188
  unsigned long interruptTime = millis();
João Lino's avatar
João Lino committed
189
  unsigned long diff = interruptTime - lastInterruptTime;
190
  
191
  // 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
192
  if (diff > ROTARY_ENCODER_DEBOUNCE_TIME) {
João Lino's avatar
João Lino committed
193
    lastInterruptTime = interruptTime;
194
  
João Lino's avatar
João Lino committed
195
    switch(rotaryEncoderMode) {
196 197 198 199
      // Input of rotary encoder controling menus
      case eRotaryEncoderMode_Menu: {
        if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
200 201
        }
        else {
202
            rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - rotaryEncoderSingleStep;
203
        }
204 205
        if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
206
        }
207 208
        if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
209 210 211 212
        }
        
        break;
      }
service-config's avatar
Mixer  
service-config committed
213
      
214 215
      // Input of rotary encoder controling time variables
      case eRotaryEncoderMode_Time: {
João Lino's avatar
João Lino committed
216
        if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
service-config's avatar
Mixer  
service-config committed
217
          if(rotaryEncoderVirtualPosition >= 60) {
218
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderMultiStep);
219 220
          }
          else {
221
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
222 223 224
          }
        }
        else {
225
          if(rotaryEncoderVirtualPosition == rotaryEncoderMinPosition) {
service-config's avatar
Mixer  
service-config committed
226
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 60);
227 228
          }
          else {
229 230
            if(rotaryEncoderVirtualPosition >= (60 + rotaryEncoderMultiStep)) {
              rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - rotaryEncoderMultiStep);
231 232
            }
            else {
233
              rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - rotaryEncoderSingleStep;
234 235 236
            }
          }
        }
237 238
        if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
239
        }
240 241
        if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
242 243 244 245
        }
        
        break;
      }
service-config's avatar
Mixer  
service-config committed
246
      
247 248 249
      // Input of rotary encoder controling generic integer variables within a range between rotaryEncoderMinPosition and rotaryEncoderMaxPosition
      case eRotaryEncoderMode_Generic: {
        if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
250
          rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
251 252
        }
        else {
253
          rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - rotaryEncoderSingleStep);
254
        }
service-config's avatar
Mixer  
service-config committed
255 256
        if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
257
        }
258 259
        if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
service-config's avatar
Mixer  
service-config committed
260 261 262 263
        }
       
        break;
      }
264 265 266 267
      default: {
        
      }
    }
João Lino's avatar
João Lino committed
268 269 270
    
    repaint = true;
    refresh = true;
271 272 273
  }
}

274 275 276 277 278 279 280 281 282
void xSetupRotaryEncoder( eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep ) {
  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;
}

283
// ######################### START #########################
284
void xSafeHardwarePowerOff() {
285
  // Turn off gracefully
João Lino's avatar
João Lino committed
286
  iPumpSpeed = PUMP_SPEED_STOP_MOSFET;
287 288 289
  xRegulatePumpSpeed();

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

João Lino's avatar
João Lino committed
294 295 296
  basePT100.setPumpStatus( false );
  upPT100.setPumpStatus( false );
  downPT100.setPumpStatus( false );
297

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

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

305 306 307
  // Play Melody;
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);

308
  //termometerCalibration();
João Lino's avatar
João Lino committed
309
  delay(SETTING_WELCOME_TIMEOUT);      // pause for effect
João Lino's avatar
Rel.3  
João Lino committed
310
  //#endif
311 312
}

313
void setup() {
João Lino's avatar
João Lino committed
314 315 316 317 318 319 320 321 322
  // ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
  pinMode                         (ROTARY_ENCODER_CLK_PIN,INPUT);
  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
323
  bStatusElement              =   false;
João Lino's avatar
João Lino committed
324 325 326 327 328
  windowStartTime             =   millis();
  dWattPerPulse               =   HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ;

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

329 330
  // ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
  pinMode(PUMP_PIN, OUTPUT);   // sets the pin as output
João Lino's avatar
João Lino committed
331
  iPumpSpeed                  =   PUMP_SPEED_STOP_MOSFET;             // Time frame to operate in
332 333
  analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255

João Lino's avatar
João Lino committed
334
  // ++++++++++++++++++++++++ Piezo ++++++++++++++++++++++++
335 336
  pinMode(PIEZO_PIN, OUTPUT);

João Lino's avatar
João Lino committed
337
  // ++++++++++++++++++++++++ Temperature Sensor PT100 ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
338

João Lino's avatar
João Lino committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  // ++++++++++++++++++++++++ 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 ++++++++++++++++++++++++
  lcd.begin                       (LCD_HORIZONTAL_RESOLUTION,LCD_VERTICAL_RESOLUTION);    //  <<----- My LCD was 16x2
  lcd.setBacklightPin             (LCD_BACKLIGHT_PIN,POSITIVE);        // Setup backlight pin
  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 );

  // ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
João Lino's avatar
Rel.3  
João Lino committed
354 355 356 357 358 359 360 361 362 363 364 365 366
  eMenuType                   =   eMenuType_Main;
  
  eMainMenuPosition           =   eMainMenu_GO;
  eMainMenuSelection          =   eMainMenu_NULL;
  eBeerProfileMenuPosition    =   eBeerProfileMenu_Basic;
  eBeerProfileMenuSelection   =   eBeerProfileMenu_NULL;
  eStageMenuPosition          =   eStageMenu_Startpoint;
  eStageMenuSelection         =   eStageMenu_NULL;
  eMaltMenuPosition           =   eMaltMenu_CastleMalting_Chteau_Pilsen_2RS;
  eMaltMenuSelection          =   eMaltMenu_NULL;
  eSettingsMenuPosition       =   eSettingsMenu_PT100_Element;
  eSettingsMenuSelection      =   eSettingsMenu_NULL;

João Lino's avatar
João Lino committed
367
  cookingStage                =   eCookingStage_Startpoint;
João Lino's avatar
Rel.3  
João Lino committed
368
  beerProfile                 =   eBeerProfile_Basic;
João Lino's avatar
João Lino committed
369
  // ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
João Lino's avatar
Rel.3  
João Lino committed
370

João Lino's avatar
João Lino committed
371
  cooking                     =   false;
372
  bStageFirstRun              =   true;
373

João Lino's avatar
João Lino committed
374
  clockStartTime              =   0;
João Lino's avatar
Rel.3  
João Lino committed
375
  clockLastUpdate             =   0;
João Lino's avatar
João Lino committed
376 377 378 379 380 381 382 383 384
  clockCounter                =   0;
  clockIgnore                 =   0;
  clockStart                  =   false;
  clockEnd                    =   false;
                        
  cookTime                    =   3600;
  cookTemperature             =   25;
  //cookMode                  =   quick_start;
  //cookMixerSpeed            =   120;
João Lino's avatar
Rel.3  
João Lino committed
385
  finalYield                  =   25;
João Lino's avatar
João Lino committed
386

João Lino's avatar
João Lino committed
387 388 389 390 391 392 393 394 395 396 397
  startpointTime              =   PROFILE_BASIC_STARTPOINT_TIME;
  betaGlucanaseTime           =   PROFILE_BASIC_BETAGLUCANASE_TIME;
  debranchingTime             =   PROFILE_BASIC_DEBRANCHING_TIME;
  proteolyticTime             =   PROFILE_BASIC_PROTEOLYTIC_TIME;
  betaAmylaseTime             =   PROFILE_BASIC_BETAAMYLASE_TIME;
  alphaAmylaseTime            =   PROFILE_BASIC_ALPHAAMYLASE_TIME;
  mashoutTime                 =   PROFILE_BASIC_MASHOUT_TIME;
  recirculationTime           =   PROFILE_BASIC_RECIRCULATION_TIME;
  spargeTime                  =   PROFILE_BASIC_SPARGE_TIME;
  boilTime                    =   PROFILE_BASIC_BOIL_TIME;
  coolingTime                 =   PROFILE_BASIC_COOLING_TIME;
João Lino's avatar
Rel.3  
João Lino committed
398
  cleaningTime                =   SETTING_CLEANING_TIME;
João Lino's avatar
João Lino committed
399

João Lino's avatar
João Lino committed
400 401 402 403 404 405 406 407 408 409 410
  startpointTemperature       =   PROFILE_BASIC_STARTPOINT_TEMPERATURE;
  betaGlucanaseTemperature    =   PROFILE_BASIC_BETAGLUCANASE_TEMPERATURE;
  debranchingTemperature      =   PROFILE_BASIC_DEBRANCHING_TEMPERATURE;
  proteolyticTemperature      =   PROFILE_BASIC_PROTEOLYTIC_TEMPERATURE;
  betaAmylaseTemperature      =   PROFILE_BASIC_BETAAMYLASE_TEMPERATURE;
  alphaAmylaseTemperature     =   PROFILE_BASIC_ALPHAAMYLASE_TEMPERATURE;
  mashoutTemperature          =   PROFILE_BASIC_MASHOUT_TEMPERATURE;
  recirculationTemperature    =   PROFILE_BASIC_RECIRCULATION_TEMPERATURE;
  spargeTemperature           =   PROFILE_BASIC_SPARGE_TEMPERATURE;
  boilTemperature             =   PROFILE_BASIC_BOIL_TEMPERATURE;
  coolingTemperature          =   PROFILE_BASIC_COOLING_TEMPERATURE;
João Lino's avatar
Rel.3  
João Lino committed
411
  cleaningTemperature         =   SETTING_CLEANING_TEMPERATURE;
João Lino's avatar
João Lino committed
412 413 414 415 416 417 418 419 420

  refresh                     =   true;
  repaint                     =   true;

  // ++++++++++++++++++++++++ Interrupts ++++++++++++++++++++++++
  lastInterruptTime           =   0;

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

João Lino's avatar
Rel.3  
João Lino committed
422
  // ######################### Code - Run Once #########################
João Lino's avatar
João Lino committed
423
  xSafeHardwarePowerOff           ();
João Lino's avatar
Rel.3  
João Lino committed
424
  xWelcomeUser                    ();
João Lino's avatar
João Lino committed
425
  
João Lino's avatar
Rel.3  
João Lino committed
426
  xSetupRotaryEncoder             ( eRotaryEncoderMode_Menu, eMainMenu_GO, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );
427
}
428

429
// ######################### MAIN LOOP #########################
430 431

void loop() {
432 433
  unsigned long inactivityTime = millis() - lastInterruptTime;

João Lino's avatar
João Lino committed
434
  if(inactivityTime > SETTING_MAX_INACTIVITY_TIME) {    // Inactivity check
435 436 437 438
    if(refresh) {
      repaint = true;
      refresh = false;
    }
João Lino's avatar
Rel.3  
João Lino committed
439
    repaint = displayStatus( &lcd, cooking, cookTemperature, basePT100.getCurrentTemperature(), upPT100.getCurrentTemperature(), downPT100.getCurrentTemperature(), clockCounter, repaint );
440 441
  }
  else {
João Lino's avatar
Rel.3  
João Lino committed
442
    runMenu();
443 444
  }
  
João Lino's avatar
Rel.3  
João Lino committed
445
  xManageMachineSystems();
446 447
}

448 449
// ######################### FUNCTIONS ########################

João Lino's avatar
Rel.3  
João Lino committed
450 451 452 453 454 455 456 457
void runMenu() {
  #ifdef DEBUG_OFF
  boolean debug_go = repaint;
  if(debug_go) {
    debugPrintFunction("runMenu");
    debugPrintVar("repaint", repaint);
    debugPrintVar("eMenuType", eMenuType);
    debugPrintVar("rotaryEncoderVirtualPosition", rotaryEncoderVirtualPosition);
João Lino's avatar
João Lino committed
458
  }
João Lino's avatar
Rel.3  
João Lino committed
459
  #endif
460

João Lino's avatar
Rel.3  
João Lino committed
461 462 463
  switch(eMenuType) {
    case eMenuType_Main: {
      eMainMenuPosition = static_cast<eMainMenuOptions>(rotaryEncoderVirtualPosition);
464

João Lino's avatar
Rel.3  
João Lino committed
465
      repaint = displayMainMenu( &lcd, eMainMenuPosition, repaint );
466

João Lino's avatar
Rel.3  
João Lino committed
467 468 469
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eMainMenuSelection = eMainMenuPosition;
      }
470

João Lino's avatar
Rel.3  
João Lino committed
471
      runMainMenuSelection();
João Lino's avatar
João Lino committed
472

João Lino's avatar
Rel.3  
João Lino committed
473 474
      break;
    }
João Lino's avatar
João Lino committed
475 476 477 478 479 480 481 482 483 484 485 486 487
    case eMenuType_StartFromStage: {
      eStartFromStageMenuPosition = static_cast<eStageMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displayStageMenu( &lcd, eStartFromStageMenuPosition, repaint );

      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eStartFromStageMenuSelection = eStartFromStageMenuPosition;
      }

      runStartFromStageSelection();
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
488 489 490 491
    case eMenuType_BeerProfile: {
      eBeerProfileMenuPosition = static_cast<eBeerProfileMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displayBeerProfileMenu( &lcd, eBeerProfileMenuPosition, repaint );
João Lino's avatar
João Lino committed
492

João Lino's avatar
Rel.3  
João Lino committed
493 494 495
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eBeerProfileMenuSelection = eBeerProfileMenuPosition;
      }
João Lino's avatar
João Lino committed
496

João Lino's avatar
Rel.3  
João Lino committed
497 498 499
      runBeerProfileSelection();
      
      break;
João Lino's avatar
João Lino committed
500
    }
João Lino's avatar
Rel.3  
João Lino committed
501 502 503 504
    case eMenuType_Stage: {
      eStageMenuPosition = static_cast<eStageMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displayStageMenu( &lcd, eStageMenuPosition, repaint );
João Lino's avatar
João Lino committed
505

João Lino's avatar
Rel.3  
João Lino committed
506 507 508
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eStageMenuSelection = eStageMenuPosition;
      }
509

João Lino's avatar
Rel.3  
João Lino committed
510 511 512
      runStageSelection();
      
      break;
513
    }
João Lino's avatar
Rel.3  
João Lino committed
514 515 516 517
    case eMenuType_Malt: {
      eMaltMenuPosition = static_cast<eMaltMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displayMaltMenu( &lcd, eMaltMenuPosition, repaint );
518

João Lino's avatar
Rel.3  
João Lino committed
519 520 521
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eMaltMenuSelection = eMaltMenuPosition;
      }
522

João Lino's avatar
Rel.3  
João Lino committed
523 524 525
      runMaltSelection();
      
      break;
526
    }
João Lino's avatar
Rel.3  
João Lino committed
527 528 529 530
    case eMenuType_Settings: {
      eSettingsMenuPosition = static_cast<eSettingsMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displaySettingsMenu( &lcd, eSettingsMenuPosition, repaint );
531

João Lino's avatar
Rel.3  
João Lino committed
532 533 534
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eSettingsMenuSelection = eSettingsMenuPosition;
      }
535

João Lino's avatar
Rel.3  
João Lino committed
536 537 538
      runSettingsSelection();
      
      break;
539 540
    }
  }
João Lino's avatar
Rel.3  
João Lino committed
541 542 543 544

  #ifdef DEBUG_OFF
  if(debug_go) {
    debugPrintVar("repaint", repaint);
545
  }
João Lino's avatar
Rel.3  
João Lino committed
546
  #endif
547 548
}

João Lino's avatar
Rel.3  
João Lino committed
549 550
void runSettingsSelection() {
  switch(eSettingsMenuSelection) {
João Lino's avatar
João Lino committed
551
    case eSettingsMenu_Pump: {
João Lino's avatar
Rel.3  
João Lino committed
552
      // Stuff
João Lino's avatar
João Lino committed
553 554 555 556 557 558
      if( xSetGenericValue( iPumpSpeed?0:1, 0, 1, "pump", "bool" ) ) {
        iPumpSpeed = PUMP_SPEED_MAX_MOSFET;
      } else {
        iPumpSpeed = PUMP_SPEED_STOP_MOSFET;
      }
      analogWrite(PUMP_PIN, iPumpSpeed);
João Lino's avatar
Rel.3  
João Lino committed
559 560

      backToStatus();
561
      
João Lino's avatar
João Lino committed
562 563 564 565 566 567
      break;
    }
    case eSettingsMenu_PT100_Element: {
      // Stuff

      backToStatus();
568 569 570
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
571 572 573 574
    case eSettingsMenu_PT100_Up: {
      // Stuff

      backToStatus();
575 576 577
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
578 579 580 581
    case eSettingsMenu_PT100_Down: {
      // Stuff

      backToStatus();
582
      
583 584
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    case eSettingsMenu_Back: {
      eMenuType = eMenuType_Main;
      repaint = true;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenuPosition, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );

      break;
    }
    default: {
    }
  }

  eSettingsMenuSelection = eSettingsMenu_NULL;
}

void runMaltSelection() {
  switch(eMaltMenuSelection) {
    case eMaltMenu_CastleMalting_Chteau_Pilsen_2RS: {
      // Stuff

      backToStatus();
607
      
608 609
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
610 611 612 613
    case eMaltMenu_CastleMalting_Wheat_Blanc: {
      // Stuff

      backToStatus();
614
      
615 616
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
617 618 619
    case eMaltMenu_Back: {
      eMenuType = eMenuType_Main;
      repaint = true;
620
      
João Lino's avatar
Rel.3  
João Lino committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenuPosition, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );

      break;
    }
    default: {
    }
  }

  eMaltMenuSelection = eMaltMenu_NULL;
}

void runStageSelection() {
  switch(eStageMenuSelection) {
    case eStageMenu_Startpoint: {
      startpointTime = getTimer( startpointTime );
  
João Lino's avatar
João Lino committed
638
      startpointTemperature = xSetGenericValue( startpointTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
639 640

      backToStatus();
service-config's avatar
Mixer  
service-config committed
641 642
      
      break;
643
    }
João Lino's avatar
Rel.3  
João Lino committed
644 645 646
    case eStageMenu_BetaGlucanase: {
      betaGlucanaseTime = getTimer( betaGlucanaseTime );
  
João Lino's avatar
João Lino committed
647
      betaGlucanaseTemperature = xSetGenericValue( betaGlucanaseTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
648 649

      backToStatus();
650
      
João Lino's avatar
Rel.3  
João Lino committed
651 652 653 654 655
      break;
    }
    case eStageMenu_Debranching: {
      debranchingTime = getTimer( debranchingTime );
  
João Lino's avatar
João Lino committed
656
      debranchingTemperature = xSetGenericValue( debranchingTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
657 658

      backToStatus();
659 660 661
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
662 663
    case eStageMenu_Proteolytic: {
      proteolyticTime = getTimer( proteolyticTime );
664
      
João Lino's avatar
João Lino committed
665
      proteolyticTemperature = xSetGenericValue( proteolyticTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
666 667

      backToStatus();
668 669
      
      break;
670
    }
João Lino's avatar
Rel.3  
João Lino committed
671 672 673
    case eStageMenu_BetaAmylase: {
      betaAmylaseTime = getTimer( betaAmylaseTime );
  
João Lino's avatar
João Lino committed
674
      betaAmylaseTemperature = xSetGenericValue( betaAmylaseTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
675 676

      backToStatus();
677
      
678 679
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
680 681 682
    case eStageMenu_AlphaAmylase: {
      alphaAmylaseTime = getTimer( alphaAmylaseTime );
  
João Lino's avatar
João Lino committed
683
      alphaAmylaseTemperature = xSetGenericValue( alphaAmylaseTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
684 685 686 687 688 689 690 691

      backToStatus();
      
      break;
    }
    case eStageMenu_Mashout: {
      mashoutTime = getTimer( mashoutTime );
  
João Lino's avatar
João Lino committed
692
      mashoutTemperature = xSetGenericValue( mashoutTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
693 694

      backToStatus();
695
      
696 697
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
698 699 700
    case eStageMenu_Recirculation: {
      recirculationTime = getTimer( recirculationTime );
  
João Lino's avatar
João Lino committed
701
      recirculationTemperature = xSetGenericValue( recirculationTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
702 703

      backToStatus();
704
      
João Lino's avatar
Rel.3  
João Lino committed
705 706 707 708 709
      break;
    }
    case eStageMenu_Sparge: {
      spargeTime = getTimer( spargeTime );
  
João Lino's avatar
João Lino committed
710
      spargeTemperature = xSetGenericValue( spargeTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
711 712

      backToStatus();
713 714 715
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
716 717
    case eStageMenu_Boil: {
      boilTime = getTimer( boilTime );
718
      
João Lino's avatar
João Lino committed
719
      boilTemperature = xSetGenericValue( boilTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
720 721

      backToStatus();
722
      
723 724
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
725 726
    case eStageMenu_Cooling: {
      coolingTime = getTimer( coolingTime );
727
      
João Lino's avatar
João Lino committed
728
      coolingTemperature = xSetGenericValue( coolingTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
729 730

      backToStatus();
731 732 733
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
734 735 736
    case eStageMenu_Back: {
      eMenuType = eMenuType_Main;
      repaint = true;
737
      
João Lino's avatar
Rel.3  
João Lino committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenuPosition, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );

      break;
    }
    default: {
    }
  }

  eStageMenuSelection = eStageMenu_NULL;
}

void runBeerProfileSelection() {
  switch(eBeerProfileMenuSelection) {
    case eBeerProfileMenu_Basic: {
      beerProfile                 =   eBeerProfile_Basic;

João Lino's avatar
João Lino committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
      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;
João Lino's avatar
Rel.3  
João Lino committed
778 779 780 781 782 783 784 785

      backToStatus();
          
      break;
    }
    case eBeerProfileMenu_Trigo: {
      beerProfile                 =   eBeerProfile_Trigo;

João Lino's avatar
João Lino committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
      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
Rel.3  
João Lino committed
809 810

      backToStatus();
811 812 813
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
814 815 816
    case eBeerProfileMenu_IPA: {
      beerProfile                 =   eBeerProfile_IPA;

João Lino's avatar
João Lino committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
      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
Rel.3  
João Lino committed
841 842
      backToStatus();

843 844
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
845 846 847
    case eBeerProfileMenu_Belga: {
      beerProfile                 =   eBeerProfile_Belga;

João Lino's avatar
João Lino committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
      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;
871
      
João Lino's avatar
João Lino committed
872
      backToStatus();
873 874 875
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
876 877
    case eBeerProfileMenu_Red: {
      beerProfile                 =   eBeerProfile_Red;
João Lino's avatar
João Lino committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
      
      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;
902

João Lino's avatar
Rel.3  
João Lino committed
903 904
      backToStatus();
      
905 906
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
907 908 909
    case eBeerProfileMenu_APA: {
      beerProfile                 =   eBeerProfile_APA;

João Lino's avatar
João Lino committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
      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;
933
      
João Lino's avatar
João Lino committed
934
      backToStatus();
935 936 937
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
938 939 940
    case eBeerProfileMenu_Custom: {
      beerProfile                 =   eBeerProfile_Custom;

João Lino's avatar
João Lino committed
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
      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
Rel.3  
João Lino committed
964 965 966 967 968 969 970 971 972 973 974

      backToStatus();
      
      break;
    }
    case eBeerProfileMenu_Back: {
      eMenuType = eMenuType_Main;
      repaint = true;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenuPosition, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );
975

976 977 978
      break;
    }
    default: {
João Lino's avatar
Rel.3  
João Lino committed
979
    }
980
  }
981

João Lino's avatar
Rel.3  
João Lino committed
982
  eBeerProfileMenuSelection = eBeerProfileMenu_NULL;
983 984
}

João Lino's avatar
João Lino committed
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
void runStartFromStageSelection_Processor( unsigned long *stageTime, int *stageTemperature, eCookingStages nextStage ) {
  // Stop anything that might be still going on
  xSafeHardwarePowerOff();

  finalYield = xSetFinalYield( finalYield );
  (*stageTime) = getTimer( clockCounter/1000, (*stageTime) );
  (*stageTemperature) = xSetTemperature( (*stageTemperature) );

  xSetupGlobalVariablesForStage( nextStage );

  startBrewing();

  backToStatus();

  xPurgePump();
}

void runStartFromStageSelection() {
  switch(eStartFromStageMenuSelection) {
    case eStageMenu_Startpoint: {
      runStartFromStageSelection_Processor( &startpointTime, &startpointTemperature, eCookingStage_Startpoint );
      break;
    }
    case eStageMenu_BetaGlucanase: {
      runStartFromStageSelection_Processor( &betaGlucanaseTime, &betaGlucanaseTemperature, eCookingStage_BetaGlucanase );
      break;
    }
    case eStageMenu_Debranching: {
      runStartFromStageSelection_Processor( &debranchingTime, &debranchingTemperature, eCookingStage_Debranching );
      break;
    }
    case eStageMenu_Proteolytic: {
      runStartFromStageSelection_Processor( &proteolyticTime, &proteolyticTemperature, eCookingStage_Proteolytic );
      break;
    }
    case eStageMenu_BetaAmylase: {
      runStartFromStageSelection_Processor( &betaAmylaseTime, &betaAmylaseTemperature, eCookingStage_BetaAmylase );
      break;
    }
    case eStageMenu_AlphaAmylase: {
      runStartFromStageSelection_Processor( &alphaAmylaseTime, &alphaAmylaseTemperature, eCookingStage_AlphaAmylase );
      break;
    }
    case eStageMenu_Mashout: {
      runStartFromStageSelection_Processor( &mashoutTime, &mashoutTemperature, eCookingStage_Mashout );
      break;
    }
    case eStageMenu_Recirculation: {
      runStartFromStageSelection_Processor( &recirculationTime, &recirculationTemperature, eCookingStage_Recirculation );
      break;
    }
    case eStageMenu_Sparge: {
      runStartFromStageSelection_Processor( &spargeTime, &spargeTemperature, eCookingStage_Sparge );
      break;
    }
    case eStageMenu_Boil: {
      runStartFromStageSelection_Processor( &boilTime, &boilTemperature, eCookingStage_Boil );
      break;
    }
    case eStageMenu_Cooling: {
      runStartFromStageSelection_Processor( &coolingTime, &coolingTemperature, eCookingStage_Cooling );
      break;
    }
    case eStageMenu_Back: {
      resetMenu( true );
      break;
    }
    default: {
    }
  }
  eStartFromStageMenuSelection = eStageMenu_NULL;
}

João Lino's avatar
Rel.3  
João Lino committed
1058 1059
void runMainMenuSelection() {
  switch(eMainMenuSelection) {
João Lino's avatar
João Lino committed
1060 1061 1062 1063 1064 1065 1066 1067 1068
    case eMainMenu_GO_FROM_STAGE: {
      eMenuType = eMenuType_StartFromStage;
      repaint = true;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eBeerProfileMenuPosition, MENU_SIZE_PROFILES_MENU - 1, 1, 1, 0 );
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
1069
    case eMainMenu_GO: {
João Lino's avatar
João Lino committed
1070
      finalYield = xSetFinalYield( finalYield );
1071

João Lino's avatar
Rel.3  
João Lino committed
1072
      startBrewing();
1073

João Lino's avatar
Rel.3  
João Lino committed
1074
      xSetupGlobalVariablesForStage( eCookingStage_Startpoint );
1075

João Lino's avatar
Rel.3  
João Lino committed
1076 1077 1078
      backToStatus();
      
      xPurgePump();
1079

João Lino's avatar
Rel.3  
João Lino committed
1080 1081 1082 1083
      break;
    }
    case eMainMenu_STOP: {
      stopBrewing();
1084

João Lino's avatar
Rel.3  
João Lino committed
1085 1086 1087 1088 1089 1090
      backToStatus();
      
      break;
    }
    case eMainMenu_SKIP: {
      cookTime = 0;
1091

João Lino's avatar
Rel.3  
João Lino committed
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 1125 1126 1127 1128 1129 1130
      backToStatus();
      
      break;
    }
    case eMainMenu_BeerProfile: {
      eMenuType = eMenuType_BeerProfile;
      repaint = true;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eBeerProfileMenuPosition, MENU_SIZE_PROFILES_MENU - 1, 1, 1, 0 );
      
      break;
    }
    case eMainMenu_Stage: {
      eMenuType = eMenuType_Stage;
      repaint = true;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eStageMenuPosition, MENU_SIZE_STAGE_MENU - 1, 1, 1, 0 );
      
      break;
    }
    case eMainMenu_Malt: {
      eMenuType = eMenuType_Malt;
      repaint = true;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMaltMenuPosition, MENU_SIZE_MALT_MENU - 1, 1, 1, 0 );
      
      break;
    }
    case eMainMenu_Hops: {
      backToStatus();
      
      break;
    }
    case eMainMenu_Clean: {
      // Stop anything that might be still going on
      xSafeHardwarePowerOff();
1131

João Lino's avatar
Rel.3  
João Lino committed
1132 1133
      // Start at the Clean stage
      startBrewing();
1134

João Lino's avatar
Rel.3  
João Lino committed
1135
      xSetupGlobalVariablesForStage( eCookingStage_Clean );
1136

João Lino's avatar
Rel.3  
João Lino committed
1137 1138 1139
      backToStatus();
      
      xPurgePump();
1140

João Lino's avatar
Rel.3  
João Lino committed
1141 1142 1143 1144 1145
      break;
    }
    case eMainMenu_Purge: {
      // Stop anything that might be still going on
      xSafeHardwarePowerOff();
1146

João Lino's avatar
Rel.3  
João Lino committed
1147 1148
      // Start at the Purge stage
      startBrewing();
1149

João Lino's avatar
Rel.3  
João Lino committed
1150
      xSetupGlobalVariablesForStage( eCookingStage_Purge );
1151

João Lino's avatar
Rel.3  
João Lino committed
1152 1153 1154
      backToStatus();
      
      xPurgePump();
1155

João Lino's avatar
Rel.3  
João Lino committed
1156 1157 1158 1159 1160
      break;
    }
    case eMainMenu_Settings: {
      eMenuType = eMenuType_Settings;
      repaint = true;
1161

João Lino's avatar
Rel.3  
João Lino committed
1162 1163 1164 1165 1166 1167 1168
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eSettingsMenuPosition, MENU_SIZE_SETTINGS_MENU - 1, 1, 1, 0 );
      
      break;
    }
    case eMainMenu_Back: {
      backToStatus();
1169

João Lino's avatar
Rel.3  
João Lino committed
1170 1171 1172 1173 1174
      break;
    }
    default: {
    }
  }
1175

João Lino's avatar
Rel.3  
João Lino committed
1176
  eMainMenuSelection = eMainMenu_NULL;
1177 1178
}

João Lino's avatar
Rel.3  
João Lino committed
1179
void xCountTheTime( int temperatureRange, boolean bAverageUpDown ) {
1180
  unsigned long now = millis();
João Lino's avatar
Rel.3  
João Lino committed
1181 1182 1183 1184 1185 1186 1187
  unsigned long elapsedTime = now - clockLastUpdate;
  double temperatureCount = 0;
  
  if( bAverageUpDown ) {
    float tup = upPT100.getCurrentTemperature();
    float tdown = downPT100.getCurrentTemperature();
    if(tup > tdown) {
João Lino's avatar
João Lino committed
1188
      temperatureCount = tdown;
João Lino's avatar
Rel.3  
João Lino committed
1189 1190
    }
    else {
João Lino's avatar
João Lino committed
1191
      temperatureCount = tup;
João Lino's avatar
Rel.3  
João Lino committed
1192 1193 1194 1195
    }
  } else {
    temperatureCount = basePT100.getCurrentTemperature();
  }
João Lino's avatar
João Lino committed
1196

João Lino's avatar
João Lino committed
1197
  // Check if the machine is in the right temperature range, for the current mode,
João Lino's avatar
Rel.3  
João Lino committed
1198 1199
  //if(!( temperatureCount > (cookTemperature - temperatureRange) && temperatureCount < (cookTemperature + temperatureRange))) {
  float margin = temperatureRange;
João Lino's avatar
João Lino committed
1200
/*  if( cookTemperature >= 100.0 ) {
João Lino's avatar
Rel.3  
João Lino committed
1201
    margin = 2.0;
João Lino's avatar
João Lino committed
1202 1203
  }*/

João Lino's avatar
Rel.3  
João Lino committed
1204 1205
  if( temperatureCount < (cookTemperature - margin) ) {
    clockIgnore += elapsedTime;
João Lino's avatar
João Lino committed
1206
  }
João Lino's avatar
João Lino committed
1207 1208 1209 1210 1211 1212 1213 1214
  /*else {
    if( (temperatureCount >= (cookTemperature - margin)) && (temperatureCount < cookTemperature ) {
      clockIgnore += elapsedTime / margin * (cookTemperature - temperatureCount) ;
    }
    else {
      
    }
  }*/
João Lino's avatar
João Lino committed
1215 1216
  
  // Calculate the remaining time on the clock
João Lino's avatar
João Lino committed
1217
  clockCounter = cookTime * 1000 - (now - clockStartTime - clockIgnore);
João Lino's avatar
João Lino committed
1218

João Lino's avatar
Rel.3  
João Lino committed
1219 1220 1221 1222 1223 1224 1225
  if ( clockCounter < 0 ) {
    clockCounter = 0;
  }

  clockLastUpdate = now;

  #ifdef DEBUG_OFF
1226 1227
  debugPrintFunction("xCountTheTime");
  debugPrintVar("millis()", now);
João Lino's avatar
João Lino committed
1228
  debugPrintVar("cookTime", cookTime);
1229 1230 1231
  debugPrintVar("clockStartTime", clockStartTime);
  debugPrintVar("clockIgnore", clockIgnore);
  debugPrintVar("clockCounter", clockCounter); 
João Lino's avatar
Rel.3  
João Lino committed
1232
  #endif
1233 1234 1235
}

bool isTimeLeft() {
1236 1237
  if( clockCounter > 0 ) {
    return true;
João Lino's avatar
João Lino committed
1238
  }
1239
  return false;
1240 1241
}

1242
//HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ
1243 1244
double ulWattToWindowTime( double ulAppliedWatts ) {
  double ulPulsesRequired = ulAppliedWatts / dWattPerPulse;
1245
  return (double)iWindowSize / 1000.0 * ulPulsesRequired * 1000.0 / HEATING_ELEMENT_AC_FREQUENCY_HZ;
1246 1247
}

João Lino's avatar
Rel.3  
João Lino committed
1248 1249
bool xRegulateTemperature( boolean bAverageUpDown ) {
  double difference = 0;
João Lino's avatar
João Lino committed
1250 1251 1252
  bool overTemperature = false;
  double wattage = 0.0;
  
João Lino's avatar
João Lino committed
1253 1254 1255
  float tup = upPT100.getCurrentTemperature();
  float tdown = downPT100.getCurrentTemperature();
  float tbase = basePT100.getCurrentTemperature();
João Lino's avatar
Rel.3  
João Lino committed
1256

João Lino's avatar
João Lino committed
1257
  if( bAverageUpDown ) {
João Lino's avatar
Rel.3  
João Lino committed
1258 1259 1260 1261 1262 1263 1264
    if(tup > tdown) {
      difference = cookTemperature - tup;
    }
    else {
      difference = cookTemperature - tdown;
    }

João Lino's avatar
João Lino committed
1265 1266
    if(tbase > cookTemperature && (tbase >= (PUMP_TEMPERATURE_MAX_OPERATION - 2.0) || difference >= 5.0)) {
      difference = cookTemperature - tbase;
João Lino's avatar
Rel.3  
João Lino committed
1267 1268
    }

João Lino's avatar
João Lino committed
1269
    if( (tbase < cookTemperature) && (difference < (cookTemperature - tbase)) ) {
João Lino's avatar
Rel.3  
João Lino committed
1270 1271 1272
      difference = cookTemperature - tbase;
    }
  } else {
João Lino's avatar
João Lino committed
1273
    difference = cookTemperature - tbase;
João Lino's avatar
Rel.3  
João Lino committed
1274 1275
  }

João Lino's avatar
João Lino committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
  // Deviation between the cook temperature set and the cook temperature measured
  if( difference < 0.0 ) {
    difference = difference * (-1.0);
    overTemperature = true;
  }
  
  // Calculate applied wattage, based on the distance from the target temperature
  if( overTemperature ) {
    // turn it off
    wattage = 0.0;
  } else {
João Lino's avatar
João Lino committed
1287
    //if(difference <= 0.1) {
João Lino's avatar
João Lino committed
1288
      // turn it off
João Lino's avatar
João Lino committed
1289 1290
    //  wattage = 0.0;
    //} else {
João Lino's avatar
Rel.3  
João Lino committed
1291
      if(difference <= 0.5) {
João Lino's avatar
João Lino committed
1292
        // pulse lightly at 500 watt
João Lino's avatar
Rel.3  
João Lino committed
1293
        if(cookTemperature > 99.0) {
João Lino's avatar
João Lino committed
1294
          wattage = 2000.0;
João Lino's avatar
Rel.3  
João Lino committed
1295 1296
        }
        else {
João Lino's avatar
João Lino committed
1297 1298 1299 1300 1301 1302
          if(cookTemperature > 70.0) {
            wattage = 1000.0;
          }
          else {
            wattage = 500.0;
          }
João Lino's avatar
Rel.3  
João Lino committed
1303
        }
João Lino's avatar
João Lino committed
1304
      } else {
João Lino's avatar
Rel.3  
João Lino committed
1305
        if(difference <= 1.0) {
João Lino's avatar
João Lino committed
1306
          // pulse moderately at 1000 watt
João Lino's avatar
João Lino committed
1307 1308 1309 1310 1311 1312 1313
          if(cookTemperature > 99.0) {
            wattage = 2000.0;
          }
          else {
            wattage = 1000.0;
          }
          
João Lino's avatar
João Lino committed
1314
        } else {
João Lino's avatar
Rel.3  
João Lino committed
1315
          if(difference <= 3.0) {
João Lino's avatar
João Lino committed
1316
            // pulse hardly at 2000 watt
1317
            wattage = 2000.0;
João Lino's avatar
João Lino committed
1318 1319 1320 1321 1322 1323
          } else {
            //pulse constantly at HEATING_ELEMENT_MAX_WATTAGE watt
            wattage = HEATING_ELEMENT_MAX_WATTAGE;
          }
        }
      }
João Lino's avatar
João Lino committed
1324
    //}
João Lino's avatar
João Lino committed
1325 1326 1327
  }
  
  // Update the recorded time for the begining of the window, if the previous window has passed
1328 1329
  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;
1330
  }
João Lino's avatar
João Lino committed
1331 1332
  
  // Apply wattage to the element at the right time
1333
  if( ulWattToWindowTime( wattage ) > (millis() - windowStartTime) ) {
1334
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN,HIGH);
João Lino's avatar
Rel.3  
João Lino committed
1335
    bStatusElement = true;
João Lino's avatar
João Lino committed
1336 1337
  } else {
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN,LOW);
João Lino's avatar
Rel.3  
João Lino committed
1338
    bStatusElement = false;
João Lino's avatar
João Lino committed
1339
  }
1340

João Lino's avatar
Rel.3  
João Lino committed
1341
  #ifdef DEBUG_OFF
1342
  //debugPrintFunction("xRegulateTemperature");
1343
  debugPrintVar("difference", difference);
1344
  //debugPrintVar("overTemperature", overTemperature);
1345
  debugPrintVar("wattage", wattage);
1346 1347 1348 1349
  //debugPrintVar("ulWattToWindowTime( wattage )", ulWattToWindowTime( wattage ) );
  //debugPrintVar("millis()", millis());
  //debugPrintVar("windowStartTime", windowStartTime);
  //debugPrintVar("test", ulWattToWindowTime( wattage ) > (millis() - windowStartTime) ); 
João Lino's avatar
Rel.3  
João Lino committed
1350 1351 1352 1353 1354
  #endif
}

void xPurgePump() {
  for(int i = 0; i < 2; i++) {
João Lino's avatar
João Lino committed
1355
    analogWrite(PUMP_PIN, PUMP_SPEED_MAX_MOSFET);  // analogWrite values from 0 to 255
João Lino's avatar
Rel.3  
João Lino committed
1356
    delay(1000);
João Lino's avatar
João Lino committed
1357
    analogWrite(PUMP_PIN, PUMP_SPEED_STOP_MOSFET);  // analogWrite values from 0 to 255
João Lino's avatar
Rel.3  
João Lino committed
1358 1359
    delay(1500);
  }
1360 1361
}

1362
bool xRegulatePumpSpeed() {
João Lino's avatar
Rel.3  
João Lino committed
1363 1364 1365
  //  analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255

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

João Lino's avatar
João Lino committed
1368 1369 1370
    basePT100.setPumpStatus( false );
    upPT100.setPumpStatus( false );
    downPT100.setPumpStatus( false );
1371 1372
  }
  else {
João Lino's avatar
Rel.3  
João Lino committed
1373 1374
    analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255

João Lino's avatar
João Lino committed
1375 1376 1377
    basePT100.setPumpStatus( true );
    upPT100.setPumpStatus( true );
    downPT100.setPumpStatus( true );
1378
  }
1379 1380
}

1381
void xWarnClockEnded() {
1382
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);
1383 1384
}

João Lino's avatar
Rel.3  
João Lino committed
1385 1386 1387 1388
void xWarnCookEnded() {
  sing(MELODY_UNDERWORLD_SHORT, PIEZO_PIN);
}

João Lino's avatar
João Lino committed
1389
void xStageFirstRun( int stageTime, int stageTemperature, int stagePumpSpeed, eCookingStages stage ) {
João Lino's avatar
Rel.3  
João Lino committed
1390 1391 1392 1393 1394
  #ifdef DEBUG_OFF
  debugPrintFunction("xStageFirstRun");
  debugPrintVar("cookingStage", stage);
  #endif

João Lino's avatar
João Lino committed
1395 1396 1397 1398
  // Set Stage
  bStageFirstRun = true;
  cookingStage = stage;

João Lino's avatar
João Lino committed
1399 1400 1401 1402 1403 1404 1405 1406
  // Set the clock
  cookTime = stageTime;
  
  // Set the target temperature
  cookTemperature = stageTemperature;
  
  // Reset the clock
  clockStartTime = millis();
João Lino's avatar
Rel.3  
João Lino committed
1407
  clockLastUpdate = clockStartTime;
João Lino's avatar
João Lino committed
1408
  clockIgnore = 0;
1409 1410 1411

  // Set the pump speed
  iPumpSpeed = stagePumpSpeed;
1412 1413
}

João Lino's avatar
Rel.3  
João Lino committed
1414 1415 1416 1417 1418
void xSetupGlobalVariablesForStage(eCookingStages nextStage) {
  #ifdef DEBUG_OFF
  debugPrintFunction("xSetupGlobalVariablesForStage");
  debugPrintVar("cookingStage", nextStage);
  #endif
João Lino's avatar
João Lino committed
1419 1420 1421 1422

  // Operate the machine according to the current mode
  switch(nextStage) {
    case eCookingStage_Startpoint: {
João Lino's avatar
Rel.3  
João Lino committed
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
      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.");

          xWaitForAction("Malt", say);

          repaint = true;

          break;
        }
João Lino's avatar
João Lino committed
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
        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.");

          xWaitForAction("Malt", say);

          repaint = true;

          break;
        }
João Lino's avatar
Rel.3  
João Lino committed
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
        default: {

        }
      }

      // Make sure there is water
      xWaitForAction("Water", "Make sure there is water in the machine before start cooking.");

      repaint = true;

João Lino's avatar
João Lino committed
1472
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1473
      xStageFirstRun( startpointTime, startpointTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Startpoint );
João Lino's avatar
João Lino committed
1474 1475 1476 1477
      
      break;
    }
    case eCookingStage_BetaGlucanase: {
João Lino's avatar
Rel.3  
João Lino committed
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
      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.");

          xWaitForAction("Malt", say);

          repaint = true;

          break;
        }
João Lino's avatar
João Lino committed
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
        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.");

          xWaitForAction("Malt", say);

          repaint = true;

          break;
        }
João Lino's avatar
Rel.3  
João Lino committed
1517 1518 1519 1520 1521
        default: {

        }
      }

João Lino's avatar
João Lino committed
1522
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1523
      xStageFirstRun( betaGlucanaseTime, betaGlucanaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_BetaGlucanase );
João Lino's avatar
João Lino committed
1524 1525 1526 1527 1528
      
      break;
    }
    case eCookingStage_Debranching: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1529
      xStageFirstRun( debranchingTime, debranchingTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Debranching );
João Lino's avatar
João Lino committed
1530 1531 1532 1533 1534
      
      break;
    }
    case eCookingStage_Proteolytic: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1535
      xStageFirstRun( proteolyticTime, proteolyticTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Proteolytic );
João Lino's avatar
João Lino committed
1536 1537 1538 1539 1540
      
      break;
    }
    case eCookingStage_BetaAmylase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1541
      xStageFirstRun( betaAmylaseTime, betaAmylaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_BetaAmylase );
João Lino's avatar
João Lino committed
1542 1543 1544 1545 1546
      
      break;
    }
    case eCookingStage_AlphaAmylase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1547
      xStageFirstRun( alphaAmylaseTime, alphaAmylaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_AlphaAmylase );
João Lino's avatar
João Lino committed
1548 1549 1550 1551 1552
      
      break;
    }
    case eCookingStage_Mashout: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1553
      xStageFirstRun( mashoutTime, mashoutTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Mashout );
João Lino's avatar
João Lino committed
1554 1555 1556
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
1557 1558 1559 1560 1561
    case eCookingStage_Recirculation: {// Make sure there is water
      xWaitForAction("Sparge Water", "Start heating your sparge water.");

      repaint = true;

João Lino's avatar
João Lino committed
1562
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1563
      xStageFirstRun( recirculationTime, recirculationTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Recirculation );
João Lino's avatar
João Lino committed
1564 1565 1566 1567
      
      break;
    }
    case eCookingStage_Sparge: {
João Lino's avatar
Rel.3  
João Lino committed
1568 1569 1570 1571 1572
      // Make sure there is water
      xWaitForAction("Sparge Water", "Start pouring the sparge water.");

      repaint = true;

João Lino's avatar
João Lino committed
1573
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1574
      xStageFirstRun( spargeTime, spargeTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Sparge );
João Lino's avatar
João Lino committed
1575 1576 1577 1578
      
      break;
    }
    case eCookingStage_Boil: {
João Lino's avatar
Rel.3  
João Lino committed
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
      switch(beerProfile) {
        case eBeerProfile_Trigo: {
          String say = "Get ";

          float hopAmount = 0.8 * ((float) finalYield);
          say += String(hopAmount);

          say += String("g of Magnum 9.4\% and Styrian Golding 5\% ready.");

          xWaitForAction("Hops", say);

          break;
        }
João Lino's avatar
João Lino committed
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
        case eBeerProfile_IPA: {
          String say = "Get ";

          float hopAmount = 0.8 * ((float) finalYield);
          say += String(hopAmount);

          say += String("g of Chinook, Cascade and Styrian Golding ready.");

          xWaitForAction("Hops", say);

          break;
        }
João Lino's avatar
Rel.3  
João Lino committed
1604 1605 1606 1607 1608 1609 1610 1611
        default: {
          xWaitForAction("Hops", "Add the hops in the right order, at the right time.");

        }
      }

      repaint = true;

João Lino's avatar
João Lino committed
1612
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1613
      xStageFirstRun( boilTime, boilTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Boil );
João Lino's avatar
João Lino committed
1614 1615 1616 1617
      
      break;
    }
    case eCookingStage_Cooling: {
João Lino's avatar
Rel.3  
João Lino committed
1618 1619 1620 1621 1622 1623
      // Make sure there is water
      xWaitForAction("Coil", "Add the coil and connect it to the main water supply.");

      repaint = true;

      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1624
      xStageFirstRun( coolingTime, coolingTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Cooling );
João Lino's avatar
Rel.3  
João Lino committed
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636

      break;
    }
    case eCookingStage_Clean: {
      // Make sure there is water
      xWaitForAction("Water", "Add 13 liters.");

      // Make sure there is water
      xWaitForAction("Star San HB", "Add 0.89oz/26ml.");

      repaint = true;

João Lino's avatar
João Lino committed
1637
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1638
      xStageFirstRun( cleaningTime, cleaningTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Clean );
João Lino's avatar
João Lino committed
1639 1640 1641 1642 1643

      break;
    }
    case eCookingStage_Purge: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1644
      xStageFirstRun( 0, 0, PUMP_SPEED_MAX_MOSFET, eCookingStage_Purge );
João Lino's avatar
João Lino committed
1645 1646 1647 1648 1649 1650 1651

      xRegulatePumpSpeed();

      break;
    }
    case eCookingStage_Done: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1652
      xStageFirstRun( 0, 0, PUMP_SPEED_STOP_MOSFET, eCookingStage_Done );
João Lino's avatar
João Lino committed
1653 1654 1655 1656

      break;
    }
  }
1657 1658 1659
}

void xTransitionIntoStage(eCookingStages nextStage) {
João Lino's avatar
João Lino committed
1660 1661 1662 1663 1664 1665 1666
  // Turn off all hardware that can damage itself if the machine is not cooking
  xSafeHardwarePowerOff();    
  
  // Warn the user a stage has ended
  xWarnClockEnded();
  
  // Reset global stage variables
João Lino's avatar
Rel.3  
João Lino committed
1667
  xSetupGlobalVariablesForStage( nextStage );
1668 1669
}

João Lino's avatar
Rel.3  
João Lino committed
1670
void xBasicStageOperation( int iStageTime, int iStageTemperature, int iStageTemperatureRange, eCookingStages nextStage, boolean bAverageUpDown ) {
João Lino's avatar
João Lino committed
1671 1672 1673
  if(bStageFirstRun) {
    // Don't run this again
    bStageFirstRun = false;
João Lino's avatar
João Lino committed
1674 1675

    //xStageFirstRun( iStageTime, iStageTemperature, 255 );
João Lino's avatar
João Lino committed
1676 1677 1678 1679
    
    // When the stage should be skipped
    if( iStageTime == 0) {
      // Continue to the next stage
João Lino's avatar
Rel.3  
João Lino committed
1680
      //xSetupGlobalVariablesForStage( nextStage );
João Lino's avatar
João Lino committed
1681 1682 1683 1684 1685
      
      // There is nothing to do, in this stage
      return;
    } else {
      // Set the clock, target temperature and Reset the clock
1686
      //xStageFirstRun( iStageTime, iStageTemperature, PUMP_SPEED_SLOW );
João Lino's avatar
João Lino committed
1687
      //xStageFirstRun( iStageTime, iStageTemperature, 255 );
João Lino's avatar
João Lino committed
1688 1689 1690
    }
  } else {
    // Account for time spent at the target temperature | Input 1: range in ºC within which the target temperature is considered to be reached
João Lino's avatar
Rel.3  
João Lino committed
1691 1692 1693 1694 1695
    #ifdef DEBUG_OFF
    xCountTheTime( iStageTemperatureRange, false );
    #else
    xCountTheTime( iStageTemperatureRange, bAverageUpDown );
    #endif
João Lino's avatar
João Lino committed
1696 1697 1698
    
    if( isTimeLeft() ) {
      // Do temperature control
João Lino's avatar
Rel.3  
João Lino committed
1699
      xRegulateTemperature( bAverageUpDown );
1700 1701 1702

      // Do flow control
      xRegulatePumpSpeed();
João Lino's avatar
João Lino committed
1703 1704
      
    } else {
João Lino's avatar
Rel.3  
João Lino committed
1705 1706 1707 1708
      #ifdef DEBUG_OFF
      debugPrintFunction("xBasicStageOperation");
      debugPrintVar("clockCounter", clockCounter);
      #endif
João Lino's avatar
João Lino committed
1709 1710 1711 1712 1713 1714 1715 1716 1717
      // Continue to the next stage
      xTransitionIntoStage( nextStage );
      
      // There is nothing to do, in this stage
      return;
    }
  }
  // There is nothing to do, in this iteration
  return;
1718 1719
}

João Lino's avatar
Rel.3  
João Lino committed
1720
void xManageMachineSystems() {
1721

João Lino's avatar
Rel.3  
João Lino committed
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
  #ifdef DEBUG
  Serial.print(millis());
  Serial.print(",");
  if(cooking) {
    Serial.print("1");
  }
  else {
    Serial.print("0");
  }
  Serial.print(",");
  Serial.print(cookTemperature);
  Serial.print(",");
  if(bStatusElement) {
    Serial.print("1");
  }
  else {
    Serial.print("0");
  }
  Serial.print(",");
  #endif
1742 1743

  // Measure temperature, for effect
João Lino's avatar
João Lino committed
1744 1745 1746
  basePT100.measure(false);
  upPT100.measure(false);
  downPT100.measure(true);
1747 1748 1749

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

1752 1753
    return;
  }
João Lino's avatar
João Lino committed
1754 1755 1756 1757 1758
  
  // Operate the machine according to the current mode
  switch(cookingStage) {
    case eCookingStage_Startpoint: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1759
      xBasicStageOperation( startpointTime, startpointTemperature, 0, eCookingStage_BetaGlucanase, false);
João Lino's avatar
João Lino committed
1760 1761 1762 1763 1764
      
      break;
    }
    case eCookingStage_BetaGlucanase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1765
      xBasicStageOperation( betaGlucanaseTime, betaGlucanaseTemperature, 3, eCookingStage_Debranching, true );
João Lino's avatar
João Lino committed
1766 1767 1768 1769 1770
      
      break;
    }
    case eCookingStage_Debranching: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1771
      xBasicStageOperation( debranchingTime, debranchingTemperature, 3, eCookingStage_Proteolytic, true );
João Lino's avatar
João Lino committed
1772 1773 1774 1775 1776
      
      break;
    }
    case eCookingStage_Proteolytic: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1777
      xBasicStageOperation( proteolyticTime, proteolyticTemperature, 3, eCookingStage_BetaAmylase, true );
João Lino's avatar
João Lino committed
1778 1779 1780 1781 1782
      
      break;
    }
    case eCookingStage_BetaAmylase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1783
      xBasicStageOperation( betaAmylaseTime, betaAmylaseTemperature, 4, eCookingStage_AlphaAmylase, true );
João Lino's avatar
João Lino committed
1784 1785 1786 1787 1788
      
      break;
    }
    case eCookingStage_AlphaAmylase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1789
      xBasicStageOperation( alphaAmylaseTime, alphaAmylaseTemperature, 2, eCookingStage_Mashout, true );
João Lino's avatar
João Lino committed
1790 1791 1792 1793 1794
      
      break;
    }
    case eCookingStage_Mashout: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1795
      xBasicStageOperation( mashoutTime, mashoutTemperature, 1, eCookingStage_Recirculation, true );
João Lino's avatar
João Lino committed
1796 1797 1798 1799 1800
      
      break;
    }
    case eCookingStage_Recirculation: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1801
      xBasicStageOperation( recirculationTime, recirculationTemperature, 1, eCookingStage_Sparge, true );
João Lino's avatar
João Lino committed
1802 1803 1804 1805 1806
      
      break;
    }
    case eCookingStage_Sparge: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1807
      xBasicStageOperation( spargeTime, spargeTemperature, 3, eCookingStage_Boil, false );
João Lino's avatar
João Lino committed
1808 1809 1810 1811 1812
      
      break;
    }
    case eCookingStage_Boil: {
      // A basic operation for a basic stage
João Lino's avatar
Rel.3  
João Lino committed
1813
      xBasicStageOperation( boilTime, boilTemperature, 2, eCookingStage_Cooling, false );
João Lino's avatar
João Lino committed
1814 1815 1816 1817 1818
      
      break;
    }
    case eCookingStage_Cooling: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1819
      xBasicStageOperation( coolingTime, coolingTemperature, 0, eCookingStage_Done, false );
João Lino's avatar
Rel.3  
João Lino committed
1820 1821 1822 1823 1824

      break;
    }
    case eCookingStage_Clean: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1825
      xBasicStageOperation( cleaningTime, cleaningTemperature, 0, eCookingStage_Done, false );
1826 1827 1828 1829 1830 1831

      break;
    }
    case eCookingStage_Purge: {
      // A basic operation for a basic stage
      //xBasicStageOperation( coolingTime, coolingTemperature, 1, eCookingStage_Done );
João Lino's avatar
João Lino committed
1832
      iPumpSpeed = PUMP_SPEED_MAX_MOSFET;
1833 1834 1835

      xRegulatePumpSpeed();

João Lino's avatar
João Lino committed
1836 1837
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
1838 1839 1840 1841 1842 1843
    case eCookingStage_Done: {
      // Update cooking state
      stopBrewing();

      // Ask for screen refresh
      repaint = true;
João Lino's avatar
João Lino committed
1844 1845 1846
      
      // Warn the user that the cooking is done
      xWarnCookEnded();
João Lino's avatar
Rel.3  
João Lino committed
1847 1848

      break;
João Lino's avatar
João Lino committed
1849 1850
    }
  }
1851 1852 1853 1854 1855
}

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

void startBrewing() {
1856 1857
  //sing(MELODY_SUPER_MARIO, PIEZO_PIN);

1858 1859 1860 1861 1862 1863 1864
  cooking = true;
}

void stopBrewing() {
  cooking = false;
}

João Lino's avatar
João Lino committed
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
void resetMenu( boolean requestRepaintPaint ) {
  eMenuType = eMenuType_Main;

  if( requestRepaintPaint ) {
    repaint = true;
  }
  
  // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
  xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenuPosition, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );
}

1876 1877
void backToStatus() {
  lastInterruptTime = millis() - SETTING_MAX_INACTIVITY_TIME - 1;
João Lino's avatar
João Lino committed
1878
  resetMenu(true);
1879 1880 1881 1882
}
// #################################################### Helpers ##################################################################

// #################################################### Set Variables ##################################################################
João Lino's avatar
João Lino committed
1883
int getTimer( int initialValue, int defaultValue ) {
João Lino's avatar
João Lino committed
1884
  // 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
1885
  xSetupRotaryEncoder( eRotaryEncoderMode_Time, initialValue, 7200, 0, 1, 30 );
1886 1887

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1888
  int rotaryEncoderPreviousPosition = 0;
1889 1890 1891 1892 1893 1894
  int minutes = 0;
  int seconds = 0;
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
João Lino's avatar
João Lino committed
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
  lcd.print("Set Time (");
  minutes = defaultValue/60;
  lcd.print(minutes);
  seconds = defaultValue-minutes*60;
  lcd.print(":");
  if(seconds<10) {
    lcd.print("0");
  }
  lcd.print(seconds);
  lcd.print(")");
1905
  lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1906 1907 1908
  lcd.print("      0:00");
  
  while(true) {
1909
    // Check if pushbutton is pressed
João Lino's avatar
João Lino committed
1910
    if ((digitalRead(ROTARY_ENCODER_SW_PIN))) {  
1911
      // Wait until switch is released
João Lino's avatar
João Lino committed
1912
      while (digitalRead(ROTARY_ENCODER_SW_PIN)) {}  
1913
      
João Lino's avatar
João Lino committed
1914 1915
      // debounce
      delay(10);
1916

João Lino's avatar
João Lino committed
1917
      // Job is done, break the circle
1918
      break;
1919
    } else {
João Lino's avatar
João Lino committed
1920
      // Don't forget to keep an eye on the cooking
João Lino's avatar
Rel.3  
João Lino committed
1921
      xManageMachineSystems();
João Lino's avatar
João Lino committed
1922
    }
1923 1924
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
1925 1926 1927 1928
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      minutes = rotaryEncoderVirtualPosition/60;
      seconds = rotaryEncoderVirtualPosition-minutes*60;
1929
      
1930
      lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
João Lino's avatar
João Lino committed
1931 1932 1933 1934 1935 1936 1937 1938
      
      if(minutes<100) {
        lcd.print(" ");
      }
      if(minutes<10) {
        lcd.print(" ");
      }
      lcd.print("    ");
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
      lcd.print(minutes);
      lcd.print(":");
      if(seconds<10) {
        lcd.print("0");
      }
      lcd.print(seconds);
      lcd.println("                ");
    }
  }
  
service-config's avatar
Mixer  
service-config committed
1949
  return rotaryEncoderVirtualPosition;
1950 1951
}

João Lino's avatar
João Lino committed
1952 1953 1954 1955 1956
int getTimer( int initialValue ) {
  return getTimer( initialValue, initialValue );
}

int getTemperature(int initialValue) {
1957 1958
  
  // set operation state
1959
  rotaryEncoderMode = eRotaryEncoderMode_Generic;
João Lino's avatar
João Lino committed
1960
  rotaryEncoderVirtualPosition = initialValue;  
1961 1962

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1963
  int rotaryEncoderPreviousPosition = 0;
1964 1965 1966 1967 1968
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Temperature");
1969
  lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1970 1971
  lcd.print("       0 *C");
  
1972
  rotaryEncoderMaxPosition = TEMPERATURE_SETTING_MAX_VALUE;
service-config's avatar
Mixer  
service-config committed
1973
  
1974
  while(true) {
1975
    // Check if pushbutton is pressed
João Lino's avatar
João Lino committed
1976
    if ((digitalRead(ROTARY_ENCODER_SW_PIN))) {  
1977
      // Wait until switch is released
João Lino's avatar
João Lino committed
1978
      while (digitalRead(ROTARY_ENCODER_SW_PIN)) {}  
1979
      
João Lino's avatar
João Lino committed
1980 1981
      // debounce
      delay(10);
1982

João Lino's avatar
João Lino committed
1983
      // Job is done, break the circle
1984
      break;
1985
    } else {
João Lino's avatar
João Lino committed
1986
      // Don't forget to keep an eye on the cooking
João Lino's avatar
Rel.3  
João Lino committed
1987
      xManageMachineSystems();
João Lino's avatar
João Lino committed
1988
    }
1989 1990
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
1991 1992
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
1993
      
1994
      lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1995
      lcd.print("     ");
service-config's avatar
Mixer  
service-config committed
1996
      if(rotaryEncoderVirtualPosition<10) {
1997 1998 1999
        lcd.print("  ");
      }
      else {
service-config's avatar
Mixer  
service-config committed
2000
        if(rotaryEncoderVirtualPosition<100) {
2001 2002 2003
          lcd.print(" ");
        }
      }
service-config's avatar
Mixer  
service-config committed
2004
      lcd.print(rotaryEncoderVirtualPosition);
2005 2006 2007 2008 2009
      lcd.print(" *C");
      lcd.println("                ");
    }
  }
  
service-config's avatar
Mixer  
service-config committed
2010
  return rotaryEncoderVirtualPosition;
2011 2012
}

João Lino's avatar
João Lino committed
2013
int xSetGenericValue(int initialValue, int minimumValue, int maximumValue, char *valueName, char *unit) {  
João Lino's avatar
João Lino committed
2014
  // 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
2015
  xSetupRotaryEncoder( eRotaryEncoderMode_Generic, initialValue, maximumValue, minimumValue, 1, 5 );
2016 2017

  // initialize variables
service-config's avatar
Mixer  
service-config committed
2018
  int rotaryEncoderPreviousPosition = 0;
2019 2020 2021
  
  // Setup Screen
  lcd.clear();
2022 2023 2024 2025 2026
  lcd.home();
  lcd.print( "Set " );
  lcd.print( valueName );
  lcd.setCursor ( 0 , LCD_VERTICAL_RESOLUTION - 1 );
  lcd.print( "       0 " );
João Lino's avatar
João Lino committed
2027
  lcd.print( unit );
2028
  
2029
  while(true) {
2030 2031 2032
    // Check if pushbutton is pressed
    if ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {
      // Wait until switch is released
João Lino's avatar
João Lino committed
2033
      while ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {}  
2034
      
João Lino's avatar
João Lino committed
2035 2036
      // debounce
      delay( 10 );
service-config's avatar
Mixer  
service-config committed
2037

João Lino's avatar
João Lino committed
2038
      // Job is done, break the circle
service-config's avatar
Mixer  
service-config committed
2039
      break;
2040
    } else {
João Lino's avatar
João Lino committed
2041
      // Don't forget to keep an eye on the cooking
João Lino's avatar
Rel.3  
João Lino committed
2042
      xManageMachineSystems();
João Lino's avatar
João Lino committed
2043
    }
service-config's avatar
Mixer  
service-config committed
2044
    
2045 2046
    // Check if there was an update by the rotary encoder
    if( rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition ) {
service-config's avatar
Mixer  
service-config committed
2047 2048
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      
2049 2050 2051 2052
      lcd.setCursor( 0, LCD_VERTICAL_RESOLUTION - 1 );
      lcd.print( "     " );
      if( rotaryEncoderVirtualPosition < 10 ) {
        lcd.print( "  " );
2053 2054
      }
      else {
2055 2056
        if( rotaryEncoderVirtualPosition < 100 ) {
          lcd.print( " " );
2057 2058
        }
      }
2059
      lcd.print( rotaryEncoderVirtualPosition );
2060 2061
      lcd.print( " " );
      lcd.print( unit );
2062
      lcd.println( "                " );
2063 2064
    }
  }
2065 2066
  
  return rotaryEncoderVirtualPosition;
2067 2068
}

João Lino's avatar
João Lino committed
2069 2070 2071 2072 2073 2074 2075 2076
int xSetTemperature( int initialValue ) {  
  return xSetGenericValue( initialValue, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
}

int xSetFinalYield( int initialValue ) {  
  return xSetGenericValue( initialValue, SETTING_MACHINE_YIELD_CAPACITY_MIN, SETTING_MACHINE_YIELD_CAPACITY_MAX, "Final Yield", "l" );
}

2077
// ###################### Set Variables ##################################################
2078

João Lino's avatar
Rel.3  
João Lino committed
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
void xWaitForAction(String title, String message) {
  while(true) {
    // Check if pushbutton is pressed
    if ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {
      // Wait until switch is released
      while ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {}  
      
      // debounce
      delay( 10 );

      // Job is done, break the circle
      break;
    } else {
      sing(BUZZ_1, PIEZO_PIN);

      // Print the message
      if(! lcdPrint(&lcd, title, message)) {
        break;
      }
2098 2099 2100 2101
    }
  }
}

João Lino's avatar
Rel.3  
João Lino committed
2102 2103 2104 2105 2106 2107 2108 2109
boolean gotButtonPress(int iPin) {
  boolean ret = false;

  if ((digitalRead(iPin))) {    // check if pushbutton is pressed
    ret = true;
    while (digitalRead(iPin)) {}    // wait til switch is released
    delay(10);                            // debounce
  } 
2110

João Lino's avatar
Rel.3  
João Lino committed
2111 2112
  return ret;
}