brew.ino 53.7 KB
Newer Older
1
#define DEBUG
service-config's avatar
Mixer  
service-config committed
2

3 4 5
// ######################### CONSTANTS #########################

// ######################### SETTINGS #########################
6
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
7 8 9 10 11 12
#define HEATING_ELEMENT_DEFAULT_WINDOW_SIZE       1000
#define HEATING_ELEMENT_OUTPUT_PIN                24
#define HEATING_ELEMENT_MAX_HEAT_PWM_INTEGER      5
#define HEATING_ELEMENT_MAX_HEAT_PWM_FLOAT        5.0
#define HEATING_ELEMENT_MAX_WATTAGE               3000.0          // Minimum = 2000.0
#define HEATING_ELEMENT_AC_FREQUENCY_HZ           50.0
13

service-config's avatar
Mixer  
service-config committed
14
// ++++++++++++++++++++++++ Temperature ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
15
#define PT100_BASE_INPUT_PIN                      A4
16
#define PT100_BASE_OUTPUT_PIN                     32
João Lino's avatar
João Lino committed
17 18
#define PT100_BASE_TIME_BETWEEN_READINGS          100
#define PT100_UP_INPUT_PIN                        A5
19
#define PT100_UP_OUTPUT_PIN                       30
João Lino's avatar
João Lino committed
20 21
#define PT100_UP_TIME_BETWEEN_READINGS            100
#define PT100_DOWN_INPUT_PIN                      A6
22
#define PT100_DOWN_OUTPUT_PIN                     31
João Lino's avatar
João Lino committed
23 24
#define PT100_DOWN_TIME_BETWEEN_READINGS          100

25 26
#define PT100_BASE_DEFAULT_ADC_VMAX               1.1
#define PT100_BASE_DEFAULT_VS                     5.0
27 28
#define PT100_BASE_DEFAULT_R1_RESISTENCE          605.2 //608.99
#define PT100_BASE_DEFAULT_LINE_RESISTENCE        0.0
João Lino's avatar
João Lino committed
29
#define PT100_BASE_DEFAULT_OPERATION_RESISTENCE   0.0
30 31
#define PT100_UP_DEFAULT_ADC_VMAX                 1.1
#define PT100_UP_DEFAULT_VS                       5.0
32 33
#define PT100_UP_DEFAULT_R1_RESISTENCE            615.6 //621 //620 //618.1
#define PT100_UP_DEFAULT_LINE_RESISTENCE          0.0
João Lino's avatar
João Lino committed
34
#define PT100_UP_DEFAULT_OPERATION_RESISTENCE     0.0
35 36
#define PT100_DOWN_DEFAULT_ADC_VMAX               1.1
#define PT100_DOWN_DEFAULT_VS                     5.0
37 38
#define PT100_DOWN_DEFAULT_R1_RESISTENCE          618.6 //619.3 //617.2
#define PT100_DOWN_DEFAULT_LINE_RESISTENCE        0.0
João Lino's avatar
João Lino committed
39
#define PT100_DOWN_DEFAULT_OPERATION_RESISTENCE   0.0
40

service-config's avatar
Mixer  
service-config committed
41
// ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
42 43
//#define MIXER_PIN     12
//#define MIXER_MAX_POSITION   255
44

45 46 47 48
// ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
#include "Melody.h"
#define PIEZO_PIN                                 25

49 50 51 52 53 54 55 56
// ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
#define PUMP_PIN                                  6
#define PUMP_SPEED_STOP                           0
#define PUMP_SPEED_SLOW                           64
#define PUMP_SPEED_AVERAGE                        128
#define PUMP_SPEED_FAST                           192
#define PUMP_SPEED_MAX                            255

57
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
58 59 60 61
#define ROTARY_ENCODER_INTERRUPT_NUMBER           1    // On Mega2560 boards, interrupt 1 is on pin 3
#define ROTARY_ENCODER_CLK_PIN                    3    // Used for generating interrupts using CLK signal
#define ROTARY_ENCODER_DT_PIN                     22    // Used for reading DT signal
#define ROTARY_ENCODER_SW_PIN                     23    // Used for the push button switch
62
#define ROTARY_ENCODER_DEBOUNCE_TIME              20    // Number of miliseconds to ignore new signals a signal is received
63

64
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
65 66 67 68 69 70 71
#define SETTING_WELCOME_TIMEOUT                   100
#define SETTING_MAX_INACTIVITY_TIME               3000
#define MENU_MAX_DEPTH                            10
#define MENU_INIT_VALUES                          -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
#define MENU_SIZE_MAIN_MENU                       17
#define SETTING_SERIAL_MONITOR_BAUD_RATE          9600
#define SETTING_SERIAL_MONITOR_WELCOME_MESSAGE    "Let's start Brewing!"
72

73 74 75 76 77
// ######################### LIBRARIES #########################
// ++++++++++++++++++++++++ LiquidCrystal_I2C ++++++++++++++++++++++++
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
78

João Lino's avatar
João Lino committed
79 80 81 82 83 84 85 86 87 88 89
#define LCD_I2C_ADDR                              0x27    // <<----- Add your address here.  Find it from I2C Scanner
#define LCD_HORIZONTAL_RESOLUTION                 16
#define LCD_VERTICAL_RESOLUTION                   2
#define LCD_BACKLIGHT_PIN                         3
#define LCD_EN_PIN                                2
#define LCD_RW_PIN                                1
#define LCD_RS_PIN                                0
#define LCD_D4_PIN                                4
#define LCD_D5_PIN                                5
#define LCD_D6_PIN                                6
#define LCD_D7_PIN                                7
90 91 92 93

// ++++++++++++++++++++++++ PT100 +++++++++++++++++++++++++++++++++
#include <PT100.h>

94 95
// ++++++++++++++++++++++++ ENUM +++++++++++++++++++++++++++++++++
#include "CustomDataStructures.h"
service-config's avatar
Mixer  
service-config committed
96

97 98 99 100 101 102 103 104 105 106
// ######################### TEMPLATES #########################
// ++++++++++++++++++++++++ Debug ++++++++++++++++++++++++
template <class T> void debugPrintVar( char *name, const T& value );
template <class T> void debugPrintVar( char *name, const T& value ) {
  Serial.print("[");
  Serial.print(name);
  Serial.print(":");
  Serial.print(value);
  Serial.println("]");
}
107 108 109 110 111
void debugPrintFunction( char *name ) {
  Serial.print("++++++++++++++++++++++++ ");
  Serial.print(name);
  Serial.println("++++++++++++++++++++++++");
}
112

113
// ######################### VARIABLES #########################
114
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
115 116 117 118 119 120 121
eRotaryEncoderMode      rotaryEncoderMode;
eMainMenuOptions        mainMenuOption;
ePresetsMenuOptions     presetsMenuOption;
eMaltMenuOptions        maltMenuOption;
eSettingsMenuOptions    settingsMenuOption;
eCookingStages          cookingStage;

122
// ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
123 124 125
boolean                 cooking;
boolean                 bStageFirstRun;

João Lino's avatar
João Lino committed
126 127 128
unsigned long                     clockStartTime;
unsigned long                     clockCounter;
unsigned long                     clockIgnore;
João Lino's avatar
João Lino committed
129 130 131
boolean                 clockStart;
boolean                 clockEnd;

João Lino's avatar
João Lino committed
132
unsigned long                     cookTime;
João Lino's avatar
João Lino committed
133 134 135 136 137
int                     cookTemperature;
//cook_mode_list        cookMode;
//int                   cookMixerSpeed;
int                     cookHeatPWM;
        
João Lino's avatar
João Lino committed
138 139 140 141 142 143 144 145 146 147 148
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;
João Lino's avatar
João Lino committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        
int                     startpointTemperature;
int                     betaGlucanaseTemperature;
int                     debranchingTemperature;
int                     proteolyticTemperature;
int                     betaAmylaseTemperature;
int                     alphaAmylaseTemperature;
int                     mashoutTemperature;
int                     recirculationTemperature;
int                     spargeTemperature;
int                     boilTemperature;
int                     coolingTemperature;

//int                   menuSize;
int                     menu_position[MENU_MAX_DEPTH] = {MENU_INIT_VALUES};

boolean                 refresh;
boolean                 repaint;
167 168

// ++++++++++++++++++++++++ Interrupts ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
169
static unsigned long    lastInterruptTime;
170

171
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
172 173 174 175 176
volatile int            rotaryEncoderVirtualPosition = 0;
volatile int            rotaryEncoderMaxPosition = 1;
volatile int            rotaryEncoderMinPosition = 0;
volatile int            rotaryEncoderSingleStep = 1;
volatile int            rotaryEncoderMultiStep = 1;
177

João Lino's avatar
João Lino committed
178 179
volatile boolean        onISR = false;

180
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
181 182 183
int                     iWindowSize;             // Time frame to operate in
unsigned long           windowStartTime;
double                  dWattPerPulse;
184

185 186 187
// ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
int                     iPumpSpeed;             // Time frame to operate in

188
// ######################### INITIALIZE #########################
189
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
João Lino's avatar
João Lino committed
190
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);
191 192

// +++++++++++++++++++++++ PT100 +++++++++++++++++++++++
193 194 195
PT100                   basePT100("base", PT100_BASE_OUTPUT_PIN, PT100_BASE_INPUT_PIN, PT100_BASE_TIME_BETWEEN_READINGS, PT100_BASE_DEFAULT_ADC_VMAX, PT100_BASE_DEFAULT_VS, PT100_BASE_DEFAULT_R1_RESISTENCE, PT100_BASE_DEFAULT_LINE_RESISTENCE, PT100_BASE_DEFAULT_OPERATION_RESISTENCE, 0.0, 0.0);
PT100                   upPT100("up", PT100_UP_OUTPUT_PIN, PT100_UP_INPUT_PIN, PT100_UP_TIME_BETWEEN_READINGS, PT100_UP_DEFAULT_ADC_VMAX, PT100_UP_DEFAULT_VS, PT100_UP_DEFAULT_R1_RESISTENCE, PT100_UP_DEFAULT_LINE_RESISTENCE, PT100_UP_DEFAULT_OPERATION_RESISTENCE, 0.112329092, -3.57);
PT100                   downPT100("down", PT100_DOWN_OUTPUT_PIN, PT100_DOWN_INPUT_PIN, PT100_DOWN_TIME_BETWEEN_READINGS, PT100_DOWN_DEFAULT_ADC_VMAX, PT100_DOWN_DEFAULT_VS, PT100_DOWN_DEFAULT_R1_RESISTENCE, PT100_DOWN_DEFAULT_LINE_RESISTENCE, PT100_DOWN_DEFAULT_OPERATION_RESISTENCE, 0.22, -5.5);
196 197

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

285 286 287 288 289 290 291 292 293
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;
}

294
// ######################### START #########################
295
void xSafeHardwarePowerOff() {
296 297 298 299 300
  // Turn off gracefully
  iPumpSpeed = PUMP_SPEED_STOP;
  xRegulatePumpSpeed();

  // Force shutdown
301
  analogWrite(PUMP_PIN, PUMP_SPEED_STOP);  // analogWrite values from 0 to 255
João Lino's avatar
João Lino committed
302
  digitalWrite(HEATING_ELEMENT_OUTPUT_PIN, LOW);  // Turn heading element OFF for safety
303 304

//analogWrite(MIXER_PIN, 0);        // Turn mixer OFF for safety
305 306 307
}

void displayWelcome() {
308
#ifndef DEBUG
João Lino's avatar
João Lino committed
309
  lcdPrint("  Let's start", "    Brewing!");    // Write welcome
310

311 312 313
  // Play Melody;
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);

314
  //termometerCalibration();
João Lino's avatar
João Lino committed
315
  delay(SETTING_WELCOME_TIMEOUT);      // pause for effect
316
#endif
317 318
}

319
void setup() {
João Lino's avatar
João Lino committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  // ++++++++++++++++++++++++ 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);
  windowStartTime             =   millis();
  dWattPerPulse               =   HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ;

  // ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
  //  pinMode    (MIXER_PIN, OUTPUT);
  //  analogWrite    (MIXER_PIN, 0);

336 337 338 339 340
  // ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
  pinMode(PUMP_PIN, OUTPUT);   // sets the pin as output
  iPumpSpeed                  =   PUMP_SPEED_STOP;             // Time frame to operate in
  analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255

341 342 343
  // ++++++++++++++++++++++++ Pump ++++++++++++++++++++++++
  pinMode(PIEZO_PIN, OUTPUT);

João Lino's avatar
João Lino committed
344 345
  // ++++++++++++++++++++++++ Temperature Sensor PT100 ++++++++++++++++++++++++
  //basePT100.setup();
346
/*
João Lino's avatar
João Lino committed
347 348 349
  analogReference  (INTERNAL1V1);          // EXTERNAL && INTERNAL2V56 && INTERNAL1V1
  pinMode    (PT100_OUTPUT_PIN, OUTPUT);  // setup temperature sensor input pin
  digitalWrite    (PT100_OUTPUT_PIN, LOW);    // initialize sensor off
350
*/
João Lino's avatar
João Lino committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  // ++++++++++++++++++++++++ Serial Monitor ++++++++++++++++++++++++
  Serial.begin                    (SETTING_SERIAL_MONITOR_BAUD_RATE);    // setup terminal baud rate
  Serial.println                  (SETTING_SERIAL_MONITOR_WELCOME_MESSAGE);  // print a start message to the terminal

  // ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
  lcd.begin                       (LCD_HORIZONTAL_RESOLUTION,LCD_VERTICAL_RESOLUTION);    //  <<----- My LCD was 16x2
  lcd.setBacklightPin             (LCD_BACKLIGHT_PIN,POSITIVE);        // Setup backlight pin
  lcd.setBacklight                (HIGH);              // Switch on the backlight

  // ######################### INITIALIZE #########################
  // ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
  // set operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
  xSetupRotaryEncoder             ( eRotaryEncoderMode_Disabled, 0, 0, 0, 0, 0 );

  // ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
  presetsMenuOption           =   ePresetsMenu_Trigo;
  maltMenuOption              =   eMaltMenu_CastleMalting_Chteau_Pilsen_2RS;
  settingsMenuOption          =   eSettingsMenu_PT100_Element;
  cookingStage                =   eCookingStage_Startpoint;
  // ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
  cooking                     =   false;
372
  bStageFirstRun              =   true;
373

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

João Lino's avatar
João Lino committed
386
  startpointTime              =   3600; //120;
João Lino's avatar
João Lino committed
387 388 389 390 391 392 393 394 395 396 397
  betaGlucanaseTime           =   0;
  debranchingTime             =   0;
  proteolyticTime             =   0;
  betaAmylaseTime             =   3600;
  alphaAmylaseTime            =   1800;
  mashoutTime                 =   300;
  recirculationTime           =   1200;
  spargeTime                  =   1200;
  boilTime                    =   5400;
  coolingTime                 =   120;

398
  startpointTemperature       =   50; //30;
João Lino's avatar
João Lino committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  betaGlucanaseTemperature    =   40;
  debranchingTemperature      =   40;
  proteolyticTemperature      =   50;
  betaAmylaseTemperature      =   60;
  alphaAmylaseTemperature     =   70;
  mashoutTemperature          =   80;
  recirculationTemperature    =   80;
  spargeTemperature           =   80;
  boilTemperature             =   100;
  coolingTemperature          =   25;

  refresh                     =   true;
  repaint                     =   true;

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

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

// ######################### Code - Run Once #########################
João Lino's avatar
João Lino committed
420 421 422
  xSafeHardwarePowerOff           ();
  displayWelcome                  ();
  
423
  xSetupRotaryEncoder             ( eRotaryEncoderMode_Menu, eMainMenu_GO, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
424
}
425

426
// ######################### MAIN LOOP #########################
427 428

void loop() {
429 430
  unsigned long inactivityTime = millis() - lastInterruptTime;

João Lino's avatar
João Lino committed
431
  if(inactivityTime > SETTING_MAX_INACTIVITY_TIME) {    // Inactivity check
432 433 434 435 436 437 438 439 440 441 442 443 444
    if(refresh) {
      repaint = true;
      refresh = false;
    }
    displayStatus();
  }
  else {
    displayMainMenu();
  }
  
  operateMachine();
}

445 446
// ######################### FUNCTIONS ########################

447
void xPaintStatusTemplate() {
João Lino's avatar
João Lino committed
448 449
  // Clear LCD
  lcd.clear();        
450

João Lino's avatar
João Lino committed
451 452
  // Position the cursor at the begining of where the temperature template goes onto the screen
  lcd.home();    
453

João Lino's avatar
João Lino committed
454 455
  // Print the target and measured temperature template
  if(cooking) {
João Lino's avatar
João Lino committed
456
    lcd.print("ON  XX 000.0/000C");
João Lino's avatar
João Lino committed
457 458
  }
  else {
João Lino's avatar
João Lino committed
459
    lcd.print("OFF XX 000.0/000C");
João Lino's avatar
João Lino committed
460
  }
461

João Lino's avatar
João Lino committed
462 463
  // Position the cursor at the begining of where the mode and time template goes onto the screen
  lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
464

João Lino's avatar
João Lino committed
465
  lcd.print("****       00:00");
466 467 468 469 470 471
}

void displayStatus() {

  // Check whether a template repaint is required
  if(repaint) {
João Lino's avatar
João Lino committed
472 473
    // Repaint the LCD template
    xPaintStatusTemplate();
474 475 476 477
    
    // Reset the repaint flag after the repaint has been done
    repaint = false;
  }
João Lino's avatar
João Lino committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  
  double displayTemperature = 0.0;
  unsigned long ulTimeToShow = millis() % 6000;

  lcd.setCursor (4,0);
  if(ulTimeToShow < 2000) {
    displayTemperature = basePT100.getCurrentTemperature();

    lcd.print("TS");
  }
  else {
    if(ulTimeToShow < 4000) {
      displayTemperature = upPT100.getCurrentTemperature();

      lcd.print("UP");
    }
    else {
      displayTemperature = downPT100.getCurrentTemperature();

      lcd.print("DW");
    }
  }
500

501
  // Print positions with no numbers, before the measured temperature value
João Lino's avatar
João Lino committed
502 503
  lcd.setCursor (7,0);
  if (displayTemperature < 10) {
504 505 506
    lcd.print("  ");
  }
  else {
João Lino's avatar
João Lino committed
507
    if (displayTemperature < 100) {
508 509 510 511 512
      lcd.print(" ");
    }
  }

  // Print measured temperature value onto the LCD
João Lino's avatar
João Lino committed
513
  lcd.print(displayTemperature, 1);
514 515

  // Print positions with no numbers, before the target temperature value
João Lino's avatar
João Lino committed
516
  lcd.setCursor (13,0);
517 518 519 520 521 522 523 524 525 526 527 528 529
  if (cookTemperature < 10) {
    lcd.print("  ");
  }
  else {
    if (cookTemperature < 100) {
      lcd.print(" ");
    }
  }

  // Print target temperature value onto the LCD
  lcd.print(cookTemperature);
  
  // Calculate the numbers on the timer clock
João Lino's avatar
João Lino committed
530 531
  unsigned long minutes = clockCounter / 1000 / 60;
  unsigned long seconds = (clockCounter / 1000) % 60;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558

  // Position the cursor at the begining of where the timer goes onto the screen
  lcd.setCursor (10, 1);
  
  // Print the timer values onto the LCD
  if (minutes < 10) {
    lcd.print(" 0");
  }
  else {
    if (minutes < 100) {
      lcd.print(" ");
    }
  }
  lcd.print(minutes);
  lcd.print(":");
  if(seconds<10) {
    lcd.print("0");
  }
  lcd.print(seconds);
}

void displayMainMenu() {
  switch(menu_position[0]) {
    case eMainMenu_GO: {
      MainMenu_GO();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
559 560
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
561
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_GO, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
562 563 564
      
      break;
    }
565 566 567 568 569 570 571 572 573 574
    case eMainMenu_STOP: {
      MainMenu_STOP();
      
      menu_position[0] = -1;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_GO, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
      
      break;
    }
575 576 577 578
    case eMainMenu_Presets: {
      MainMenu_Presets();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
579 580
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
581
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Presets, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
582 583 584
      
      break;
    }
585 586 587 588
    case eMainMenu_Malt: {
      MainMenu_Malt();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
589 590
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
591
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Malt, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
592 593 594
      
      break;
    }
595 596 597 598
    case eMainMenu_Startpoint: {
      MainMenu_Startpoint();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
599 600
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
601
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Startpoint, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
602 603 604
      
      break;
    }
605 606 607 608
    case eMainMenu_BetaGlucanase: {
      MainMenu_BetaGlucanase();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
609 610
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
611
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_BetaGlucanase, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
service-config's avatar
Mixer  
service-config committed
612 613
      
      break;
614 615 616 617 618
    }
    case eMainMenu_Debranching: {
      MainMenu_Debranching();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
619 620
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
621
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Settings, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
622 623 624
      
      break;
    }
625 626 627 628
    case eMainMenu_Proteolytic: {
      MainMenu_Proteolytic();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
629 630
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
631
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Proteolytic, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
632 633
      
      break;
634
    }
635 636 637 638
    case eMainMenu_BetaAmylase: {
      MainMenu_BetaAmylase();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
639 640
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
641
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_BetaAmylase, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
642
      
643 644
      break;
    }
645 646 647 648
    case eMainMenu_AlphaAmylase: {
      MainMenu_AlphaAmylase();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
649 650
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
651
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_AlphaAmylase, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
652
      
653 654
      break;
    }
655 656 657 658
    case eMainMenu_Mashout: {
      MainMenu_Mashout();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
659 660
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
661
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Mashout, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
662 663 664 665 666 667 668
      
      break;
    }
    case eMainMenu_Recirculation: {
      MainMenu_Recirculation();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
669 670
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
671
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Recirculation, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
672
      
673 674 675 676
      break;
    }
    case eMainMenu_Sparge: {
      MainMenu_Sparge();
677
      
678
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
679 680
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
681
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Sparge, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
682 683 684 685 686 687 688
      
      break;
    }
    case eMainMenu_Boil: {
      MainMenu_Boil();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
689 690
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
691
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Boil, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
692 693 694 695 696 697 698
      
      break;
    }
    case eMainMenu_Hops: {
      MainMenu_Hops();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
699 700
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
701
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Hops, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
702 703 704 705 706 707 708
      
      break;
    }
    case eMainMenu_Cooling: {
      MainMenu_Cooling();
      
      menu_position[0] = -1;
João Lino's avatar
João Lino committed
709 710
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
711
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Cooling, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
712 713 714
      
      break;
    }
715 716 717 718 719 720 721 722 723 724
    case eMainMenu_Purge: {
      MainMenu_Purge();

      menu_position[0] = -1;
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Purge, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
      
      break;
    }
725 726 727 728
    case eMainMenu_Settings: {
      MainMenu_Settings();
      
      menu_position[0] = -1;
729 730
      
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
731
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Settings, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
732 733 734 735 736
      
      break;
    }
    case eMainMenu_Back: {
      MainMenu_Back();
737
        
738
      menu_position[0] = -1;
739
        
740
      // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
741 742
      xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_Back, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );

743 744 745 746 747 748 749 750 751 752
      break;
    }
    default: {
      if(repaint) {

        // display menu
        lcd.clear();
        lcd.home (); // go home
        lcd.print("Brewery Menu");
        
service-config's avatar
Mixer  
service-config committed
753
        switch(rotaryEncoderVirtualPosition) {
754 755 756 757 758
          case eMainMenu_GO: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> GO           ");
            break;
          }
759 760 761 762 763
          case eMainMenu_STOP: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> STOP         ");
            break;
          }
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
          case eMainMenu_Presets: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Presets      ");
            break;
          }
          case eMainMenu_Malt: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Malt         ");
            break;
          }
          case eMainMenu_Startpoint: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Startpoint   ");
            break;
          }
          case eMainMenu_BetaGlucanase: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> BetaGlucanase");
            break;
          }
          case eMainMenu_Debranching: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Debranching  ");
            break;
          }
          case eMainMenu_Proteolytic: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Proteolytic  ");
            break;
          }
          case eMainMenu_BetaAmylase: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Beta Amylase ");
            break;
          }
          case eMainMenu_AlphaAmylase: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Alpha Amylase");
            break;
          }
          case eMainMenu_Mashout: {
805
            lcd.setCursor (0,1);        // go to start of 2nd line
806
            lcd.print("-> Mashout      ");
807 808
            break;
          }
809
          case eMainMenu_Recirculation: {
810
            lcd.setCursor (0,1);        // go to start of 2nd line
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
            lcd.print("-> Recirculation");
            break;
          }
          case eMainMenu_Sparge: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Sparge       ");
            break;
          }
          case eMainMenu_Boil: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Boil         ");
            break;
          }
          case eMainMenu_Hops: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Hops         ");
            break;
          }
          case eMainMenu_Cooling: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Cooling      ");
            break;
          }
834 835 836 837 838
          case eMainMenu_Purge: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Purge        ");
            break;
          }
839 840 841
          case eMainMenu_Settings: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Settings     ");
842 843
            break;
          }
844 845 846 847 848
          case eMainMenu_Back: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Back         ");
            break;
          }
849
          default: {
850
            // reset operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
851
            xSetupRotaryEncoder( eRotaryEncoderMode_Menu, eMainMenu_GO, MENU_SIZE_MAIN_MENU - 1, 0, 1, 0 );
852 853 854
          } 
        }
        
855 856 857
        repaint = false;
      }
      
João Lino's avatar
João Lino committed
858
      if ((digitalRead(ROTARY_ENCODER_SW_PIN))) {    // check if pushbutton is pressed
859
        menu_position[0] = rotaryEncoderVirtualPosition;
João Lino's avatar
João Lino committed
860 861
        while (digitalRead(ROTARY_ENCODER_SW_PIN)) {}    // wait til switch is released
        delay(10);                            // debounce
862
        break;
863 864 865 866 867
      }
    } 
  }
}

868
void MainMenu_GO() {
João Lino's avatar
João Lino committed
869
  startBrewing();
870

871 872 873 874 875 876 877 878
  xTransitionIntoStage_GlobalVariables( eCookingStage_Startpoint );

  backToStatus();
}

void MainMenu_STOP() {
  stopBrewing();

879 880 881 882
  backToStatus();
}

void MainMenu_Presets() {
883 884

  backToStatus();
João Lino's avatar
João Lino committed
885
  
886 887 888
}

void MainMenu_Malt() {
889 890

  backToStatus();
João Lino's avatar
João Lino committed
891
  
892 893 894
}

void MainMenu_Startpoint() {
João Lino's avatar
João Lino committed
895 896 897
  startpointTime = getTimer( startpointTime );
  
  startpointTemperature = xSetGenericValue( startpointTemperature, 0, 120, "temperature", "*C" );
898 899

  backToStatus();
900 901 902
}

void MainMenu_BetaGlucanase() {
João Lino's avatar
João Lino committed
903 904 905
  betaGlucanaseTime = getTimer( betaGlucanaseTime );
  
  betaGlucanaseTemperature = xSetGenericValue( betaGlucanaseTemperature, 0, 120, "temperature", "*C" );
906 907

  backToStatus();
908 909 910
}

void MainMenu_Debranching() {
João Lino's avatar
João Lino committed
911 912 913
  debranchingTime = getTimer( debranchingTime );
  
  debranchingTemperature = xSetGenericValue( debranchingTemperature, 0, 120, "temperature", "*C" );
914 915

  backToStatus();
916 917 918
}

void MainMenu_Proteolytic() {
João Lino's avatar
João Lino committed
919 920 921
  proteolyticTime = getTimer( proteolyticTime );
  
  proteolyticTemperature = xSetGenericValue( proteolyticTemperature, 0, 120, "temperature", "*C" );
922 923

  backToStatus();
924 925 926
}

void MainMenu_BetaAmylase() {
João Lino's avatar
João Lino committed
927 928 929
  betaAmylaseTime = getTimer( betaAmylaseTime );
  
  betaAmylaseTemperature = xSetGenericValue( betaAmylaseTemperature, 0, 120, "temperature", "*C" );
930 931

  backToStatus();
932 933 934
}

void MainMenu_AlphaAmylase() {
João Lino's avatar
João Lino committed
935 936 937
  alphaAmylaseTime = getTimer( alphaAmylaseTime );
  
  alphaAmylaseTemperature = xSetGenericValue( alphaAmylaseTemperature, 0, 120, "temperature", "*C" );
938 939

  backToStatus();
940 941 942
}

void MainMenu_Mashout() {
João Lino's avatar
João Lino committed
943 944 945
  mashoutTime = getTimer( mashoutTime );
  
  mashoutTemperature = xSetGenericValue( mashoutTemperature, 0, 120, "temperature", "*C" );
946 947

  backToStatus();
948 949 950
}

void MainMenu_Recirculation() {
João Lino's avatar
João Lino committed
951 952 953
  recirculationTime = getTimer( recirculationTime );
  
  recirculationTemperature = xSetGenericValue( recirculationTemperature, 0, 120, "temperature", "*C" );
954 955

  backToStatus();
956 957 958
}

void MainMenu_Sparge() {
João Lino's avatar
João Lino committed
959 960 961
  spargeTime = getTimer( spargeTime );
  
  spargeTemperature = xSetGenericValue( spargeTemperature, 0, 120, "temperature", "*C" );
962 963

  backToStatus();
964 965 966
}

void MainMenu_Boil() {
João Lino's avatar
João Lino committed
967 968 969
  boilTime = getTimer( boilTime );
  
  boilTemperature = xSetGenericValue( boilTemperature, 0, 120, "temperature", "*C" );
970 971

  backToStatus();
972 973 974
}

void MainMenu_Hops() {
975 976

  backToStatus();
João Lino's avatar
João Lino committed
977
  
978 979
}

980
void MainMenu_Cooling() {
João Lino's avatar
João Lino committed
981 982 983
  coolingTime = getTimer( coolingTime );
  
  coolingTemperature = xSetGenericValue( coolingTemperature, 0, 120, "temperature", "*C" );
984 985

  backToStatus();
986 987
}

988 989 990 991 992 993 994 995 996 997 998
void MainMenu_Purge() {
  // Stop anything that might be still going on
  xSafeHardwarePowerOff();

  // Start at the Purge stage
  startBrewing();
  xTransitionIntoStage_GlobalVariables( eCookingStage_Purge );

  backToStatus();
}

999
void MainMenu_Settings() {
1000
  iPumpSpeed = xSetGenericValue( iPumpSpeed, 0, 255, "Pump Speed", "PWM" );
1001 1002

  backToStatus();
1003 1004
}

1005 1006 1007
void MainMenu_Back() {
  backToStatus();
}
1008 1009

void xCountTheTime( int temperatureRange ) {
1010
  unsigned long now = millis();
João Lino's avatar
João Lino committed
1011

João Lino's avatar
João Lino committed
1012 1013
  // Check if the machine is in the right temperature range, for the current mode,
  if(!(basePT100.getCurrentTemperature() > (cookTemperature - temperatureRange) && basePT100.getCurrentTemperature() < (cookTemperature + temperatureRange))) {
João Lino's avatar
João Lino committed
1014
    clockIgnore += now - clockStartTime - clockIgnore;
João Lino's avatar
João Lino committed
1015 1016 1017
  }
  
  // Calculate the remaining time on the clock
João Lino's avatar
João Lino committed
1018
  clockCounter = cookTime * 1000 - (now - clockStartTime - clockIgnore);
João Lino's avatar
João Lino committed
1019

1020
#ifdef DEBUG_OFF
1021 1022
  debugPrintFunction("xCountTheTime");
  debugPrintVar("millis()", now);
João Lino's avatar
João Lino committed
1023
  debugPrintVar("cookTime", cookTime);
1024 1025 1026
  debugPrintVar("clockStartTime", clockStartTime);
  debugPrintVar("clockIgnore", clockIgnore);
  debugPrintVar("clockCounter", clockCounter); 
1027
#endif
1028 1029 1030
}

bool isTimeLeft() {
1031 1032
  if( clockCounter > 0 ) {
    return true;
João Lino's avatar
João Lino committed
1033
  }
1034
  return false;
1035 1036
}

1037
//HEATING_ELEMENT_MAX_WATTAGE / HEATING_ELEMENT_AC_FREQUENCY_HZ
1038 1039
double ulWattToWindowTime( double ulAppliedWatts ) {
  double ulPulsesRequired = ulAppliedWatts / dWattPerPulse;
1040
  return (double)iWindowSize / 1000.0 * ulPulsesRequired * 1000.0 / HEATING_ELEMENT_AC_FREQUENCY_HZ;
1041 1042 1043
}

bool xRegulateTemperature() {
1044
  double difference = cookTemperature - basePT100.getCurrentTemperature();
João Lino's avatar
João Lino committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  bool overTemperature = false;
  double wattage = 0.0;
  
  // 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 {
1059
    if(difference <= 0.1) {
João Lino's avatar
João Lino committed
1060 1061 1062
      // turn it off
      wattage = 0.0;
    } else {
1063
      if(difference <= 1) {
João Lino's avatar
João Lino committed
1064 1065 1066
        // pulse lightly at 500 watt
        wattage = 500.0;
      } else {
1067
        if(difference <= 3) {
João Lino's avatar
João Lino committed
1068 1069 1070
          // pulse moderately at 1000 watt
          wattage = 1000.0;
        } else {
1071
          if(difference <= 6) {
João Lino's avatar
João Lino committed
1072
            // pulse hardly at 2000 watt
1073
            wattage = 2000.0;
João Lino's avatar
João Lino committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
          } else {
            //pulse constantly at HEATING_ELEMENT_MAX_WATTAGE watt
            wattage = HEATING_ELEMENT_MAX_WATTAGE;
          }
        }
      }
    }
  }
  
  // Update the recorded time for the begining of the window, if the previous window has passed
1084 1085
  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;
1086
  }
João Lino's avatar
João Lino committed
1087 1088
  
  // Apply wattage to the element at the right time
1089
  if( ulWattToWindowTime( wattage ) > (millis() - windowStartTime) ) {
1090
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN,HIGH);
João Lino's avatar
João Lino committed
1091 1092 1093
  } else {
    digitalWrite(HEATING_ELEMENT_OUTPUT_PIN,LOW);
  }
1094

João Lino's avatar
João Lino committed
1095
#ifdef DEBUG_OFF
1096
  //debugPrintFunction("xRegulateTemperature");
1097
  debugPrintVar("difference", difference);
1098
  //debugPrintVar("overTemperature", overTemperature);
1099
  debugPrintVar("wattage", wattage);
1100 1101 1102 1103
  //debugPrintVar("ulWattToWindowTime( wattage )", ulWattToWindowTime( wattage ) );
  //debugPrintVar("millis()", millis());
  //debugPrintVar("windowStartTime", windowStartTime);
  //debugPrintVar("test", ulWattToWindowTime( wattage ) > (millis() - windowStartTime) ); 
1104
#endif
1105 1106
}

1107 1108
bool xRegulatePumpSpeed() {
  analogWrite(PUMP_PIN, iPumpSpeed);  // analogWrite values from 0 to 255
1109 1110 1111

  if( iPumpSpeed ) {
    //basePT100.setPower( PT100_BASE_DARLINGTON_ADC_VMAX, PT100_BASE_DARLINGTON_VS );
1112
    basePT100.setMeasuredTemperatureDeviation( -0.90 );
1113
    upPT100.setMeasuredTemperatureDeviation( -1.00 );
1114
    downPT100.setMeasuredTemperatureDeviation( -1.10 );
1115 1116 1117 1118 1119 1120 1121
  }
  else {
    //basePT100.setPower( PT100_BASE_DEFAULT_ADC_VMAX, PT100_BASE_DEFAULT_VS );
    basePT100.setMeasuredTemperatureDeviation( 0.0 );
    upPT100.setMeasuredTemperatureDeviation( 0.0 );
    downPT100.setMeasuredTemperatureDeviation( 0.0 );
  }
1122 1123
}

1124
void xWarnClockEnded() {
1125
  sing(MELODY_SUPER_MARIO_START, PIEZO_PIN);
1126 1127
}

João Lino's avatar
João Lino committed
1128 1129 1130 1131 1132
void xStageFirstRun( int stageTime, int stageTemperature, int stagePumpSpeed, eCookingStages stage ) {
  // Set Stage
  bStageFirstRun = true;
  cookingStage = stage;

João Lino's avatar
João Lino committed
1133 1134 1135 1136 1137 1138 1139 1140 1141
  // Set the clock
  cookTime = stageTime;
  
  // Set the target temperature
  cookTemperature = stageTemperature;
  
  // Reset the clock
  clockStartTime = millis();
  clockIgnore = 0;
1142 1143 1144

  // Set the pump speed
  iPumpSpeed = stagePumpSpeed;
1145 1146 1147
}

void xTransitionIntoStage_GlobalVariables(eCookingStages nextStage) {
João Lino's avatar
João Lino committed
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

  // Operate the machine according to the current mode
  switch(nextStage) {
    case eCookingStage_Startpoint: {
      // A basic operation for a basic stage
      xStageFirstRun( startpointTime, startpointTemperature, 255, eCookingStage_Startpoint );
      
      break;
    }
    case eCookingStage_BetaGlucanase: {
      // A basic operation for a basic stage
      xStageFirstRun( betaGlucanaseTime, betaGlucanaseTemperature, 255, eCookingStage_BetaGlucanase );
      
      break;
    }
    case eCookingStage_Debranching: {
      // A basic operation for a basic stage
      xStageFirstRun( debranchingTime, debranchingTemperature, 255, eCookingStage_Debranching );
      
      break;
    }
    case eCookingStage_Proteolytic: {
      // A basic operation for a basic stage
      xStageFirstRun( proteolyticTime, proteolyticTemperature, 255, eCookingStage_Proteolytic );
      
      break;
    }
    case eCookingStage_BetaAmylase: {
      // A basic operation for a basic stage
      xStageFirstRun( betaAmylaseTime, betaAmylaseTemperature, 255, eCookingStage_BetaAmylase );
      
      break;
    }
    case eCookingStage_AlphaAmylase: {
      // A basic operation for a basic stage
      xStageFirstRun( alphaAmylaseTime, alphaAmylaseTemperature, 255, eCookingStage_AlphaAmylase );
      
      break;
    }
    case eCookingStage_Mashout: {
      // A basic operation for a basic stage
      xStageFirstRun( mashoutTime, mashoutTemperature, 255, eCookingStage_Mashout );
      
      break;
    }
    case eCookingStage_Recirculation: {
      // A basic operation for a basic stage
      xStageFirstRun( recirculationTime, recirculationTemperature, 255, eCookingStage_Recirculation );
      
      break;
    }
    case eCookingStage_Sparge: {
      // A basic operation for a basic stage
      xStageFirstRun( spargeTime, spargeTemperature, 255, eCookingStage_Sparge );
      
      break;
    }
    case eCookingStage_Boil: {
      // A basic operation for a basic stage
      xStageFirstRun( boilTime, boilTemperature, 255, eCookingStage_Boil );
      
      break;
    }
    case eCookingStage_Cooling: {
      // A basic operation for a basic stage
      xStageFirstRun( coolingTime, coolingTemperature, 255, eCookingStage_Cooling );

      break;
    }
    case eCookingStage_Purge: {
      // A basic operation for a basic stage
      xStageFirstRun( 0, 0, 255, eCookingStage_Purge );

      xRegulatePumpSpeed();

      break;
    }
    case eCookingStage_Done: {
      // A basic operation for a basic stage
      xStageFirstRun( 0, 0, 0, eCookingStage_Done );

      break;
    }
    default : {
      // A basic operation for a basic stage
      xStageFirstRun( 0, 0, 0, eCookingStage_Done );
    }
  }
1236 1237 1238
}

void xTransitionIntoStage(eCookingStages nextStage) {
João Lino's avatar
João Lino committed
1239 1240 1241 1242 1243 1244 1245 1246 1247
  
  // 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
  xTransitionIntoStage_GlobalVariables( nextStage );
1248 1249 1250
}

void xBasicStageOperation( int iStageTime, int iStageTemperature, int iStageTemperatureRange, eCookingStages nextStage ) {
João Lino's avatar
João Lino committed
1251 1252 1253
  if(bStageFirstRun) {
    // Don't run this again
    bStageFirstRun = false;
João Lino's avatar
João Lino committed
1254 1255

    //xStageFirstRun( iStageTime, iStageTemperature, 255 );
João Lino's avatar
João Lino committed
1256 1257 1258 1259
    
    // When the stage should be skipped
    if( iStageTime == 0) {
      // Continue to the next stage
João Lino's avatar
João Lino committed
1260
      //xTransitionIntoStage_GlobalVariables( nextStage );
João Lino's avatar
João Lino committed
1261 1262 1263 1264 1265
      
      // There is nothing to do, in this stage
      return;
    } else {
      // Set the clock, target temperature and Reset the clock
1266
      //xStageFirstRun( iStageTime, iStageTemperature, PUMP_SPEED_SLOW );
João Lino's avatar
João Lino committed
1267
      //xStageFirstRun( iStageTime, iStageTemperature, 255 );
João Lino's avatar
João Lino committed
1268 1269 1270 1271 1272 1273 1274 1275
    }
  } else {
    // Account for time spent at the target temperature | Input 1: range in ºC within which the target temperature is considered to be reached
    xCountTheTime( iStageTemperatureRange );
    
    if( isTimeLeft() ) {
      // Do temperature control
      xRegulateTemperature();
1276 1277 1278

      // Do flow control
      xRegulatePumpSpeed();
João Lino's avatar
João Lino committed
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
      
    } else {
      // Continue to the next stage
      xTransitionIntoStage( nextStage );
      
      // There is nothing to do, in this stage
      return;
    }
  }
  // There is nothing to do, in this iteration
  return;
1290 1291 1292
}

void xWarnCookEnded() {
1293
  sing(MELODY_UNDERWORLD, PIEZO_PIN);
1294 1295 1296 1297 1298
}

void operateMachine() {

  // Measure temperature, for effect
1299 1300
  //analogWrite(6, 0);
  //delay(1);
1301
  basePT100.measure();
João Lino's avatar
João Lino committed
1302 1303
  upPT100.measure();
  downPT100.measure();
1304
  Serial.println(" ");
1305
  //analogWrite(6, iPumpSpeed);
1306 1307 1308

  // 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
1309
    xSafeHardwarePowerOff();
1310

1311 1312
    return;
  }
João Lino's avatar
João Lino committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
  
  // Operate the machine according to the current mode
  switch(cookingStage) {
    case eCookingStage_Startpoint: {
      // A basic operation for a basic stage
      xBasicStageOperation( startpointTime, startpointTemperature, 1, eCookingStage_BetaGlucanase );
      
      break;
    }
    case eCookingStage_BetaGlucanase: {
      // A basic operation for a basic stage
      xBasicStageOperation( betaGlucanaseTime, betaGlucanaseTemperature, 1, eCookingStage_Debranching );
      
      break;
    }
    case eCookingStage_Debranching: {
      // A basic operation for a basic stage
      xBasicStageOperation( debranchingTime, debranchingTemperature, 1, eCookingStage_Proteolytic );
      
      break;
    }
    case eCookingStage_Proteolytic: {
      // A basic operation for a basic stage
      xBasicStageOperation( proteolyticTime, proteolyticTemperature, 1, eCookingStage_BetaAmylase );
      
      break;
    }
    case eCookingStage_BetaAmylase: {
      // A basic operation for a basic stage
      xBasicStageOperation( betaAmylaseTime, betaAmylaseTemperature, 1, eCookingStage_AlphaAmylase );
      
      break;
    }
    case eCookingStage_AlphaAmylase: {
      // A basic operation for a basic stage
      xBasicStageOperation( alphaAmylaseTime, alphaAmylaseTemperature, 1, eCookingStage_Mashout );
      
      break;
    }
    case eCookingStage_Mashout: {
      // A basic operation for a basic stage
      xBasicStageOperation( mashoutTime, mashoutTemperature, 1, eCookingStage_Recirculation );
      
      break;
    }
    case eCookingStage_Recirculation: {
      // A basic operation for a basic stage
      xBasicStageOperation( recirculationTime, recirculationTemperature, 1, eCookingStage_Sparge );
      
      break;
    }
    case eCookingStage_Sparge: {
      // A basic operation for a basic stage
      xBasicStageOperation( spargeTime, spargeTemperature, 1, eCookingStage_Boil );
      
      break;
    }
    case eCookingStage_Boil: {
      // A basic operation for a basic stage
      xBasicStageOperation( boilTime, boilTemperature, 1, eCookingStage_Cooling );
      
      break;
    }
    case eCookingStage_Cooling: {
      // A basic operation for a basic stage
      xBasicStageOperation( coolingTime, coolingTemperature, 1, eCookingStage_Done );
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

      break;
    }
    case eCookingStage_Purge: {
      // A basic operation for a basic stage
      //xBasicStageOperation( coolingTime, coolingTemperature, 1, eCookingStage_Done );
      iPumpSpeed = PUMP_SPEED_MAX;

      xRegulatePumpSpeed();

João Lino's avatar
João Lino committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
      break;
    }
    default : {
      // Update state
      cooking = false;
      
      // Warn the user that the cooking is done
      xWarnCookEnded();
    }
  }
1399 1400 1401 1402 1403
}

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

void startBrewing() {
1404 1405
  //sing(MELODY_SUPER_MARIO, PIEZO_PIN);

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  cooking = true;
}

void stopBrewing() {
  cooking = false;
}

void backToStatus() {
  lastInterruptTime = millis() - SETTING_MAX_INACTIVITY_TIME - 1;
}
// #################################################### Helpers ##################################################################

