brew.ino 35 KB
Newer Older
1 2 3 4 5 6 7 8 9
// ######################### DECLARE #########################
// ++++++++++++++++++++++++ ERROR ++++++++++++++++++++++++
//#include "error.h"
// ++++++++++++++++++++++++ ERROR ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Library - DHT ++++++++++++++++++++++++
//#include "DHT.h"      //DHT humidity/temperature sensors
//#define DHTPIN 2        // What pin we're connected to
//#define DHTTYPE DHT11   // DHT 11 
#define ponto1 A4//
service-config's avatar
Mixer  
service-config committed
10 11 12 13 14 15
//#define ponto2 A5//
//#define R1 99.45
//#define R2 100.2
//#define R3 100.05
//#define calibragem 12.709066573

service-config's avatar
service-config committed
16
//float amostra1;//
17 18
float temperatura, temperatura1;//
float tabela[] = {100.00, 100.39, 100.78, 101.17, 101.56, 101.95, 102.34, 102.73, 103.12, 103.51, 103.90, 104.29, 104.68, 105.07, 105.46, 105.85, 106.24, 106.63, 107.02, 107.40, 107.79, 108.18, 108.57, 108.96, 109.35, 109.73, 110.12, 110.51, 110.90, 111.29, 111.67, 112.06, 112.45, 112.83, 113.22, 113.61, 114.00, 114.38, 114.77, 115.15, 115.54, 115.93, 116.31, 116.70, 117.08, 117.47, 117.86, 118.24, 118.63, 119.01, 119.40, 119.78, 120.17, 120.55, 120.94, 121.32, 121.71, 122.09, 122.47, 122.86, 123.24, 123.63, 124.01, 124.39, 124.78, 125.16, 125.54, 125.93, 126.31, 126.69, 127.08, 127.46, 127.84, 128.22, 128.61, 128.99, 129.37, 129.75, 130.13, 130.52, 130.90, 131.28, 131.66, 132.04, 132.42, 132.80, 133.18, 133.57, 133.95, 134.33, 134.71, 135.09, 135.47, 135.85, 136.23, 136.61, 136.99, 137.37, 137.75, 138.13, 138.51, 138.88, 139.26, 139.64, 140.02, 140.40, 140.78, 141.16, 141.54, 141.91, 142.29, 142.67, 143.05, 143.43, 143.80, 144.18, 144.56, 144.94, 145.31, 145.69, 146.07, 146.44, 146.82, 147.20, 147.57, 147.95, 148.33, 148.70, 149.08, 149.46, 149.83, 150.21, 150.58, 150.96, 151.33, 151.71, 152.08, 152.46, 152.83, 153.21, 153.58, 153.96, 154.33, 154.71, 155.08, 155.46, 155.83, 156.20, 156.58, 156.95, 157.33, 157.70, 158.07, 158.45, 158.82, 159.19, 159.56, 159.94, 160.31, 160.68, 150.00, 160.00, 161.05, 161.43, 161.80, 162.17, 162.54, 162.91, 163.29, 163.66, 164.03, 164.40, 164.77, 165.14, 165.51, 165.89, 166.26, 166.63, 167.00, 167.37, 167.74, 168.11, 168.48, 168.85, 169.22, 169.59, 169.96, 170.33, 170.70, 171.07, 171.43, 171.80, 172.17, 172.54, 172.91, 173.28, 173.65, 174.02, 174.38, 174.75, 175.12, 175.49, 175.86, 176.22, 176.59, 176.96, 177.33, 177.69, 178.06, 178.43, 178.79, 179.16};
service-config's avatar
Mixer  
service-config committed
19 20
//float amostras[] = {630.00,630.00,630.00}; //,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00,630.00};

service-config's avatar
service-config committed
21
float Vout;
service-config's avatar
Mixer  
service-config committed
22 23 24 25 26
float Rx;

/*
float amostra2;//
float diferenca;
27 28 29 30 31 32

float tensaoPorAmostra;
float Vg;
float Vs;
float a;
float Rx;
service-config's avatar
Mixer  
service-config committed
33
*/
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// ++++++++++++++++++++++++ Library - DHT ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define LCD_I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define LCD_X   16
#define LCD_Y   2
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
#include <PID_v1.h>
service-config's avatar
Mixer  
service-config committed
53
#define HEATING_ELEMENT  24
54
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
service-config's avatar
Mixer  
service-config committed
55
// ++++++++++++++++++++++++ Temperature ++++++++++++++++++++++++
service-config's avatar
service-config committed
56
#define TEMPERATURE_SENSOR_PIN 30
service-config's avatar
Mixer  
service-config committed
57 58 59 60 61 62
#define TEMPERATURE_MAX_POSITION 120
// ++++++++++++++++++++++++ Temperature ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
#define MIXER_PIN 12
#define MIXER_MAX_POSITION 255
// ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
#define SETTING_WELCOME_TIMEOUT  2000
#define MENU_MAX_DEPTH 10
#define MENU_INIT_VALUES -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
// ######################### DECLARE #########################


// ######################### INITIALIZE #########################
// ++++++++++++++++++++++++ Library - DHT ++++++++++++++++++++++++
//DHT dht(DHTPIN, DHTTYPE);
// ++++++++++++++++++++++++ Library - DHT ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
int n = 1;
LiquidCrystal_I2C	lcd(LCD_I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
service-config's avatar
Mixer  
service-config committed
80
volatile int  rotaryEncoderVirtualPosition = 0;
service-config's avatar
service-config committed
81
volatile int rotaryEncoderMaxPosition;
82 83 84 85 86 87 88 89 90 91 92 93 94
volatile int menuSize = 2;
const int PinCLK = 3;                   // Used for generating interrupts using CLK signal
const int PinDT = 22;                    // Used for reading DT signal
const int PinSW = 23;                    // Used for the push button switch
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
float Kp = 200;
float Ki = 50;
float Kd = 0;
service-config's avatar
Mixer  
service-config committed
95
//PID myPID(&Input, &Output, &Setpoint,Kp,Ki,Kd, DIRECT);
96
//PID myPID(&Input, &Output, &Setpoint,1000,50,0, DIRECT);
service-config's avatar
Mixer  
service-config committed
97
PID myPID(&Input, &Output, &Setpoint,200,50,0, DIRECT);
98

service-config's avatar
Mixer  
service-config committed
99
int WindowSize = 1000;
100 101
unsigned long windowStartTime;
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
service-config's avatar
Mixer  
service-config committed
102 103 104
// ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
//const int mixerSpeed = 130;
// ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
105 106 107 108 109 110 111 112 113 114
// ++++++++++++++++++++++++ Error ++++++++++++++++++++++++
enum sys_error {
  no_error,
  catastrofic_failure_sensor_temperature,
  catastrofic_failure_program,
  catastrofic_failure_logic_auto_manual
};
sys_error error = no_error;
// ++++++++++++++++++++++++ Error ++++++++++++++++++++++++
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
service-config's avatar
Mixer  
service-config committed
115

116 117 118 119 120 121 122
// global
enum state_machine {
  state_welcome,
  state_menu,
  state_manual_time,
  state_manual_temperature,
  state_manual_mode,
service-config's avatar
Mixer  
service-config committed
123
  state_manual_mixer,
124 125 126
  state_error
};
state_machine state = state_welcome;
service-config's avatar
Mixer  
service-config committed
127

128 129 130 131 132 133 134 135 136
// menu
enum main_menu_list {
  main_manual,
  main_auto
};
enum manual_menu_list {
  manual_temperature,
  manual_time,
  manual_mode,
service-config's avatar
service-config committed
137
  manual_mixer,
138 139 140 141 142 143 144 145 146
  manual_start,
  manual_back
};
enum auto_menu_list {
  auto_recipe,
  auto_start,
  auto_back
};
int menu_position[MENU_MAX_DEPTH]= {MENU_INIT_VALUES};
service-config's avatar
Mixer  
service-config committed
147

148 149 150 151 152
// cooking
enum cook_mode_list {
  quick_start,
  start_at_temperature
};
service-config's avatar
Mixer  
service-config committed
153 154
int cookTime = 3600;
int cookTemperature = 36;
155
cook_mode_list cookMode = quick_start;
service-config's avatar
service-config committed
156
int cookMixerSpeed = 180; //0; //130;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
// ++++++++++++++++++++++++ State Machine ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
//float currentTemperatureCelsius;
//float targetTemperatureCelsius = -1;
// ++++++++++++++++++++++++ Global Variables ++++++++++++++++++++++++
// ######################### INITIALIZE #########################

// ######################### INTERRUPTS #########################
static unsigned long lastInterruptTime = 0;
void isr ()  {                    // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
  
  unsigned long interruptTime = millis();    
  
  // If interrupts come faster than 5ms, assume it's a bounce and ignore
  if ((interruptTime - lastInterruptTime) > 5) {
    switch(state) {
service-config's avatar
Mixer  
service-config committed
173 174
      
      // Input of rotay encoder controling menus
175 176 177
      case state_manual_mode:
      case state_menu: {
        if (!digitalRead(PinDT)) {
service-config's avatar
Mixer  
service-config committed
178
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 1);
179 180
        }
        else {
service-config's avatar
Mixer  
service-config committed
181
            rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - 1;
182
        }
service-config's avatar
Mixer  
service-config committed
183 184
        if (rotaryEncoderVirtualPosition >= menuSize) {
            rotaryEncoderVirtualPosition = 0;
185
        }
service-config's avatar
Mixer  
service-config committed
186 187
        if (rotaryEncoderVirtualPosition < 0) {
            rotaryEncoderVirtualPosition = menuSize -1;
188 189 190 191
        }
        
        break;
      }
service-config's avatar
Mixer  
service-config committed
192 193
      
      // Input of rotay encoder controling time variables
194 195
      case state_manual_time: {
        if (!digitalRead(PinDT)) {
service-config's avatar
Mixer  
service-config committed
196 197
          if(rotaryEncoderVirtualPosition >= 60) {
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 30);
198 199
          }
          else {
service-config's avatar
Mixer  
service-config committed
200
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 1);
201 202 203
          }
        }
        else {
service-config's avatar
Mixer  
service-config committed
204 205
          if(rotaryEncoderVirtualPosition == 0) {
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 60);
206 207
          }
          else {
service-config's avatar
Mixer  
service-config committed
208 209
            if(rotaryEncoderVirtualPosition >= 90) {
              rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - 30);
210 211
            }
            else {
service-config's avatar
Mixer  
service-config committed
212
              rotaryEncoderVirtualPosition = rotaryEncoderVirtualPosition - 1;
213 214 215
            }
          }
        }
service-config's avatar
Mixer  
service-config committed
216 217
        if (rotaryEncoderVirtualPosition > 7200) {
            rotaryEncoderVirtualPosition = 7200;
218
        }
service-config's avatar
Mixer  
service-config committed
219 220
        if (rotaryEncoderVirtualPosition < 0) {
            rotaryEncoderVirtualPosition = 0;
221 222 223 224
        }
        
        break;
      }
service-config's avatar
Mixer  
service-config committed
225 226 227
      
      // Input of rotay encoder controling integer variables within a range between 0 and rotaryEncoderMaxPosition
      case state_manual_mixer:
228 229 230
      case state_manual_temperature: {
        if (!digitalRead(PinDT)) {
          if ((interruptTime - lastInterruptTime) < 10) {
service-config's avatar
Mixer  
service-config committed
231
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 5);
232 233
          }
          else {
service-config's avatar
Mixer  
service-config committed
234
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition + 1);
235 236 237 238
          }
        }
        else {
          if ((interruptTime - lastInterruptTime) < 10) {
service-config's avatar
Mixer  
service-config committed
239
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - 5);
240 241
          }
          else {
service-config's avatar
Mixer  
service-config committed
242
            rotaryEncoderVirtualPosition = (rotaryEncoderVirtualPosition - 1);
243 244
          }
        }
service-config's avatar
Mixer  
service-config committed
245 246
        if (rotaryEncoderVirtualPosition > rotaryEncoderMaxPosition) {
            rotaryEncoderVirtualPosition = rotaryEncoderMaxPosition;
247
        }
service-config's avatar
Mixer  
service-config committed
248 249 250 251 252 253
        if (rotaryEncoderVirtualPosition < 0) {
            rotaryEncoderVirtualPosition = 0;
        }
       
        break;
      }
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
      default: {
        
      }
    }
  }
  
  lastInterruptTime = interruptTime;
}
// ######################### INTERRUPTS #########################

// ######################### START #########################
void setup() {
// ++++++++++++++++++++++++ Library - DHT ++++++++++++++++++++++++
  //dht.begin();
// ++++++++++++++++++++++++ Library - DHT ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
  lcd.begin (LCD_X,LCD_Y); //  <<----- My LCD was 16x2
  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
// ++++++++++++++++++++++++ Library - LiquidCrystal_I2C ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
  pinMode(PinCLK,INPUT);
  pinMode(PinDT, INPUT);
  pinMode(PinSW, INPUT);
  attachInterrupt(1, isr, FALLING);   // interrupt 0 is always connected to pin 2 on Arduino UNO
// ++++++++++++++++++++++++ Rotary Encoder ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
  pinMode(HEATING_ELEMENT, OUTPUT);
  digitalWrite(HEATING_ELEMENT,LOW);
  
  windowStartTime = millis();

  //initialize the variables we're linked to
  Setpoint = 100;

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
// ++++++++++++++++++++++++ Heating Element Relay ++++++++++++++++++++++++
service-config's avatar
Mixer  
service-config committed
296
// ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
service-config's avatar
service-config committed
297 298
  pinMode(MIXER_PIN, OUTPUT);
  analogWrite(MIXER_PIN, 0);
service-config's avatar
Mixer  
service-config committed
299 300
// ++++++++++++++++++++++++ Mixer ++++++++++++++++++++++++
// ++++++++++++++++++++++++ Temperature Sensor PT100 ++++++++++++++++++++++++
service-config's avatar
service-config committed
301 302 303 304 305 306
//  analogReference(EXTERNAL);
  analogReference(INTERNAL1V1);
//  analogReference(INTERNAL2V56);

  pinMode(TEMPERATURE_SENSOR_PIN, OUTPUT);
  digitalWrite(TEMPERATURE_SENSOR_PIN, LOW);
service-config's avatar
Mixer  
service-config committed
307
// ++++++++++++++++++++++++ Temperature Sensor PT100 ++++++++++++++++++++++++
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  Serial.begin(9600); 
  Serial.println("Let's start Brewing!");
}
// ######################### START #########################

void loop() {
  switch(state) {
    case state_welcome: {
      runWelcome();
      
      break;
    }
    case state_menu: {
      runMenu();
      
      break;
    }
service-config's avatar
Mixer  
service-config committed
325
    /*case state_manual_time: {
326 327 328 329 330 331 332 333 334
      //runManualTime();
      
      break;
    }
    case state_manual_temperature: {
      //runManualTemp();
      
      break;
    }
service-config's avatar
Mixer  
service-config committed
335 336 337 338 339
    case state_manual_mixer: {
      //runManualTemp();
      
      break;
    }*/
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    case state_error: {
      runError();
      
      break;
    }
    default: {
      lcdPrint("Error", "The brewer has stoped working properly. Please shutdown NOW for your safety!");
      state = state_error;
      error = catastrofic_failure_program;
    }
  }
}

void runWelcome() {
  // Write welcome
  lcd.clear();
  lcd.home (); // go home
  lcd.print("  Let's start");
  lcd.setCursor (0,1);        // go to start of 2nd line
  lcd.print("    Brewing!");
  
  delay(SETTING_WELCOME_TIMEOUT);
  
  state = state_menu;
}

void runMenu() {
  digitalWrite(HEATING_ELEMENT,LOW);
  
  switch(menu_position[0]) {
    case main_manual: {
      switch(menu_position[1]) {
        case manual_temperature: {
          // do work
          cookTemperature = getTemperature(cookTemperature);
          
          menu_position[1] = -1;
service-config's avatar
Mixer  
service-config committed
377
          rotaryEncoderVirtualPosition = manual_temperature;
378 379 380 381 382 383 384 385
          
          break;
        }
        case manual_time: {
          // do work
          cookTime = getTimer(cookTime);
          
          menu_position[1] = -1;
service-config's avatar
Mixer  
service-config committed
386
          rotaryEncoderVirtualPosition = manual_time;
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
          
          break;
        }
        case manual_mode: {
          // do work
          int returnMode = getMode();
          switch(returnMode) {
            case quick_start: {
              cookMode = quick_start;
              break;
            }
            case start_at_temperature: {
              cookMode = start_at_temperature;
              
              break;
            }
            default: {
              cookMode = quick_start;
            }
          }
          
          menu_position[1] = -1;
service-config's avatar
Mixer  
service-config committed
409 410 411 412 413 414 415 416 417 418
          rotaryEncoderVirtualPosition = manual_mode;
          
          break;
        }
        case manual_mixer: {
          // do work
          cookMixerSpeed = getMixerSpeed(cookMixerSpeed);
          
          menu_position[1] = -1;
          rotaryEncoderVirtualPosition = manual_time;
419 420 421 422 423 424 425 426
          
          break;
        }
        case manual_start: {
          // do work
          runStart();
          
          menu_position[1] = -1;
service-config's avatar
Mixer  
service-config committed
427
          rotaryEncoderVirtualPosition = manual_start;
428 429 430 431 432 433 434
          
          break;
        }
        case manual_back: {
          menu_position[0] = -1;
          menu_position[1] = -1;
          
service-config's avatar
Mixer  
service-config committed
435
          rotaryEncoderVirtualPosition = manual_back;
436 437 438 439 440
          
          break;
        }
        default: {
          // reset menu variables
service-config's avatar
Mixer  
service-config committed
441
          menuSize = 6;
442 443 444 445 446 447 448
          
          // display menu
          lcd.clear();
          lcd.home (); // go home
          lcd.print("Manual Menu");
          
          while(true) {
service-config's avatar
Mixer  
service-config committed
449
            switch(rotaryEncoderVirtualPosition) {
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
              case manual_temperature: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Temperature  ");
                delay(100);
                break;
              }
              case manual_time: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Time         ");
                delay(100);
                break;
              }
              case manual_mode: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Mode         ");
                delay(100);
                break;
              }
service-config's avatar
Mixer  
service-config committed
468 469 470 471 472 473
              case manual_mixer: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Mixer Speed  ");
                delay(100);
                break;
              }
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
              case manual_start: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Start        ");
                delay(100);
                break;
              }
              case manual_back: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Back         ");
                delay(100);
                break;
              }
              default: {
                //lcd.setCursor (0,1);        // go to start of 2nd line
                //lcd.print("*** Error ***");
service-config's avatar
Mixer  
service-config committed
489
                rotaryEncoderVirtualPosition = 0;
490 491 492 493
              } 
            }
            
            if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
service-config's avatar
Mixer  
service-config committed
494
              menu_position[1] = rotaryEncoderVirtualPosition;
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
              while (!digitalRead(PinSW)) {}  // wait til switch is released
              delay(10);                      // debounce
              break;
            }
          }
        } 
      }
      break;
    }
    case main_auto: {
      switch(menu_position[1]) {
        case auto_recipe: {
          // do work
          
          menu_position[1] = -1;
service-config's avatar
Mixer  
service-config committed
510
          rotaryEncoderVirtualPosition = auto_recipe;
511 512 513 514 515 516 517
          
          break;
        }
        case auto_start: {
          // do work
          
          menu_position[1] = -1;
service-config's avatar
Mixer  
service-config committed
518
          rotaryEncoderVirtualPosition = auto_start;
519 520 521 522 523 524 525 526 527
          
          break;
        }
        case auto_back: {
          // reset choces
          menu_position[0] = -1;
          menu_position[1] = -1;
          
          // set menu position
service-config's avatar
Mixer  
service-config committed
528
          rotaryEncoderVirtualPosition = main_auto;
529 530 531 532 533
          
          break;
        }
        default: {
          // reset menu variables
service-config's avatar
Mixer  
service-config committed
534
          //rotaryEncoderVirtualPosition = 0;
535 536 537 538 539 540 541 542
          menuSize = 3;
          
          // display menu
          lcd.clear();
          lcd.home (); // go home
          lcd.print("Automatic Menu");
          
          while(true) {
service-config's avatar
Mixer  
service-config committed
543
            switch(rotaryEncoderVirtualPosition) {
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
              case auto_recipe: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Recipe       ");
                delay(100);
                break;
              }
              case auto_start: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Start        ");
                delay(100);
                break;
              }
              case auto_back: {
                lcd.setCursor (0,1);        // go to start of 2nd line
                lcd.print("-> Back         ");
                delay(100);
                break;
              }
              default: {
                //lcd.setCursor (0,1);        // go to start of 2nd line
                //lcd.print("*** Error ***");
service-config's avatar
Mixer  
service-config committed
565
                rotaryEncoderVirtualPosition = 0;
566 567 568 569
              } 
            }
            
            if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
service-config's avatar
Mixer  
service-config committed
570
              menu_position[1] = rotaryEncoderVirtualPosition;
571 572 573 574 575 576 577 578 579 580 581
              while (!digitalRead(PinSW)) {}  // wait til switch is released
              delay(10);                      // debounce
              break;
            }
          }
        } 
      }
      break;
    }
    default: {
      // reset menu variables
service-config's avatar
Mixer  
service-config committed
582
      //rotaryEncoderVirtualPosition = 0;
583 584 585 586 587 588 589 590
      menuSize = 2;
      
      // display menu
      lcd.clear();
      lcd.home (); // go home
      lcd.print("Main Menu");
      
      while(true) {
service-config's avatar
Mixer  
service-config committed
591
        switch(rotaryEncoderVirtualPosition) {
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
          case main_manual: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Manual       ");
            delay(100);
            break;
          }
          case main_auto: {
            lcd.setCursor (0,1);        // go to start of 2nd line
            lcd.print("-> Automatic    ");
            delay(100);
            break;
          }
          default: {
            //lcd.setCursor (0,1);        // go to start of 2nd line
            //lcd.print("*** Error ***");
service-config's avatar
Mixer  
service-config committed
607
            rotaryEncoderVirtualPosition = 0;
608 609 610 611
          } 
        }
        
        if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
service-config's avatar
Mixer  
service-config committed
612
          menu_position[0] = rotaryEncoderVirtualPosition;
613 614 615 616 617 618 619 620 621 622 623 624 625 626
          while (!digitalRead(PinSW)) {}  // wait til switch is released
          delay(10);                      // debounce
          break;
        }
      }
    } 
  }
}

int getTimer(int init) {
  state_machine initialState = state;
  
  // set operation state
  state = state_manual_time;
service-config's avatar
Mixer  
service-config committed
627
  rotaryEncoderVirtualPosition = init;
628 629

  // initialize variables
service-config's avatar
Mixer  
service-config committed
630
  int rotaryEncoderPreviousPosition = 0;
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  int minutes = 0;
  int seconds = 0;
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Time");
  lcd.setCursor (0,LCD_Y-1);
  lcd.print("      0:00");
  
  while(true) {
    // Check for timer set
    if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
      while (!digitalRead(PinSW)) {}  // wait til switch is released
      delay(10);                      // debounce
      break;
    }
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
650 651 652 653
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      minutes = rotaryEncoderVirtualPosition/60;
      seconds = rotaryEncoderVirtualPosition-minutes*60;
654 655 656 657 658 659 660 661 662 663 664 665 666 667
      
      lcd.setCursor (0,LCD_Y-1);
      lcd.print("      ");
      lcd.print(minutes);
      lcd.print(":");
      if(seconds<10) {
        lcd.print("0");
      }
      lcd.print(seconds);
      lcd.println("                ");
    }
  }
  
  state = initialState;
service-config's avatar
Mixer  
service-config committed
668
  return rotaryEncoderVirtualPosition;
669 670 671 672 673 674 675
}

int getTemperature(int init) {
  state_machine initialState = state;
  
  // set operation state
  state = state_manual_temperature;
service-config's avatar
Mixer  
service-config committed
676
  rotaryEncoderVirtualPosition = init;  
677 678

  // initialize variables
service-config's avatar
Mixer  
service-config committed
679
  int rotaryEncoderPreviousPosition = 0;
680 681 682 683 684 685 686 687
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Temperature");
  lcd.setCursor (0,LCD_Y-1);
  lcd.print("       0 *C");
  
service-config's avatar
Mixer  
service-config committed
688 689
  rotaryEncoderMaxPosition = TEMPERATURE_MAX_POSITION;
  
690 691 692 693 694 695 696 697 698
  while(true) {
    // Check for timer set
    if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
      while (!digitalRead(PinSW)) {}  // wait til switch is released
      delay(10);                      // debounce
      break;
    }
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
699 700
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
701 702 703
      
      lcd.setCursor (0,LCD_Y-1);
      lcd.print("     ");
service-config's avatar
Mixer  
service-config committed
704
      if(rotaryEncoderVirtualPosition<10) {
705 706 707
        lcd.print("  ");
      }
      else {
service-config's avatar
Mixer  
service-config committed
708
        if(rotaryEncoderVirtualPosition<100) {
709 710 711
          lcd.print(" ");
        }
      }
service-config's avatar
Mixer  
service-config committed
712
      lcd.print(rotaryEncoderVirtualPosition);
713 714 715 716 717 718
      lcd.print(" *C");
      lcd.println("                ");
    }
  }
  
  state = initialState;
service-config's avatar
Mixer  
service-config committed
719
  return rotaryEncoderVirtualPosition;
720 721 722 723 724 725 726 727 728
}

int getMode() {
  state_machine initialState = state;
  int initialMenuSize = menuSize;
  
  // set operation state
  state = state_manual_mode;
  menuSize = 2;
service-config's avatar
Mixer  
service-config committed
729
  rotaryEncoderVirtualPosition = cookMode;  
730 731

  // initialize variables
service-config's avatar
Mixer  
service-config committed
732
  int rotaryEncoderPreviousPosition = 0;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Mode");
  lcd.setCursor (0,LCD_Y-1);
  lcd.print(" Quick Start");
  
  while(true) {
    // Check for timer set
    if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
      while (!digitalRead(PinSW)) {}  // wait til switch is released
      delay(10);                      // debounce
      break;
    }
    
    // display current timer
service-config's avatar
Mixer  
service-config committed
750 751
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
752 753 754
      
      lcd.setCursor (0,LCD_Y-1);
      
service-config's avatar
Mixer  
service-config committed
755
      switch(rotaryEncoderVirtualPosition) {
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
        case quick_start: {
          lcd.print("  Quick Start   ");
          
          break;
        }
        case start_at_temperature: {
          lcd.print("Start After Temp");
          
          break;
        }
        default: {
          lcd.print("*** error ***");
        }
      }
      lcd.println("                ");
    }
  }
  
  state = initialState;
  menuSize = initialMenuSize;
service-config's avatar
Mixer  
service-config committed
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 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
  return rotaryEncoderVirtualPosition;
}

int getMixerSpeed(int init) {
  state_machine initialState = state;
  
  // set operation state
  state = state_manual_mixer;
  rotaryEncoderVirtualPosition = init;

  // initialize variables
  int rotaryEncoderPreviousPosition = 0;
  int mixerSpeed = 0;
  
  // Setup Screen
  lcd.clear();
  lcd.home();        
  lcd.print("Set Mixer Speed");
  lcd.setCursor (0,LCD_Y-1);
  lcd.print("       000");
  
  rotaryEncoderMaxPosition = MIXER_MAX_POSITION;
  
  while(true) {
    // Check for timer set
    if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
      while (!digitalRead(PinSW)) {}  // wait til switch is released
      delay(10);                      // debounce
      break;
    }
    
    // display current timer
    if (rotaryEncoderVirtualPosition != rotaryEncoderPreviousPosition) {
      rotaryEncoderPreviousPosition = rotaryEncoderVirtualPosition;
      
      lcd.setCursor (0,LCD_Y-1);
      lcd.print("      ");
      //lcd.print(minutes);
      //lcd.print(":");
      if(rotaryEncoderVirtualPosition<100) {
        lcd.print("0");
        if(rotaryEncoderVirtualPosition<10) {
          lcd.print("0");
        }
      }
      lcd.print(rotaryEncoderVirtualPosition);
      lcd.println("                ");
service-config's avatar
service-config committed
823 824 825
      
      // DEBUG
      analogWrite(MIXER_PIN, rotaryEncoderVirtualPosition);
service-config's avatar
Mixer  
service-config committed
826 827 828 829 830
    }
  }
  
  state = initialState;
  return rotaryEncoderVirtualPosition;
831 832 833
}

void runStart() {
service-config's avatar
Mixer  
service-config committed
834 835 836
  
  // Working environment variables
  float runCurrentTemperature;
837 838 839 840
  long runTargetTemperature;
  unsigned long runStartTime;
  unsigned long runTargetTime;
  cook_mode_list runCurrentMode;
service-config's avatar
service-config committed
841
  unsigned long runMixerSpeed;
service-config's avatar
Mixer  
service-config committed
842 843
  
  // Configure environment
844 845 846
  boolean clockStart = false;
  boolean clockEnd = false;
  
service-config's avatar
Mixer  
service-config committed
847
  // Iterative work
848
  while(true) {
service-config's avatar
Mixer  
service-config committed
849
    
850 851 852
    // Build Instructions
    switch(menu_position[0]) {
      case main_manual: {
service-config's avatar
Mixer  
service-config committed
853 854
        
        // whenever changes are made to the cook variables, they must be applied to the running variables
855 856 857
        runTargetTemperature = cookTemperature;
        runTargetTime = cookTime;
        runCurrentMode = cookMode;
service-config's avatar
Mixer  
service-config committed
858
        runMixerSpeed = cookMixerSpeed;
859
        
service-config's avatar
Mixer  
service-config committed
860
        // check if time ended and is time to stop and return to menu
861
        if(clockStart && clockEnd) {
service-config's avatar
service-config committed
862 863
          
          // Turn mixer OFF for safety
service-config's avatar
service-config committed
864
          analogWrite(MIXER_PIN, 0);
service-config's avatar
service-config committed
865 866 867 868 869
          
          // Turn heading element OFF for safety
          digitalWrite(HEATING_ELEMENT,LOW);
          
          // Pause for efect
870
          delay(1000);
service-config's avatar
service-config committed
871
          
872 873 874 875 876 877
          return;
        }
        
        break;
      }
      case main_auto: {
service-config's avatar
Mixer  
service-config committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
        // get the next instruction
        
        // if it's time for a new instruction
        
          // if human interaction is required
          
            // display message
            
            // sound buzzer
            
            // check if rotary encoder press to continue
        
          // check what variables are afected
          
          // check what the new values are
          
          // apply the values
        
        // if it's NOT time for a new instruction
           //delay(1000);
898 899 900 901 902 903 904 905 906 907 908
        
        break;
      }
      default: {
        lcdPrint("Error", "The system has failed to be set into manual or automatic mode!");
        state = state_error;
        error = catastrofic_failure_logic_auto_manual;
        return;
      } 
    }
    
service-config's avatar
Mixer  
service-config committed
909 910
    // ************ Run Instructions ************
    
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    // Setup LCD
    lcd.clear();
    lcd.home();
    lcd.print("Hot: 000*C/000*C");
    //lcd.print("Cool:000*C/000*C");
    lcd.setCursor (0,LCD_Y-1);
    if(runCurrentMode == quick_start) {
      lcd.print("Quick      00:00");
      
      clockStart = true;
      runStartTime = millis();
    }
    else {
      lcd.print("Wait       00:00");
    }
    
service-config's avatar
service-config committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    float amostra1 = -1;
    float amostraAnterior = -1;
    unsigned long lastTemperatureRead = millis();
    
    while(true) {
      
      if(millis() - lastTemperatureRead >= 1000) {
        amostraAnterior = amostra1;
        digitalWrite(TEMPERATURE_SENSOR_PIN, HIGH);
        //delay(20);
        amostra1 = analogRead(ponto1);
        digitalWrite(TEMPERATURE_SENSOR_PIN, LOW);
        lastTemperatureRead = millis();
      }
      
      float Vs = 1.081; //4.9345;
      float Vin = 4.87; //8.8345;
service-config's avatar
Mixer  
service-config committed
944
      float adcStepCount = 1024.0;
service-config's avatar
service-config committed
945 946 947
      float R1 = 606.0;
      float Rl = 2.7; //2.9;
      float R_OpTemp = 0; //19.7; //21.597; //24.37;
948
      
service-config's avatar
service-config committed
949 950 951
      //Vout = amostra1 * Vs / adcStepCount;// + 0.0079816667;
      Vout = (amostra1+amostraAnterior) / 2 * Vs / adcStepCount;// + 0.0079816667;
      //Rx = 151.5 * Vout / ( 11.01 - Vout ) - 30;
service-config's avatar
Mixer  
service-config committed
952
      
service-config's avatar
service-config committed
953
      Rx = R1 / ( Vin / Vout - 1.0) - Rl - R_OpTemp;
service-config's avatar
Mixer  
service-config committed
954
      
service-config's avatar
service-config committed
955
      runCurrentTemperature = 1.08271 * pow(10.0, -13.0) * (3.12508 * pow(10.0, 16.0) - 5.65566 * pow(10.0, 6.0) * sqrt(3.51501 * pow(10.0, 19.0) - 4.61805 * pow(10.0, 16.0) * Rx));
956
      
service-config's avatar
Mixer  
service-config committed
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
      /*
      Serial.print("O valor das entradas sao: [");
      Serial.print(amostra1);
      Serial.print(", ");
      Serial.print(amostra2);
      Serial.print("]");
      Serial.print("diferenca: [");
      Serial.print(diferenca, 6);
      Serial.print("] tensao por amortra: [");
      Serial.print(tensaoPorAmostra, 6);
      Serial.print("] Vg: [");
      Serial.print(Vg, 6);
      Serial.print("] Vs: [");
      Serial.print(Vs);
      Serial.print("] a: [");
      Serial.print(a, 6);
      Serial.print("] Rx: [");
      Serial.print(Rx, 6);
      */
976
      
service-config's avatar
service-config committed
977
      // Operate mixer
service-config's avatar
service-config committed
978
      analogWrite(MIXER_PIN, runMixerSpeed);
service-config's avatar
service-config committed
979
      
980 981 982 983
      // Operate the heating element
      Input = runCurrentTemperature;
      Setpoint = runTargetTemperature;
      myPID.Compute();
service-config's avatar
Mixer  
service-config committed
984 985
      
      // Get time and use as "date" of the sensor readings
986 987
      unsigned long now = millis();
      
service-config's avatar
Mixer  
service-config committed
988
      // Check if it's time to vary the pulse width modulation and if so do it by shifting the "Relay in ON" Window
989
      if(now - windowStartTime>WindowSize)
service-config's avatar
Mixer  
service-config committed
990
      { 
991 992 993
        windowStartTime += WindowSize;
      }
      
service-config's avatar
Mixer  
service-config committed
994
      // Check if the "date" is inside the "Relay ON" window and turn it ON; if not, turn it OFF
995 996 997 998
      if(Output > now - windowStartTime) {
        digitalWrite(HEATING_ELEMENT,HIGH);
       
        if(Output != 0) {
service-config's avatar
Mixer  
service-config committed
999 1000
          Serial.print("O valor das entradas sao: [");
          Serial.print(amostra1);
service-config's avatar
service-config committed
1001 1002
          Serial.print("] Vout[");
          Serial.print(Vout,6);
service-config's avatar
Mixer  
service-config committed
1003 1004 1005 1006 1007 1008
          Serial.print("] Rx[");
          Serial.print(Rx,6);
          Serial.print("] Temperature[");
          Serial.print(runCurrentTemperature,6);
          Serial.print("] ");
          
1009 1010 1011 1012
          Serial.print("+++  Diff: ");
          Serial.print(now - windowStartTime);
          Serial.print(" | Output: ");
          Serial.println(Output);
service-config's avatar
Mixer  
service-config committed
1013
          
1014 1015 1016 1017 1018 1019
        }
      }
      else {
        digitalWrite(HEATING_ELEMENT,LOW);
       
        if(Output != 0) {
service-config's avatar
Mixer  
service-config committed
1020 1021
          Serial.print("O valor das entradas sao: [");
          Serial.print(amostra1);
service-config's avatar
service-config committed
1022 1023
          Serial.print("] Vout[");
          Serial.print(Vout,6);
service-config's avatar
Mixer  
service-config committed
1024 1025 1026 1027 1028 1029
          Serial.print("] Rx[");
          Serial.print(Rx,6);
          Serial.print("] Temperature[");
          Serial.print(runCurrentTemperature,6);
          Serial.print("] ");
          
1030 1031 1032 1033
          Serial.print("---  Diff: ");
          Serial.print(now - windowStartTime);
          Serial.print(" | Output: ");
          Serial.println(Output);
service-config's avatar
Mixer  
service-config committed
1034 1035 1036 1037 1038
          
        }
        else {
          Serial.print("O valor das entradas sao: [");
          Serial.print(amostra1);
service-config's avatar
service-config committed
1039 1040
          Serial.print("] Vout[");
          Serial.print(Vout,6);
service-config's avatar
Mixer  
service-config committed
1041 1042 1043 1044 1045
          Serial.print("] Rx[");
          Serial.print(Rx,6);
          Serial.print("] Temperature[");
          Serial.print(runCurrentTemperature,6);
          Serial.println("]"); 
1046 1047 1048 1049
        }
      }
      
      // Print status to LCD
service-config's avatar
service-config committed
1050 1051 1052 1053
      //if(runCurrentTemperature > 0) {
        lcd.setCursor (5,0);
        if (runCurrentTemperature < 10) {
          lcd.print("  ");
1054
        }
service-config's avatar
service-config committed
1055 1056 1057 1058
        else {
          if (runCurrentTemperature < 100) {
            lcd.print(" ");
          }
1059
        }
service-config's avatar
service-config committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
        lcd.print(runCurrentTemperature, 1);
        
        lcd.setCursor (11,0);
        if (runTargetTemperature < 10) {
          lcd.print("  ");
        }
        else {
          if (runTargetTemperature < 100) {
            lcd.print(" ");
          }
        }
        lcd.print(runTargetTemperature);
      //}
1073
      
service-config's avatar
Mixer  
service-config committed
1074
      // Check if the target temperature has been reached so the clock can move forward.
1075 1076 1077 1078
      if(runCurrentTemperature == runTargetTemperature && clockStart == false) {
        clockStart = true;
        runStartTime = now;
      }
service-config's avatar
Mixer  
service-config committed
1079 1080
      
      // If the clock can move forward, calculate it's display time; if not, set it to the total cooking time
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
      int displayTime = 0;
      if(clockStart) {
        displayTime = runTargetTime - ((now - runStartTime) / 1000);
        
        if(displayTime <= 0) {
          displayTime = 0;
          clockEnd = true;
          
          break;
        }
      }
      else {
        displayTime = runTargetTime;
      }
service-config's avatar
Mixer  
service-config committed
1095 1096
      
      // Print the clock to the LCD
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 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
      int minutes = displayTime/60;
      int seconds = displayTime-minutes*60;
      lcd.setCursor (10,1);
      //lcd.print("      ");
      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 runError() {
  if(error != no_error) {
    if (error == catastrofic_failure_sensor_temperature)
    {
      Serial.println("[Error] catastrofic_failure_sensor_temperature!");
      
      lcdPrint("ERROR", "A catastrofic failure was detected on the temperature sensor!");
      /*
      if (!isnan(dht.readTemperature())) {  // Check if any reads failed and exit early (to try again).
        error = no_error;
        return;
      }
      */
    }
  }
  else {
    state = state_welcome;
  }
}

void lcdPrint(String title, String message) {
  int messageLength = message.length();
  
  lcd.clear();
    
  // print title
  lcd.home();
  lcd.print(title);
    
  // print message
  if(messageLength <= LCD_X) {
    lcd.setCursor(0,LCD_Y-1);
    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
    for (int cursor = 0; cursor < messageLength - LCD_X; cursor+=2) {
      lcd.setCursor(0,LCD_Y-1);
      lcd.print(output_message.substring(cursor, cursor+16));
      delay(500);
    }
  }
}