Skip to content

Instantly share code, notes, and snippets.

@soemarko
Created June 22, 2020 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soemarko/98448f19e5851e245d81ab7386f9c15f to your computer and use it in GitHub Desktop.
Save soemarko/98448f19e5851e245d81ab7386f9c15f to your computer and use it in GitHub Desktop.
Arduino powered Rice-cooker Sous Vide
#include <AutoPID.h>
#include <LiquidCrystal.h>
#include <max6675.h>
double currentTemp = 0.0;
double targetTemp = 60.0;
bool powerState = LOW;
//!MARK: LCD setup
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int prev_btn = btnNONE;
//!MARK: Thermocouple setup
int thermo_gnd_pin = 13;
int thermo_vcc_pin = 12;
int thermo_so_pin = 11;
int thermo_cs_pin = 3;
int thermo_sck_pin = 2;
MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);
//!MARK: PID setup
unsigned long previousMillis = 0;
const long thermo_interval = 800;
int relay_pin = 19;
bool relay_state = LOW;
#define PULSEWIDTH 2500
double kP = 0.3;
double kI = 0.001;
double kD = kI / 2;
AutoPIDRelay myPID(&currentTemp, &targetTemp, &relay_state, PULSEWIDTH, kP, kI, kD);
//!MARK: LCD methods
int read_LCD_buttons() {
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE;
// For V1.0 comment the other threshold and use the one below:
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void updateScreen() {
lcd.setCursor(0,0); // move cursor to second line "1" and 9 spaces over
lcd.print("Current: ");
lcd.print(currentTemp);
lcd.print(char(223));
lcd.print("C ");
lcd.setCursor(0,1); // move to the begining of the second line
if(powerState) {
lcd.print("Target : ");
lcd.print(targetTemp);
lcd.print(char(223));
lcd.print("C ");
}
else {
lcd.print(" OFF :(");
lcd.print(targetTemp);
lcd.print(") ");
}
lcd_key = read_LCD_buttons(); // read the buttons
if(lcd_key == btnNONE && prev_btn != btnNONE) {
switch (prev_btn) { // action based on released button
case btnRIGHT:
targetTemp += 5;
break;
case btnLEFT:
targetTemp -= 5;
break;
case btnUP:
targetTemp += 0.5;
break;
case btnDOWN:
targetTemp -= 0.5;
break;
case btnSELECT:
powerState = !powerState;
break;
case btnNONE:
break;
}
}
prev_btn = lcd_key;
}
//!MARK: Arduino
void setup() {
Serial.begin(9600);
myPID.setBangBang(4);
myPID.setTimeStep(4000);
lcd.begin(16, 2);
lcd.setCursor(2,0);
lcd.print("Sous Viduino");
lcd.setCursor(11,1);
lcd.print("MK2");
pinMode(relay_pin, OUTPUT);
digitalWrite(relay_pin, relay_state);
pinMode(thermo_vcc_pin, OUTPUT);
pinMode(thermo_gnd_pin, OUTPUT);
digitalWrite(thermo_vcc_pin, HIGH);
digitalWrite(thermo_gnd_pin, LOW);
// wait for MAX chip to stabilize
delay(PULSEWIDTH);
lcd.clear();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= thermo_interval) {
previousMillis = currentMillis;
currentTemp = thermocouple.readCelsius() - 3;
}
updateScreen();
if(powerState) {
myPID.run();
digitalWrite(relay_pin, relay_state);
}
else {
myPID.stop();
relay_state = LOW;
digitalWrite(relay_pin, relay_state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment