brew.ino 67 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

eCookingStages          cookingStage;
eBeerProfile            beerProfile;

eMenuType               eMenuType;

João Lino's avatar
João Lino committed
42 43 44 45 46 47 48
MenuData                mdMainMenu;
MenuData                mdStartFromStageMenu;
MenuData                mdBeerProfileMenu;
MenuData                mdStageMenu;
MenuData                mdMaltMenu;
MenuData                mdSettingsMenu;

João Lino's avatar
Rel.3  
João Lino committed
49 50
eMainMenuOptions        eMainMenuPosition;
eMainMenuOptions        eMainMenuSelection;
João Lino's avatar
João Lino committed
51 52
eStageMenuOptions       eStartFromStageMenuPosition;
eStageMenuOptions       eStartFromStageMenuSelection;
João Lino's avatar
Rel.3  
João Lino committed
53 54 55 56 57 58 59 60 61
eBeerProfileMenuOptions eBeerProfileMenuPosition;
eBeerProfileMenuOptions eBeerProfileMenuSelection;
eStageMenuOptions       eStageMenuPosition;
eStageMenuOptions       eStageMenuSelection;
eMaltMenuOptions        eMaltMenuPosition;
eMaltMenuOptions        eMaltMenuSelection; 
eSettingsMenuOptions    eSettingsMenuPosition;
eSettingsMenuOptions    eSettingsMenuSelection;

62
// ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
63 64
boolean                 cooking;

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

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

boolean                 refresh;
boolean                 repaint;
106

João Lino's avatar
Rel.3  
João Lino committed
107 108
boolean                 bStatusElement;

109
// ++++++++++++++++++++++++ Interrupts ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
110
static unsigned long    lastInterruptTime;
111

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

João Lino's avatar
João Lino committed
119 120
volatile boolean        onISR = false;

João Lino's avatar
João Lino committed
121 122
unsigned long           rotarySwDetectTime;

123
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
124 125 126
int                     iWindowSize;             // Time frame to operate in
unsigned long           windowStartTime;
double                  dWattPerPulse;
127

128 129 130
// ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
int                     iPumpSpeed;             // Time frame to operate in

131
// ######################### INITIALIZE #########################
132
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
133
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);
134

João Lino's avatar
João Lino committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
// +++++++++++++++++++++++ Temperature +++++++++++++++++++++++
Temperature                   basePT100("base", 
                          PT100_BASE_OUTPUT_PIN, 
                          PT100_BASE_INPUT_PIN, 
                          PT100_BASE_TIME_BETWEEN_READINGS, 
                          2.0298, 2.0259, 665.24, 662.17);
Temperature                   upPT100("up", 
                          PT100_UP_OUTPUT_PIN, 
                          PT100_UP_INPUT_PIN,  
                          PT100_UP_TIME_BETWEEN_READINGS, 
                          2.0274, 2.0245, 659.43, 656.72);
Temperature                   downPT100("down", 
                          PT100_DOWN_OUTPUT_PIN,  
                          PT100_DOWN_INPUT_PIN, 
                          PT100_DOWN_TIME_BETWEEN_READINGS, 
                          2.0309, 2.0288, 658.15, 655.35);
151 152

// ######################### INTERRUPTS #########################
João Lino's avatar
João Lino committed
153
void isr ()  {    // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
154
  unsigned long interruptTime = millis();
João Lino's avatar
João Lino committed
155
  unsigned long diff = interruptTime - lastInterruptTime;
156
  
157
  // 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
158
  if (diff > ROTARY_ENCODER_DEBOUNCE_TIME) {
João Lino's avatar
João Lino committed
159
    lastInterruptTime = interruptTime;
160
  
João Lino's avatar
João Lino committed
161
    switch(rotaryEncoderMode) {
162 163 164 165
      // Input of rotary encoder controling menus
      case eRotaryEncoderMode_Menu: {
        if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
166 167
        }
        else {
168
            rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - rotaryEncoderSingleStep;
169
        }
170 171
        if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
172
        }
173 174
        if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
175 176 177 178
        }
        
        break;
      }
service-config's avatar
Mixer  
service-config committed
179
      
180 181
      // Input of rotary encoder controling time variables
      case eRotaryEncoderMode_Time: {
João Lino's avatar
João Lino committed
182
        if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
service-config's avatar
Mixer  
service-config committed
183
          if(rotaryEncoderVirtualPosition >= 60) {
184
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderMultiStep);
185 186
          }
          else {
187
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
188 189 190
          }
        }
        else {
191
          if(rotaryEncoderVirtualPosition == rotaryEncoderMinPosition) {
service-config's avatar
Mixer  
service-config committed
192
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 60);
193 194
          }
          else {
195 196
            if(rotaryEncoderVirtualPosition >= (60 + rotaryEncoderMultiStep)) {
              rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - rotaryEncoderMultiStep);
197 198
            }
            else {
199
              rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - rotaryEncoderSingleStep;
200 201 202
            }
          }
        }
203 204
        if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
205
        }
206 207
        if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
208 209 210 211
        }
        
        break;
      }
service-config's avatar
Mixer  
service-config committed
212
      
213 214 215
      // Input of rotary encoder controling generic integer variables within a range between rotaryEncoderMinPosition and rotaryEncoderMaxPosition
      case eRotaryEncoderMode_Generic: {
        if (!digitalRead(ROTARY_ENCODER_DT_PIN)) {
216
          rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + rotaryEncoderSingleStep);
217 218
        }
        else {
219
          rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - rotaryEncoderSingleStep);
220
        }
service-config's avatar
Mixer  
service-config committed
221 222
        if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
223
        }
224 225
        if (rotaryEncoderVirtualPosition < rotaryEncoderMinPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMinPosition;
service-config's avatar
Mixer  
service-config committed
226 227 228 229
        }
       
        break;
      }
230 231 232 233
      default: {
        
      }
    }
João Lino's avatar
João Lino committed
234 235 236
    
    repaint = true;
    refresh = true;
237 238 239
  }
}

240 241 242 243 244 245 246 247 248
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;
}

249
// ######################### START #########################
250
void xSafeHardwarePowerOff() {
251
  // Turn off gracefully
João Lino's avatar
João Lino committed
252
  iPumpSpeed = PUMP_SPEED_STOP_MOSFET;
253 254 255
  xRegulatePumpSpeed();

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

João Lino's avatar
João Lino committed
260 261 262
  basePT100.setPumpStatus( false );
  upPT100.setPumpStatus( false );
  downPT100.setPumpStatus( false );
263

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

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

271 272 273
  // Play Melody;
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);

274
  //termometerCalibration();
João Lino's avatar
João Lino committed
275
  delay(SETTING_WELCOME_TIMEOUT);      // pause for effect
João Lino's avatar
Rel.3  
João Lino committed
276
  //#endif
277 278
}

279
void setup() {
João Lino's avatar
João Lino committed
280 281 282 283 284 285 286 287 288
  // ++++++++++++++++++++++++ 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
289
  bStatusElement              =   false;
João Lino's avatar
João Lino committed
290 291 292 293 294
  windowStartTime             =   millis();
  dWattPerPulse               =   HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ;

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

295 296
  // ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
  pinMode(PUMP_PIN, OUTPUT);   // sets the pin as output
João Lino's avatar
João Lino committed
297
  iPumpSpeed                  =   PUMP_SPEED_STOP_MOSFET;             // Time frame to operate in
298 299
  analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255

João Lino's avatar
João Lino committed
300
  // ++++++++++++++++++++++++ Piezo ++++++++++++++++++++++++
301 302
  pinMode(PIEZO_PIN, OUTPUT);

João Lino's avatar
João Lino committed
303
  // ++++++++++++++++++++++++ Temperature Sensor PT100 ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
304

João Lino's avatar
João Lino committed
305 306 307 308 309 310 311 312 313 314 315 316 317
  // ++++++++++++++++++++++++ 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 );
João Lino's avatar
João Lino committed
318
  rotarySwDetectTime = 0;
João Lino's avatar
João Lino committed
319 320

  // ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
João Lino's avatar
Rel.3  
João Lino committed
321
  eMenuType                   =   eMenuType_Main;
João Lino's avatar
João Lino committed
322 323 324

  mdMainMenu._position        =   eMainMenu_GO;
  mdMainMenu._selection       =   eMainMenu_NULL;
João Lino's avatar
Rel.3  
João Lino committed
325 326 327 328 329 330 331 332 333 334 335 336
  
  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
337
  cookingStage                =   eCookingStage_Startpoint;
João Lino's avatar
Rel.3  
João Lino committed
338
  beerProfile                 =   eBeerProfile_Basic;
João Lino's avatar
João Lino committed
339
  // ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
João Lino's avatar
Rel.3  
João Lino committed
340

João Lino's avatar
João Lino committed
341
  cooking                     =   false;
342

João Lino's avatar
João Lino committed
343
  clockStartTime              =   0;
João Lino's avatar
Rel.3  
João Lino committed
344
  clockLastUpdate             =   0;
João Lino's avatar
João Lino committed
345 346 347 348 349 350 351 352 353
  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
354
  finalYield                  =   25;
João Lino's avatar
João Lino committed
355

João Lino's avatar
João Lino committed
356 357 358 359 360 361 362 363 364 365 366
  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
367
  cleaningTime                =   SETTING_CLEANING_TIME;
João Lino's avatar
João Lino committed
368

João Lino's avatar
João Lino committed
369 370 371 372 373 374 375 376 377 378 379
  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
380
  cleaningTemperature         =   SETTING_CLEANING_TEMPERATURE;
João Lino's avatar
João Lino committed
381 382 383 384 385 386 387 388 389

  refresh                     =   true;
  repaint                     =   true;

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

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

João Lino's avatar
Rel.3  
João Lino committed
391
  // ######################### Code - Run Once #########################
João Lino's avatar
João Lino committed
392
  xSafeHardwarePowerOff           ();
João Lino's avatar
Rel.3  
João Lino committed
393
  xWelcomeUser                    ();
João Lino's avatar
João Lino committed
394
  
João Lino's avatar
Rel.3  
João Lino committed
395
  xSetupRotaryEncoder             ( eRotaryEncoderMode_Menu, eMainMenu_GO, MENU_SIZE_MAIN_MENU - 1, 1, 1, 0 );
396
}
397

398
// ######################### MAIN LOOP #########################
399 400

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

João Lino's avatar
João Lino committed
403
  if( inactivityTime > SETTING_MAX_INACTIVITY_TIME ) {    // Inactivity check
404 405 406 407
    if(refresh) {
      repaint = true;
      refresh = false;
    }
João Lino's avatar
Rel.3  
João Lino committed
408
    repaint = displayStatus( &lcd, cooking, cookTemperature, basePT100.getCurrentTemperature(), upPT100.getCurrentTemperature(), downPT100.getCurrentTemperature(), clockCounter, repaint );
409 410
  }
  else {
João Lino's avatar
Rel.3  
João Lino committed
411
    runMenu();
412 413
  }
  
João Lino's avatar
Rel.3  
João Lino committed
414
  xManageMachineSystems();
415 416
}

417 418
// ######################### FUNCTIONS ########################

João Lino's avatar
Rel.3  
João Lino committed
419 420 421 422 423 424 425 426
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
427
  }
João Lino's avatar
Rel.3  
João Lino committed
428
  #endif
429

João Lino's avatar
Rel.3  
João Lino committed
430 431 432
  switch(eMenuType) {
    case eMenuType_Main: {
      eMainMenuPosition = static_cast<eMainMenuOptions>(rotaryEncoderVirtualPosition);
433

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

João Lino's avatar
Rel.3  
João Lino committed
436 437 438
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eMainMenuSelection = eMainMenuPosition;
      }
439

João Lino's avatar
Rel.3  
João Lino committed
440
      runMainMenuSelection();
João Lino's avatar
João Lino committed
441

João Lino's avatar
Rel.3  
João Lino committed
442 443
      break;
    }
João Lino's avatar
João Lino committed
444 445 446 447 448 449 450 451 452 453 454 455 456
    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
457 458 459 460
    case eMenuType_BeerProfile: {
      eBeerProfileMenuPosition = static_cast<eBeerProfileMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displayBeerProfileMenu( &lcd, eBeerProfileMenuPosition, repaint );
João Lino's avatar
João Lino committed
461

João Lino's avatar
Rel.3  
João Lino committed
462 463 464
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eBeerProfileMenuSelection = eBeerProfileMenuPosition;
      }
João Lino's avatar
João Lino committed
465

João Lino's avatar
Rel.3  
João Lino committed
466 467 468
      runBeerProfileSelection();
      
      break;
João Lino's avatar
João Lino committed
469
    }
João Lino's avatar
Rel.3  
João Lino committed
470 471 472 473
    case eMenuType_Stage: {
      eStageMenuPosition = static_cast<eStageMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displayStageMenu( &lcd, eStageMenuPosition, repaint );
João Lino's avatar
João Lino committed
474

João Lino's avatar
Rel.3  
João Lino committed
475 476 477
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eStageMenuSelection = eStageMenuPosition;
      }
478

João Lino's avatar
Rel.3  
João Lino committed
479 480 481
      runStageSelection();
      
      break;
482
    }
João Lino's avatar
Rel.3  
João Lino committed
483 484 485 486
    case eMenuType_Malt: {
      eMaltMenuPosition = static_cast<eMaltMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displayMaltMenu( &lcd, eMaltMenuPosition, repaint );
487

João Lino's avatar
Rel.3  
João Lino committed
488 489 490
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eMaltMenuSelection = eMaltMenuPosition;
      }
491

João Lino's avatar
Rel.3  
João Lino committed
492 493 494
      runMaltSelection();
      
      break;
495
    }
João Lino's avatar
Rel.3  
João Lino committed
496 497 498 499
    case eMenuType_Settings: {
      eSettingsMenuPosition = static_cast<eSettingsMenuOptions>(rotaryEncoderVirtualPosition);
      
      repaint = displaySettingsMenu( &lcd, eSettingsMenuPosition, repaint );
500

João Lino's avatar
Rel.3  
João Lino committed
501 502 503
      if ( gotButtonPress( ROTARY_ENCODER_SW_PIN ) ) {
        eSettingsMenuSelection = eSettingsMenuPosition;
      }
504

João Lino's avatar
Rel.3  
João Lino committed
505 506 507
      runSettingsSelection();
      
      break;
508 509
    }
  }
João Lino's avatar
Rel.3  
João Lino committed
510 511 512 513

  #ifdef DEBUG_OFF
  if(debug_go) {
    debugPrintVar("repaint", repaint);
514
  }
João Lino's avatar
Rel.3  
João Lino committed
515
  #endif
516 517
}

João Lino's avatar
Rel.3  
João Lino committed
518 519
void runSettingsSelection() {
  switch(eSettingsMenuSelection) {
João Lino's avatar
João Lino committed
520
    case eSettingsMenu_Pump: {
João Lino's avatar
Rel.3  
João Lino committed
521
      // Stuff
João Lino's avatar
João Lino committed
522 523 524 525 526 527
      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
528 529

      backToStatus();
530
      
João Lino's avatar
João Lino committed
531 532 533 534 535 536
      break;
    }
    case eSettingsMenu_PT100_Element: {
      // Stuff

      backToStatus();
537 538 539
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
540 541 542 543
    case eSettingsMenu_PT100_Up: {
      // Stuff

      backToStatus();
544 545 546
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
547 548 549 550
    case eSettingsMenu_PT100_Down: {
      // Stuff

      backToStatus();
551
      
552 553
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    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();
576
      
577 578
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
579 580 581 582
    case eMaltMenu_CastleMalting_Wheat_Blanc: {
      // Stuff

      backToStatus();
583
      
584 585
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
586 587 588
    case eMaltMenu_Back: {
      eMenuType = eMenuType_Main;
      repaint = true;
589
      
João Lino's avatar
Rel.3  
João Lino committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
      // 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
607
      startpointTemperature = xSetGenericValue( startpointTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
608 609

      backToStatus();
service-config's avatar
Mixer  
service-config committed
610 611
      
      break;
612
    }
João Lino's avatar
Rel.3  
João Lino committed
613 614 615
    case eStageMenu_BetaGlucanase: {
      betaGlucanaseTime = getTimer( betaGlucanaseTime );
  
João Lino's avatar
João Lino committed
616
      betaGlucanaseTemperature = xSetGenericValue( betaGlucanaseTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
617 618

      backToStatus();
619
      
João Lino's avatar
Rel.3  
João Lino committed
620 621 622 623 624
      break;
    }
    case eStageMenu_Debranching: {
      debranchingTime = getTimer( debranchingTime );
  
João Lino's avatar
João Lino committed
625
      debranchingTemperature = xSetGenericValue( debranchingTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
626 627

      backToStatus();
628 629 630
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
631 632
    case eStageMenu_Proteolytic: {
      proteolyticTime = getTimer( proteolyticTime );
633
      
João Lino's avatar
João Lino committed
634
      proteolyticTemperature = xSetGenericValue( proteolyticTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
635 636

      backToStatus();
637 638
      
      break;
639
    }
João Lino's avatar
Rel.3  
João Lino committed
640 641 642
    case eStageMenu_BetaAmylase: {
      betaAmylaseTime = getTimer( betaAmylaseTime );
  
João Lino's avatar
João Lino committed
643
      betaAmylaseTemperature = xSetGenericValue( betaAmylaseTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
644 645

      backToStatus();
646
      
647 648
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
649 650 651
    case eStageMenu_AlphaAmylase: {
      alphaAmylaseTime = getTimer( alphaAmylaseTime );
  
João Lino's avatar
João Lino committed
652
      alphaAmylaseTemperature = xSetGenericValue( alphaAmylaseTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
653 654 655 656 657 658 659 660

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

      backToStatus();
664
      
665 666
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
667 668 669
    case eStageMenu_Recirculation: {
      recirculationTime = getTimer( recirculationTime );
  
João Lino's avatar
João Lino committed
670
      recirculationTemperature = xSetGenericValue( recirculationTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
671 672

      backToStatus();
673
      
João Lino's avatar
Rel.3  
João Lino committed
674 675 676 677 678
      break;
    }
    case eStageMenu_Sparge: {
      spargeTime = getTimer( spargeTime );
  
João Lino's avatar
João Lino committed
679
      spargeTemperature = xSetGenericValue( spargeTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
680 681

      backToStatus();
682 683 684
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
685 686
    case eStageMenu_Boil: {
      boilTime = getTimer( boilTime );
687
      
João Lino's avatar
João Lino committed
688
      boilTemperature = xSetGenericValue( boilTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
689 690

      backToStatus();
691
      
692 693
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
694 695
    case eStageMenu_Cooling: {
      coolingTime = getTimer( coolingTime );
696
      
João Lino's avatar
João Lino committed
697
      coolingTemperature = xSetGenericValue( coolingTemperature, TEMPERATURE_MIN_VALUE, TEMPERATURE_MAX_VALUE, "temperature", "*C" );
João Lino's avatar
Rel.3  
João Lino committed
698 699

      backToStatus();
700 701 702
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
703 704 705
    case eStageMenu_Back: {
      eMenuType = eMenuType_Main;
      repaint = true;
706
      
João Lino's avatar
Rel.3  
João Lino committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
      // 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
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
      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
747 748 749 750 751 752 753 754

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

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_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
778 779

      backToStatus();
780 781 782
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
783 784 785
    case eBeerProfileMenu_IPA: {
      beerProfile                 =   eBeerProfile_IPA;

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 809
      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
810 811
      backToStatus();

812 813
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
814 815 816
    case eBeerProfileMenu_Belga: {
      beerProfile                 =   eBeerProfile_Belga;

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
      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;
840
      
João Lino's avatar
João Lino committed
841
      backToStatus();
842 843 844
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
845 846
    case eBeerProfileMenu_Red: {
      beerProfile                 =   eBeerProfile_Red;
João Lino's avatar
João Lino committed
847 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_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;
871

João Lino's avatar
Rel.3  
João Lino committed
872 873
      backToStatus();
      
874 875
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
876 877 878
    case eBeerProfileMenu_APA: {
      beerProfile                 =   eBeerProfile_APA;

João Lino's avatar
João Lino committed
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_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;
902
      
João Lino's avatar
João Lino committed
903
      backToStatus();
904 905 906
      
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
907 908 909
    case eBeerProfileMenu_Custom: {
      beerProfile                 =   eBeerProfile_Custom;

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_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
933 934 935 936 937 938 939 940 941 942 943

      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 );
944

945 946 947
      break;
    }
    default: {
João Lino's avatar
Rel.3  
João Lino committed
948
    }
949
  }
950

João Lino's avatar
Rel.3  
João Lino committed
951
  eBeerProfileMenuSelection = eBeerProfileMenu_NULL;
952 953
}

João Lino's avatar
João Lino committed
954 955 956 957 958 959 960 961
void xStartStage( unsigned long *stageTime, int *stageTemperature, eCookingStages nextStage, bool bPurgePump, bool bSetFinalYield, bool bSetTime, bool bSetTemperature ) {
  xSafeHardwarePowerOff();                      // Stop anything that might be still going on
  
  if(bSetFinalYield) { finalYield = xSetFinalYield( finalYield ); }
  if(bSetTime) { (*stageTime) = getTimer( clockCounter/1000, (*stageTime) ); }
  if(bSetTemperature) { (*stageTemperature) = xSetTemperature( (*stageTemperature) ); }
  if(bPurgePump) { xPurgePump(); }
  
João Lino's avatar
João Lino committed
962
  startBrewing();
João Lino's avatar
João Lino committed
963
  xSetupStage( nextStage );
João Lino's avatar
João Lino committed
964
  backToStatus();
João Lino's avatar
João Lino committed
965 966 967 968 969
}

void xStartStageHeadless( eCookingStages nextStage, bool bPurgePump ) {
  xStartStage( NULL, NULL, nextStage, bPurgePump, false, false, false );
}
João Lino's avatar
João Lino committed
970

João Lino's avatar
João Lino committed
971 972
void xStartStageInteractive( unsigned long *stageTime, int *stageTemperature, eCookingStages nextStage ) {
  xStartStage( stageTime, stageTemperature, nextStage, true, true, true, true );
João Lino's avatar
João Lino committed
973 974 975 976 977
}

void runStartFromStageSelection() {
  switch(eStartFromStageMenuSelection) {
    case eStageMenu_Startpoint: {
João Lino's avatar
João Lino committed
978
      xStartStageInteractive( &startpointTime, &startpointTemperature, eCookingStage_Startpoint );
João Lino's avatar
João Lino committed
979 980 981
      break;
    }
    case eStageMenu_BetaGlucanase: {
João Lino's avatar
João Lino committed
982
      xStartStageInteractive( &betaGlucanaseTime, &betaGlucanaseTemperature, eCookingStage_BetaGlucanase );
João Lino's avatar
João Lino committed
983 984 985
      break;
    }
    case eStageMenu_Debranching: {
João Lino's avatar
João Lino committed
986
      xStartStageInteractive( &debranchingTime, &debranchingTemperature, eCookingStage_Debranching );
João Lino's avatar
João Lino committed
987 988 989
      break;
    }
    case eStageMenu_Proteolytic: {
João Lino's avatar
João Lino committed
990
      xStartStageInteractive( &proteolyticTime, &proteolyticTemperature, eCookingStage_Proteolytic );
João Lino's avatar
João Lino committed
991 992 993
      break;
    }
    case eStageMenu_BetaAmylase: {
João Lino's avatar
João Lino committed
994
      xStartStageInteractive( &betaAmylaseTime, &betaAmylaseTemperature, eCookingStage_BetaAmylase );
João Lino's avatar
João Lino committed
995 996 997
      break;
    }
    case eStageMenu_AlphaAmylase: {
João Lino's avatar
João Lino committed
998
      xStartStageInteractive( &alphaAmylaseTime, &alphaAmylaseTemperature, eCookingStage_AlphaAmylase );
João Lino's avatar
João Lino committed
999 1000 1001
      break;
    }
    case eStageMenu_Mashout: {
João Lino's avatar
João Lino committed
1002
      xStartStageInteractive( &mashoutTime, &mashoutTemperature, eCookingStage_Mashout );
João Lino's avatar
João Lino committed
1003 1004 1005
      break;
    }
    case eStageMenu_Recirculation: {
João Lino's avatar
João Lino committed
1006
      xStartStageInteractive( &recirculationTime, &recirculationTemperature, eCookingStage_Recirculation );
João Lino's avatar
João Lino committed
1007 1008 1009
      break;
    }
    case eStageMenu_Sparge: {
João Lino's avatar
João Lino committed
1010
      xStartStageInteractive( &spargeTime, &spargeTemperature, eCookingStage_Sparge );
João Lino's avatar
João Lino committed
1011 1012 1013
      break;
    }
    case eStageMenu_Boil: {
João Lino's avatar
João Lino committed
1014
      xStartStageInteractive( &boilTime, &boilTemperature, eCookingStage_Boil );
João Lino's avatar
João Lino committed
1015 1016 1017
      break;
    }
    case eStageMenu_Cooling: {
João Lino's avatar
João Lino committed
1018
      xStartStageInteractive( &coolingTime, &coolingTemperature, eCookingStage_Cooling );
João Lino's avatar
João Lino committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
      break;
    }
    case eStageMenu_Back: {
      resetMenu( true );
      break;
    }
    default: {
    }
  }
  eStartFromStageMenuSelection = eStageMenu_NULL;
}

João Lino's avatar
Rel.3  
João Lino committed
1031 1032
void runMainMenuSelection() {
  switch(eMainMenuSelection) {
João Lino's avatar
João Lino committed
1033 1034 1035 1036
    case eMainMenu_GO: {
      xStartStage( NULL, NULL, eCookingStage_Startpoint, true, true, false, false );
      break;
    }
João Lino's avatar
João Lino committed
1037 1038 1039 1040
    case eMainMenu_GO_FROM_STAGE: {
      eMenuType = eMenuType_StartFromStage;
      repaint = true;
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eBeerProfileMenuPosition, MENU_SIZE_PROFILES_MENU - 1, 1, 1, 0 );
João Lino's avatar
Rel.3  
João Lino committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
      break;
    }
    case eMainMenu_STOP: {
      stopBrewing();
      backToStatus();
      break;
    }
    case eMainMenu_SKIP: {
      cookTime = 0;
      backToStatus();
      break;
    }
    case eMainMenu_BeerProfile: {
      eMenuType = eMenuType_BeerProfile;
      repaint = true;
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eBeerProfileMenuPosition, MENU_SIZE_PROFILES_MENU - 1, 1, 1, 0 );
      break;
    }
    case eMainMenu_Stage: {
      eMenuType = eMenuType_Stage;
      repaint = true;
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eStageMenuPosition, MENU_SIZE_STAGE_MENU - 1, 1, 1, 0 );
      break;
    }
    case eMainMenu_Malt: {
      eMenuType = eMenuType_Malt;
      repaint = true;
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMaltMenuPosition, MENU_SIZE_MALT_MENU - 1, 1, 1, 0 );
      break;
    }
    case eMainMenu_Hops: {
      backToStatus();
      break;
    }
    case eMainMenu_Clean: {
João Lino's avatar
João Lino committed
1076
      xStartStageHeadless( eCookingStage_Clean, true );
João Lino's avatar
Rel.3  
João Lino committed
1077 1078 1079
      break;
    }
    case eMainMenu_Purge: {
João Lino's avatar
João Lino committed
1080
      xStartStageHeadless( eCookingStage_Purge, true );
João Lino's avatar
Rel.3  
João Lino committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
      break;
    }
    case eMainMenu_Settings: {
      eMenuType = eMenuType_Settings;
      repaint = true;
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eSettingsMenuPosition, MENU_SIZE_SETTINGS_MENU - 1, 1, 1, 0 );
      break;
    }
    case eMainMenu_Back: {
      backToStatus();
      break;
    }
    default: {
    }
  }
  eMainMenuSelection = eMainMenu_NULL;
1097 1098
}

João Lino's avatar
João Lino committed
1099
void xCountTheTime( int temperatureMarginRange, boolean bMaximumOfUpDown ) {
1100
  unsigned long now = millis();
João Lino's avatar
João Lino committed
1101 1102

  // Get current maximum sensed temperaure
João Lino's avatar
Rel.3  
João Lino committed
1103
  double temperatureCount = 0;
João Lino's avatar
João Lino committed
1104
  if( bMaximumOfUpDown ) {
João Lino's avatar
Rel.3  
João Lino committed
1105 1106 1107
    float tup = upPT100.getCurrentTemperature();
    float tdown = downPT100.getCurrentTemperature();
    if(tup > tdown) {
João Lino's avatar
João Lino committed
1108
      temperatureCount = tdown;
João Lino's avatar
Rel.3  
João Lino committed
1109 1110
    }
    else {
João Lino's avatar
João Lino committed
1111
      temperatureCount = tup;
João Lino's avatar
Rel.3  
João Lino committed
1112 1113 1114 1115
    }
  } else {
    temperatureCount = basePT100.getCurrentTemperature();
  }
João Lino's avatar
João Lino committed
1116

João Lino's avatar
João Lino committed
1117 1118 1119
  // Ignote time ticks if temperature is not within the acceptable margin for this stage
  unsigned long elapsedTime = now - clockLastUpdate;
  if( temperatureCount < (cookTemperature - temperatureMarginRange) ) {
João Lino's avatar
Rel.3  
João Lino committed
1120
    clockIgnore += elapsedTime;
João Lino's avatar
João Lino committed
1121 1122 1123
  }
  
  // Calculate the remaining time on the clock
João Lino's avatar
João Lino committed
1124
  clockCounter = cookTime * 1000 - (elapsedTime - clockIgnore);
João Lino's avatar
João Lino committed
1125

João Lino's avatar
João Lino committed
1126
  // Don't let clock get bellow 0
João Lino's avatar
Rel.3  
João Lino committed
1127 1128 1129 1130 1131 1132 1133
  if ( clockCounter < 0 ) {
    clockCounter = 0;
  }

  clockLastUpdate = now;

  #ifdef DEBUG_OFF
1134 1135
  debugPrintFunction("xCountTheTime");
  debugPrintVar("millis()", now);
João Lino's avatar
João Lino committed
1136
  debugPrintVar("cookTime", cookTime);
1137 1138 1139
  debugPrintVar("clockStartTime", clockStartTime);
  debugPrintVar("clockIgnore", clockIgnore);
  debugPrintVar("clockCounter", clockCounter); 
João Lino's avatar
Rel.3  
João Lino committed
1140
  #endif
1141 1142 1143
}

bool isTimeLeft() {
1144 1145
  if( clockCounter > 0 ) {
    return true;
João Lino's avatar
João Lino committed
1146
  }
1147
  return false;
1148 1149
}

1150
//HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ
1151 1152
double ulWattToWindowTime( double ulAppliedWatts ) {
  double ulPulsesRequired = ulAppliedWatts / dWattPerPulse;
1153
  return (double)iWindowSize / 1000.0 * ulPulsesRequired * 1000.0 / HEATING_ELEMENT_AC_FREQUENCY_HZ;
1154 1155
}

João Lino's avatar
João Lino committed
1156
bool xRegulateTemperature( boolean bMaximumOfUpDown ) {
João Lino's avatar
Rel.3  
João Lino committed
1157
  double difference = 0;
João Lino's avatar
João Lino committed
1158 1159 1160
  bool overTemperature = false;
  double wattage = 0.0;
  
João Lino's avatar
João Lino committed
1161 1162 1163
  float tup = upPT100.getCurrentTemperature();
  float tdown = downPT100.getCurrentTemperature();
  float tbase = basePT100.getCurrentTemperature();
João Lino's avatar
Rel.3  
João Lino committed
1164

João Lino's avatar
João Lino committed
1165
  if( bMaximumOfUpDown ) {
João Lino's avatar
Rel.3  
João Lino committed
1166 1167 1168 1169 1170 1171 1172
    if(tup > tdown) {
      difference = cookTemperature - tup;
    }
    else {
      difference = cookTemperature - tdown;
    }

João Lino's avatar
João Lino committed
1173 1174
    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
1175 1176
    }

João Lino's avatar
João Lino committed
1177
    if( (tbase < cookTemperature) && (difference < (cookTemperature - tbase)) ) {
João Lino's avatar
Rel.3  
João Lino committed
1178 1179 1180
      difference = cookTemperature - tbase;
    }
  } else {
João Lino's avatar
João Lino committed
1181
    difference = cookTemperature - tbase;
João Lino's avatar
Rel.3  
João Lino committed
1182 1183
  }

João Lino's avatar
João Lino committed
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
  // 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
1195
    //if(difference <= 0.1) {
João Lino's avatar
João Lino committed
1196
      // turn it off
João Lino's avatar
João Lino committed
1197 1198
    //  wattage = 0.0;
    //} else {
João Lino's avatar
Rel.3  
João Lino committed
1199
      if(difference <= 0.5) {
João Lino's avatar
João Lino committed
1200
        // pulse lightly at 500 watt
João Lino's avatar
Rel.3  
João Lino committed
1201
        if(cookTemperature > 99.0) {
João Lino's avatar
João Lino committed
1202
          wattage = 2000.0;
João Lino's avatar
Rel.3  
João Lino committed
1203 1204
        }
        else {
João Lino's avatar
João Lino committed
1205 1206 1207 1208 1209 1210
          if(cookTemperature > 70.0) {
            wattage = 1000.0;
          }
          else {
            wattage = 500.0;
          }
João Lino's avatar
Rel.3  
João Lino committed
1211
        }
João Lino's avatar
João Lino committed
1212
      } else {
João Lino's avatar
Rel.3  
João Lino committed
1213
        if(difference <= 1.0) {
João Lino's avatar
João Lino committed
1214
          // pulse moderately at 1000 watt
João Lino's avatar
João Lino committed
1215 1216 1217 1218 1219 1220 1221
          if(cookTemperature > 99.0) {
            wattage = 2000.0;
          }
          else {
            wattage = 1000.0;
          }
          
João Lino's avatar
João Lino committed
1222
        } else {
João Lino's avatar
Rel.3  
João Lino committed
1223
          if(difference <= 3.0) {
João Lino's avatar
João Lino committed
1224
            // pulse hardly at 2000 watt
1225
            wattage = 2000.0;
João Lino's avatar
João Lino committed
1226 1227 1228 1229 1230 1231
          } else {
            //pulse constantly at HEATING_ELEMENT_MAX_WATTAGE watt
            wattage = HEATING_ELEMENT_MAX_WATTAGE;
          }
        }
      }
João Lino's avatar
João Lino committed
1232
    //}
João Lino's avatar
João Lino committed
1233 1234 1235
  }
  
  // Update the recorded time for the begining of the window, if the previous window has passed
1236 1237
  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;
1238
  }
João Lino's avatar
João Lino committed
1239 1240
  
  // Apply wattage to the element at the right time
1241
  if( ulWattToWindowTime( wattage ) > (millis() - windowStartTime) ) {
1242
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN,HIGH);
João Lino's avatar
Rel.3  
João Lino committed
1243
    bStatusElement = true;
João Lino's avatar
João Lino committed
1244 1245
  } else {
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN,LOW);
João Lino's avatar
Rel.3  
João Lino committed
1246
    bStatusElement = false;
João Lino's avatar
João Lino committed
1247
  }
1248

João Lino's avatar
Rel.3  
João Lino committed
1249
  #ifdef DEBUG_OFF
1250
  //debugPrintFunction("xRegulateTemperature");
1251
  debugPrintVar("difference", difference);
1252
  //debugPrintVar("overTemperature", overTemperature);
1253
  debugPrintVar("wattage", wattage);
1254 1255 1256 1257
  //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
1258 1259 1260 1261 1262
  #endif
}

void xPurgePump() {
  for(int i = 0; i < 2; i++) {
João Lino's avatar
João Lino committed
1263
    analogWrite(PUMP_PIN, PUMP_SPEED_MAX_MOSFET);  // analogWrite values from 0 to 255
João Lino's avatar
Rel.3  
João Lino committed
1264
    delay(1000);
João Lino's avatar
João Lino committed
1265
    analogWrite(PUMP_PIN, PUMP_SPEED_STOP_MOSFET);  // analogWrite values from 0 to 255
João Lino's avatar
Rel.3  
João Lino committed
1266 1267
    delay(1500);
  }
1268 1269
}

1270
bool xRegulatePumpSpeed() {
João Lino's avatar
Rel.3  
João Lino committed
1271 1272 1273
  //  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
1274
    analogWrite(PUMP_PIN, PUMP_SPEED_STOP_MOSFET);  // analogWrite values from 0 to 255
1275

João Lino's avatar
João Lino committed
1276 1277 1278
    basePT100.setPumpStatus( false );
    upPT100.setPumpStatus( false );
    downPT100.setPumpStatus( false );
1279 1280
  }
  else {
João Lino's avatar
Rel.3  
João Lino committed
1281 1282
    analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255

João Lino's avatar
João Lino committed
1283 1284 1285
    basePT100.setPumpStatus( true );
    upPT100.setPumpStatus( true );
    downPT100.setPumpStatus( true );
1286
  }
1287 1288
}

1289
void xWarnClockEnded() {
1290
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);
1291 1292
}

João Lino's avatar
Rel.3  
João Lino committed
1293 1294 1295 1296
void xWarnCookEnded() {
  sing(MELODY_UNDERWORLD_SHORT, PIEZO_PIN);
}

João Lino's avatar
João Lino committed
1297
void xPrepareForStage( int stageTime, int stageTemperature, int stagePumpSpeed, eCookingStages stage ) {
João Lino's avatar
Rel.3  
João Lino committed
1298
  #ifdef DEBUG_OFF
João Lino's avatar
João Lino committed
1299
  debugPrintFunction("xPrepareForStage");
João Lino's avatar
Rel.3  
João Lino committed
1300 1301 1302
  debugPrintVar("cookingStage", stage);
  #endif

João Lino's avatar
João Lino committed
1303
  // Reset the clock
João Lino's avatar
João Lino committed
1304 1305 1306 1307
  unsigned long now = millis();
  clockStartTime  = now;
  clockLastUpdate = now;
  clockIgnore     = 0;
1308

João Lino's avatar
João Lino committed
1309 1310 1311 1312
  iPumpSpeed      = stagePumpSpeed;         // Set the pump speed
  cookingStage    = stage;                  // Set Stage
  cookTime        = stageTime;              // Set the clock
  cookTemperature = stageTemperature;       // Set the target temperature
1313 1314
}

João Lino's avatar
João Lino committed
1315
void xSetupStage(eCookingStages nextStage) {
João Lino's avatar
Rel.3  
João Lino committed
1316
  #ifdef DEBUG_OFF
João Lino's avatar
João Lino committed
1317
  debugPrintFunction("xSetupStage");
João Lino's avatar
Rel.3  
João Lino committed
1318 1319
  debugPrintVar("cookingStage", nextStage);
  #endif
João Lino's avatar
João Lino committed
1320 1321 1322 1323

  // Operate the machine according to the current mode
  switch(nextStage) {
    case eCookingStage_Startpoint: {
João Lino's avatar
Rel.3  
João Lino committed
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
      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
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
        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
1357 1358 1359 1360 1361 1362 1363 1364 1365
        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
1366
      xPrepareForStage( startpointTime, startpointTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Startpoint );
João Lino's avatar
João Lino committed
1367 1368 1369
      break;
    }
    case eCookingStage_BetaGlucanase: {
João Lino's avatar
Rel.3  
João Lino committed
1370 1371 1372 1373 1374 1375 1376 1377 1378
      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.");
João Lino's avatar
João Lino committed
1379
          
João Lino's avatar
Rel.3  
João Lino committed
1380 1381 1382 1383
          xWaitForAction("Malt", say);
          repaint = true;
          break;
        }
João Lino's avatar
João Lino committed
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
        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
João Lino committed
1403
        default: {}
João Lino's avatar
Rel.3  
João Lino committed
1404
      }
João Lino's avatar
João Lino committed
1405
      xPrepareForStage( betaGlucanaseTime, betaGlucanaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_BetaGlucanase );
João Lino's avatar
João Lino committed
1406 1407 1408
      break;
    }
    case eCookingStage_Debranching: {
João Lino's avatar
João Lino committed
1409
      xPrepareForStage( debranchingTime, debranchingTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Debranching );
João Lino's avatar
João Lino committed
1410 1411 1412
      break;
    }
    case eCookingStage_Proteolytic: {
João Lino's avatar
João Lino committed
1413
      xPrepareForStage( proteolyticTime, proteolyticTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Proteolytic );
João Lino's avatar
João Lino committed
1414 1415 1416
      break;
    }
    case eCookingStage_BetaAmylase: {
João Lino's avatar
João Lino committed
1417
      xPrepareForStage( betaAmylaseTime, betaAmylaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_BetaAmylase );
João Lino's avatar
João Lino committed
1418 1419 1420
      break;
    }
    case eCookingStage_AlphaAmylase: {
João Lino's avatar
João Lino committed
1421
      xPrepareForStage( alphaAmylaseTime, alphaAmylaseTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_AlphaAmylase );
João Lino's avatar
João Lino committed
1422 1423 1424
      break;
    }
    case eCookingStage_Mashout: {
João Lino's avatar
João Lino committed
1425
      xPrepareForStage( mashoutTime, mashoutTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Mashout );
João Lino's avatar
João Lino committed
1426 1427
      break;
    }
João Lino's avatar
João Lino committed
1428
    case eCookingStage_Recirculation: {
João Lino's avatar
Rel.3  
João Lino committed
1429 1430
      xWaitForAction("Sparge Water", "Start heating your sparge water.");
      repaint = true;
João Lino's avatar
João Lino committed
1431
      xPrepareForStage( recirculationTime, recirculationTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Recirculation );
João Lino's avatar
João Lino committed
1432 1433 1434
      break;
    }
    case eCookingStage_Sparge: {
João Lino's avatar
Rel.3  
João Lino committed
1435 1436
      xWaitForAction("Sparge Water", "Start pouring the sparge water.");
      repaint = true;
João Lino's avatar
João Lino committed
1437
      xPrepareForStage( spargeTime, spargeTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Sparge );
João Lino's avatar
João Lino committed
1438 1439 1440
      break;
    }
    case eCookingStage_Boil: {
João Lino's avatar
Rel.3  
João Lino committed
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
      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
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
        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
1466 1467 1468 1469 1470 1471 1472 1473
        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
1474
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1475
      xPrepareForStage( boilTime, boilTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Boil );
João Lino's avatar
João Lino committed
1476 1477 1478 1479
      
      break;
    }
    case eCookingStage_Cooling: {
João Lino's avatar
Rel.3  
João Lino committed
1480 1481 1482 1483 1484 1485
      // 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
1486
      xPrepareForStage( coolingTime, coolingTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Cooling );
João Lino's avatar
Rel.3  
João Lino committed
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498

      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
1499
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1500
      xPrepareForStage( cleaningTime, cleaningTemperature, PUMP_SPEED_MAX_MOSFET, eCookingStage_Clean );
João Lino's avatar
João Lino committed
1501 1502 1503 1504 1505

      break;
    }
    case eCookingStage_Purge: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1506
      xPrepareForStage( 0, 0, PUMP_SPEED_MAX_MOSFET, eCookingStage_Purge );
João Lino's avatar
João Lino committed
1507 1508 1509 1510 1511 1512 1513

      xRegulatePumpSpeed();

      break;
    }
    case eCookingStage_Done: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1514
      xPrepareForStage( 0, 0, PUMP_SPEED_STOP_MOSFET, eCookingStage_Done );
João Lino's avatar
João Lino committed
1515 1516 1517 1518

      break;
    }
  }
1519 1520 1521
}

void xTransitionIntoStage(eCookingStages nextStage) {
João Lino's avatar
João Lino committed
1522 1523 1524 1525 1526 1527 1528
  // 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
João Lino committed
1529
  xSetupStage( nextStage );
1530 1531
}

João Lino's avatar
João Lino committed
1532
void xBasicStageOperation( int iStageTime, int iStageTemperature, int iStageTemperatureRange, eCookingStages nextStage, boolean bMaximumOfUpDown ) {
João Lino's avatar
João Lino committed
1533

João Lino's avatar
João Lino committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
  // Account for time spent at the target temperature | Input 1: range in ºC within which the target temperature is considered to be reached
  #ifdef DEBUG_OFF
  xCountTheTime( iStageTemperatureRange, false );
  #else
  xCountTheTime( iStageTemperatureRange, bMaximumOfUpDown );
  #endif
  
  if( isTimeLeft() ) {
    // Do temperature control
    xRegulateTemperature( bMaximumOfUpDown );

    // Do flow control
    xRegulatePumpSpeed();
João Lino's avatar
João Lino committed
1547 1548
    
  } else {
João Lino's avatar
Rel.3  
João Lino committed
1549
    #ifdef DEBUG_OFF
João Lino's avatar
João Lino committed
1550 1551
    debugPrintFunction("xBasicStageOperation");
    debugPrintVar("clockCounter", clockCounter);
João Lino's avatar
Rel.3  
João Lino committed
1552
    #endif
João Lino's avatar
João Lino committed
1553
    
João Lino's avatar
João Lino committed
1554 1555 1556
    // Continue to the next stage, there is nothing to do, in this stage
    xTransitionIntoStage( nextStage );
    return;
João Lino's avatar
João Lino committed
1557
  }
João Lino's avatar
João Lino committed
1558
  
João Lino's avatar
João Lino committed
1559
  return;
1560 1561
}

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

João Lino's avatar
Rel.3  
João Lino committed
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
  #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
1584 1585

  // Measure temperature, for effect
João Lino's avatar
João Lino committed
1586 1587 1588
  basePT100.measure(false);
  upPT100.measure(false);
  downPT100.measure(true);
1589 1590 1591

  // 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
1592
    xSafeHardwarePowerOff();
1593

1594 1595
    return;
  }
João Lino's avatar
João Lino committed
1596 1597 1598 1599 1600
  
  // 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
1601
      xBasicStageOperation( startpointTime, startpointTemperature, 0, eCookingStage_BetaGlucanase, false);
João Lino's avatar
João Lino committed
1602 1603 1604 1605 1606
      
      break;
    }
    case eCookingStage_BetaGlucanase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1607
      xBasicStageOperation( betaGlucanaseTime, betaGlucanaseTemperature, 3, eCookingStage_Debranching, true );
João Lino's avatar
João Lino committed
1608 1609 1610 1611 1612
      
      break;
    }
    case eCookingStage_Debranching: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1613
      xBasicStageOperation( debranchingTime, debranchingTemperature, 3, eCookingStage_Proteolytic, true );
João Lino's avatar
João Lino committed
1614 1615 1616 1617 1618
      
      break;
    }
    case eCookingStage_Proteolytic: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1619
      xBasicStageOperation( proteolyticTime, proteolyticTemperature, 3, eCookingStage_BetaAmylase, true );
João Lino's avatar
João Lino committed
1620 1621 1622 1623 1624
      
      break;
    }
    case eCookingStage_BetaAmylase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1625
      xBasicStageOperation( betaAmylaseTime, betaAmylaseTemperature, 4, eCookingStage_AlphaAmylase, true );
João Lino's avatar
João Lino committed
1626 1627 1628 1629 1630
      
      break;
    }
    case eCookingStage_AlphaAmylase: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1631
      xBasicStageOperation( alphaAmylaseTime, alphaAmylaseTemperature, 2, eCookingStage_Mashout, true );
João Lino's avatar
João Lino committed
1632 1633 1634 1635 1636
      
      break;
    }
    case eCookingStage_Mashout: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1637
      xBasicStageOperation( mashoutTime, mashoutTemperature, 1, eCookingStage_Recirculation, true );
João Lino's avatar
João Lino committed
1638 1639 1640 1641 1642
      
      break;
    }
    case eCookingStage_Recirculation: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1643
      xBasicStageOperation( recirculationTime, recirculationTemperature, 1, eCookingStage_Sparge, true );
João Lino's avatar
João Lino committed
1644 1645 1646 1647 1648
      
      break;
    }
    case eCookingStage_Sparge: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1649
      xBasicStageOperation( spargeTime, spargeTemperature, 3, eCookingStage_Boil, false );
João Lino's avatar
João Lino committed
1650 1651 1652 1653 1654
      
      break;
    }
    case eCookingStage_Boil: {
      // A basic operation for a basic stage
João Lino's avatar
Rel.3  
João Lino committed
1655
      xBasicStageOperation( boilTime, boilTemperature, 2, eCookingStage_Cooling, false );
João Lino's avatar
João Lino committed
1656 1657 1658 1659 1660
      
      break;
    }
    case eCookingStage_Cooling: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1661
      xBasicStageOperation( coolingTime, coolingTemperature, 0, eCookingStage_Done, false );
João Lino's avatar
Rel.3  
João Lino committed
1662 1663 1664 1665 1666

      break;
    }
    case eCookingStage_Clean: {
      // A basic operation for a basic stage
João Lino's avatar
João Lino committed
1667
      xBasicStageOperation( cleaningTime, cleaningTemperature, 0, eCookingStage_Done, false );
1668 1669 1670 1671 1672 1673

      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
1674
      iPumpSpeed = PUMP_SPEED_MAX_MOSFET;
1675 1676 1677

      xRegulatePumpSpeed();

João Lino's avatar
João Lino committed
1678 1679
      break;
    }
João Lino's avatar
Rel.3  
João Lino committed
1680 1681 1682 1683 1684 1685
    case eCookingStage_Done: {
      // Update cooking state
      stopBrewing();

      // Ask for screen refresh
      repaint = true;
João Lino's avatar
João Lino committed
1686 1687 1688
      
      // Warn the user that the cooking is done
      xWarnCookEnded();
João Lino's avatar
Rel.3  
João Lino committed
1689 1690

      break;
João Lino's avatar
João Lino committed
1691 1692
    }
  }
1693 1694 1695 1696 1697
}

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

void startBrewing() {
1698 1699
  //sing(MELODY_SUPER_MARIO, PIEZO_PIN);

1700 1701 1702 1703 1704 1705 1706
  cooking = true;
}

void stopBrewing() {
  cooking = false;
}

João Lino's avatar
João Lino committed
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
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 );
}

1718 1719
void backToStatus() {
  lastInterruptTime = millis() - SETTING_MAX_INACTIVITY_TIME - 1;
João Lino's avatar
João Lino committed
1720
  resetMenu(true);
1721 1722 1723 1724
}
// #################################################### Helpers ##################################################################

// #################################################### Set Variables ##################################################################
João Lino's avatar
João Lino committed
1725
int getTimer( int initialValue, int defaultValue ) {
João Lino's avatar
João Lino committed
1726
  // 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
1727
  xSetupRotaryEncoder( eRotaryEncoderMode_Time, initialValue, 7200, 0, 1, 30 );
1728 1729

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1730
  int rotaryEncoderPreviousPosition = 0;
1731 1732 1733 1734 1735 1736
  int minutes = 0;
  int seconds = 0;
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
João Lino's avatar
João Lino committed
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
  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(")");
1747
  lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1748 1749 1750
  lcd.print("      0:00");
  
  while(true) {
1751
    // Check if pushbutton is pressed
João Lino's avatar
João Lino committed
1752
    if ((digitalRead(ROTARY_ENCODER_SW_PIN))) {  
1753
      // Wait until switch is released
João Lino's avatar
João Lino committed
1754
      while (digitalRead(ROTARY_ENCODER_SW_PIN)) {}  
1755
      
João Lino's avatar
João Lino committed
1756 1757
      // debounce
      delay(10);
1758

João Lino's avatar
João Lino committed
1759
      // Job is done, break the circle
1760
      break;
1761
    } else {
João Lino's avatar
João Lino committed
1762
      // Don't forget to keep an eye on the cooking
João Lino's avatar
Rel.3  
João Lino committed
1763
      xManageMachineSystems();
João Lino's avatar
João Lino committed
1764
    }
1765 1766
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
1767 1768 1769 1770
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      minutes = rotaryEncoderVirtualPosition/60;
      seconds = rotaryEncoderVirtualPosition-minutes*60;
1771
      
1772
      lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
João Lino's avatar
João Lino committed
1773 1774 1775 1776 1777 1778 1779 1780
      
      if(minutes<100) {
        lcd.print(" ");
      }
      if(minutes<10) {
        lcd.print(" ");
      }
      lcd.print("    ");
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
      lcd.print(minutes);
      lcd.print(":");
      if(seconds<10) {
        lcd.print("0");
      }
      lcd.print(seconds);
      lcd.println("                ");
    }
  }
  
service-config's avatar
Mixer  
service-config committed
1791
  return rotaryEncoderVirtualPosition;
1792 1793
}

João Lino's avatar
João Lino committed
1794 1795 1796 1797 1798
int getTimer( int initialValue ) {
  return getTimer( initialValue, initialValue );
}

int getTemperature(int initialValue) {
1799 1800
  
  // set operation state
1801
  rotaryEncoderMode = eRotaryEncoderMode_Generic;
João Lino's avatar
João Lino committed
1802
  rotaryEncoderVirtualPosition = initialValue;  
1803 1804

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1805
  int rotaryEncoderPreviousPosition = 0;
1806 1807 1808 1809 1810
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Temperature");
1811
  lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1812 1813
  lcd.print("       0 *C");
  
1814
  rotaryEncoderMaxPosition = TEMPERATURE_SETTING_MAX_VALUE;
service-config's avatar
Mixer  
service-config committed
1815
  
1816
  while(true) {
1817
    // Check if pushbutton is pressed
João Lino's avatar
João Lino committed
1818
    if ((digitalRead(ROTARY_ENCODER_SW_PIN))) {  
1819
      // Wait until switch is released
João Lino's avatar
João Lino committed
1820
      while (digitalRead(ROTARY_ENCODER_SW_PIN)) {}  
1821
      
João Lino's avatar
João Lino committed
1822 1823
      // debounce
      delay(10);
1824

João Lino's avatar
João Lino committed
1825
      // Job is done, break the circle
1826
      break;
1827
    } else {
João Lino's avatar
João Lino committed
1828
      // Don't forget to keep an eye on the cooking
João Lino's avatar
Rel.3  
João Lino committed
1829
      xManageMachineSystems();
João Lino's avatar
João Lino committed
1830
    }
1831 1832
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
1833 1834
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
1835
      
1836
      lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1837
      lcd.print("     ");
service-config's avatar
Mixer  
service-config committed
1838
      if(rotaryEncoderVirtualPosition<10) {
1839 1840 1841
        lcd.print("  ");
      }
      else {
service-config's avatar
Mixer  
service-config committed
1842
        if(rotaryEncoderVirtualPosition<100) {
1843 1844 1845
          lcd.print(" ");
        }
      }
service-config's avatar
Mixer  
service-config committed
1846
      lcd.print(rotaryEncoderVirtualPosition);
1847 1848 1849 1850 1851
      lcd.print(" *C");
      lcd.println("                ");
    }
  }
  
service-config's avatar
Mixer  
service-config committed
1852
  return rotaryEncoderVirtualPosition;
1853 1854
}

João Lino's avatar
João Lino committed
1855
int xSetGenericValue(int initialValue, int minimumValue, int maximumValue, const char *valueName, const char *unit) {  
João Lino's avatar
João Lino committed
1856
  // 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
1857
  xSetupRotaryEncoder( eRotaryEncoderMode_Generic, initialValue, maximumValue, minimumValue, 1, 5 );
1858 1859

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1860
  int rotaryEncoderPreviousPosition = 0;
1861 1862 1863
  
  // Setup Screen
  lcd.clear();
1864 1865 1866 1867 1868
  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
1869
  lcd.print( unit );
1870
  
1871
  while(true) {
1872 1873 1874
    // 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
1875
      while ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {}  
1876
      
João Lino's avatar
João Lino committed
1877 1878
      // debounce
      delay( 10 );
service-config's avatar
Mixer  
service-config committed
1879

João Lino's avatar
João Lino committed
1880
      // Job is done, break the circle
service-config's avatar
Mixer  
service-config committed
1881
      break;
1882
    } else {
João Lino's avatar
João Lino committed
1883
      // Don't forget to keep an eye on the cooking
João Lino's avatar
Rel.3  
João Lino committed
1884
      xManageMachineSystems();
João Lino's avatar
João Lino committed
1885
    }
service-config's avatar
Mixer  
service-config committed
1886
    
1887 1888
    // Check if there was an update by the rotary encoder
    if( rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition ) {
service-config's avatar
Mixer  
service-config committed
1889 1890
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      
1891 1892 1893 1894
      lcd.setCursor( 0, LCD_VERTICAL_RESOLUTION - 1 );
      lcd.print( "     " );
      if( rotaryEncoderVirtualPosition < 10 ) {
        lcd.print( "  " );
1895 1896
      }
      else {
1897 1898
        if( rotaryEncoderVirtualPosition < 100 ) {
          lcd.print( " " );
1899 1900
        }
      }
1901
      lcd.print( rotaryEncoderVirtualPosition );
1902 1903
      lcd.print( " " );
      lcd.print( unit );
1904
      lcd.println( "                " );
1905 1906
    }
  }