// #################################################### Set Variables ##################################################################
1419
int getTimer(int init) {
João Lino's avatar
João Lino committed
1420 1421
  // set operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
  xSetupRotaryEncoder( eRotaryEncoderMode_Time, init, 7200, 0, 1, 30 );
1422 1423

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1424
  int rotaryEncoderPreviousPosition = 0;
1425 1426 1427 1428 1429 1430 1431
  int minutes = 0;
  int seconds = 0;
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Time");
1432
  lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1433 1434 1435
  lcd.print("      0:00");
  
  while(true) {
1436
    // Check if pushbutton is pressed
João Lino's avatar
João Lino committed
1437
    if ((digitalRead(ROTARY_ENCODER_SW_PIN))) {  
1438
      // Wait until switch is released
João Lino's avatar
João Lino committed
1439
      while (digitalRead(ROTARY_ENCODER_SW_PIN)) {}  
1440
      
João Lino's avatar
João Lino committed
1441 1442
      // debounce
      delay(10);
1443

João Lino's avatar
João Lino committed
1444
      // Job is done, break the circle
1445
      break;
1446
    } else {
João Lino's avatar
João Lino committed
1447 1448 1449
      // Don't forget to keep an eye on the cooking
      operateMachine();
    }
1450 1451
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
1452 1453 1454 1455
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      minutes = rotaryEncoderVirtualPosition/60;
      seconds = rotaryEncoderVirtualPosition-minutes*60;
1456
      
1457
      lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
      lcd.print("      ");
      lcd.print(minutes);
      lcd.print(":");
      if(seconds<10) {
        lcd.print("0");
      }
      lcd.print(seconds);
      lcd.println("                ");
    }
  }
  
service-config's avatar
Mixer  
service-config committed
1469
  return rotaryEncoderVirtualPosition;
1470 1471 1472 1473 1474
}

int getTemperature(int init) {
  
  // set operation state
1475
  rotaryEncoderMode = eRotaryEncoderMode_Generic;
service-config's avatar
Mixer  
service-config committed
1476
  rotaryEncoderVirtualPosition = init;  
1477 1478

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1479
  int rotaryEncoderPreviousPosition = 0;
1480 1481 1482 1483 1484
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Temperature");
1485
  lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1486 1487
  lcd.print("       0 *C");
  
1488
  rotaryEncoderMaxPosition = TEMPERATURE_SETTING_MAX_VALUE;
service-config's avatar
Mixer  
service-config committed
1489
  
1490
  while(true) {
1491
    // Check if pushbutton is pressed
João Lino's avatar
João Lino committed
1492
    if ((digitalRead(ROTARY_ENCODER_SW_PIN))) {  
1493
      // Wait until switch is released
João Lino's avatar
João Lino committed
1494
      while (digitalRead(ROTARY_ENCODER_SW_PIN)) {}  
1495
      
João Lino's avatar
João Lino committed
1496 1497
      // debounce
      delay(10);
1498

João Lino's avatar
João Lino committed
1499
      // Job is done, break the circle
1500
      break;
1501
    } else {
João Lino's avatar
João Lino committed
1502 1503 1504
      // Don't forget to keep an eye on the cooking
      operateMachine();
    }
1505 1506
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
1507 1508
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
1509
      
1510
      lcd.setCursor (0,LCD_VERTICAL_RESOLUTION-1);
1511
      lcd.print("     ");
service-config's avatar
Mixer  
service-config committed
1512
      if(rotaryEncoderVirtualPosition<10) {
1513 1514 1515
        lcd.print("  ");
      }
      else {
service-config's avatar
Mixer  
service-config committed
1516
        if(rotaryEncoderVirtualPosition<100) {
1517 1518 1519
          lcd.print(" ");
        }
      }
service-config's avatar
Mixer  
service-config committed
1520
      lcd.print(rotaryEncoderVirtualPosition);
1521 1522 1523 1524 1525
      lcd.print(" *C");
      lcd.println("                ");
    }
  }
  
service-config's avatar
Mixer  
service-config committed
1526
  return rotaryEncoderVirtualPosition;
1527 1528
}

João Lino's avatar
João Lino committed
1529 1530 1531
int xSetGenericValue(int init, int min, int max, char *valueName, char *unit) {  
  // set operation state | INPUT : eRotaryEncoderMode newMode, int newPosition, int newMaxPosition, int newMinPosition, int newSingleStep, int newMultiStep
  xSetupRotaryEncoder( eRotaryEncoderMode_Generic, init, max, min, 1, 5 );
1532 1533

  // initialize variables
service-config's avatar
Mixer  
service-config committed
1534
  int rotaryEncoderPreviousPosition = 0;
1535 1536 1537
  
  // Setup Screen
  lcd.clear();
1538 1539 1540 1541 1542
  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
1543
  lcd.print( unit );
1544
  
1545
  while(true) {
1546 1547 1548
    // 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
1549
      while ( digitalRead(ROTARY_ENCODER_SW_PIN) ) {}  
1550
      
João Lino's avatar
João Lino committed
1551 1552
      // debounce
      delay( 10 );
service-config's avatar
Mixer  
service-config committed
1553

João Lino's avatar
João Lino committed
1554
      // Job is done, break the circle
service-config's avatar
Mixer  
service-config committed
1555
      break;
1556
    } else {
João Lino's avatar
João Lino committed
1557 1558 1559
      // Don't forget to keep an eye on the cooking
      operateMachine();
    }
service-config's avatar
Mixer  
service-config committed
1560
    
1561 1562
    // Check if there was an update by the rotary encoder
    if( rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition ) {
service-config's avatar
Mixer  
service-config committed
1563 1564
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      
1565 1566 1567 1568
      lcd.setCursor( 0, LCD_VERTICAL_RESOLUTION - 1 );
      lcd.print( "     " );
      if( rotaryEncoderVirtualPosition < 10 ) {
        lcd.print( "  " );
1569 1570
      }
      else {
1571 1572
        if( rotaryEncoderVirtualPosition < 100 ) {
          lcd.print( " " );
1573 1574
        }
      }
1575
      lcd.print( rotaryEncoderVirtualPosition );
1576 1577
      lcd.print( " " );
      lcd.print( unit );
1578
      lcd.println( "                " );
1579 1580
    }
  }
1581 1582
  
  return rotaryEncoderVirtualPosition;
1583 1584
}

1585
// ###################### Set Variables ##################################################
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596

void lcdPrint(String title, String message) {
  int messageLength = message.length();
  
  lcd.clear();
    
  // print title
  lcd.home();
  lcd.print(title);
    
  // print message
1597 1598
  if(messageLength <= LCD_HORIZONTAL_RESOLUTION) {
    lcd.setCursor(0,LCD_VERTICAL_RESOLUTION-1);
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    lcd.print(message);
    delay(1000);
  }
  // print scrolling message
  else {
    String output_message = "                ";
    output_message += message;
    messageLength = output_message.length();
    
    // Adjust the message size for proper printing
    if ( messageLength & 1 == 1 ) {
      output_message+=" ";
      messageLength+=2;
    }
    
    // print scrolling message
1615 1616
    for (int cursor = 0; cursor < messageLength - LCD_HORIZONTAL_RESOLUTION; cursor+=2) {
      lcd.setCursor(0,LCD_VERTICAL_RESOLUTION-1);
1617 1618 1619 1620 1621 1622
      lcd.print(output_message.substring(cursor, cursor+16));
      delay(500);
    }
  }
}

1623