1907 1908
  
  return rotaryEncoderVirtualPosition;
1909 1910
}

João Lino's avatar
João Lino committed
1911 1912 1913 1914 1915 1916 1917 1918
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" );
}

João Lino's avatar
João Lino committed
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
unsigned long getInactivityTime() {
  unsigned long now = millis();
  unsigned long rotaryEncoderInactivityTime = now - lastInterruptTime;

  if(rotaryEncoderInactivityTime > SETTING_MAX_INACTIVITY_TIME) {
    if (digitalRead(ROTARY_ENCODER_SW_PIN)) {
        while(digitalRead(ROTARY_ENCODER_SW_PIN)) {
          delay(ROTARY_ENCODER_SW_DEBOUNCE_TIME);
        }
        
        now = millis();
        rotaryEncoderInactivityTime = now - lastInterruptTime;
        rotarySwDetectTime = now;
        
        repaint = true;
        refresh = true;
    }
  }
  
  unsigned long switchInactivityTime = now - rotarySwDetectTime;
  return rotaryEncoderInactivityTime > switchInactivityTime ? switchInactivityTime : rotaryEncoderInactivityTime ;
}

1942
// ###################### Set Variables ##################################################
1943

João Lino's avatar
Rel.3  
João Lino committed
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
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;
      }
1963 1964 1965 1966
    }
  }
}

João Lino's avatar
Rel.3  
João Lino committed
1967 1968 1969 1970 1971 1972 1973 1974
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
  } 
1975

João Lino's avatar
Rel.3  
João Lino committed
1976 1977
  return ret;
}