Hi, I am working on a similar but simpler project, with two DS18B20 sensors both in the mash wort. I am using gas, not electricity. Would you like to share the sketch?
I'd be happy to share it. As I said, I'm pretty inexperienced, so use it at your own risk!
I still have some debug lines in here. I haven't taken any time to clean things up yet.
/*
* Mash temperature controller V1.0
* By Paul Aplikowski
* Designed for a RIMS system.
*
*/
//Various Libraries to include
#include <ClickEncoder.h>
#include <TimerOne.h>
#include <OneWire.h> //temp probe communication
#include <DallasTemperature.h> //temp probe communication
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//LCD Setup
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define LCD_RS 0
#define LCD_RW 1
#define LCD_EN 2
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
#define LCD_CHARS 20
#define LCD_LINES 4
//Pin Definitions
// Note that encoder pins are set at creation of ClickEncoder below
#define ONE_WIRE_BUS 2 //data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_PWR 3
#define ONE_WIRE_GND 4
#define PUMP_SWITCH 8 //the switch to control the pump
#define PUMP_POWER 9 //the relay to control the pump (HIGH means off)
#define ELEMENT_POWER 7 //the SSR to control the element
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); //pass our oneWire reference to Dallas Temperature
LiquidCrystal_I2C lcd(I2C_ADDR,LCD_EN, LCD_RW, LCD_RS, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
//Variables
ClickEncoder *encoder;
int16_t last, setP = 125;
float rTemp;
float mTemp;
bool pumpState = HIGH; //setting to turn pump on and off
String pumpText = "OFF";
bool demandHeat = LOW; //flag to determine if heating is in demand
String demandHeatText = "REST";
bool elementArmed = LOW; //setting to arm heating element
String elementArmedText = "ARMED";
void timerIsr() {
encoder->service();
}
//pump button variables
int reading = 0;
int buttonState;
int lastButtonState;
long lastDebounceTime = 0;
long debounceDelay = 50;
/*void displayAccelerationStatus() {
lcd.setCursor(0, 1);
lcd.print("Acceleration ");
lcd.print(encoder->getAccelerationEnabled() ? "on " : "off");
}*/
void setup() {
//temp probe setup
sensors.begin();
pinMode(ONE_WIRE_PWR, OUTPUT);
digitalWrite(ONE_WIRE_PWR, HIGH);
pinMode(ONE_WIRE_GND, OUTPUT);
digitalWrite(ONE_WIRE_GND, LOW);
pinMode(ONE_WIRE_BUS, INPUT);
//initialize the pump switch and relay pin
pinMode(PUMP_SWITCH, INPUT);
pinMode(PUMP_POWER, OUTPUT);
digitalWrite(PUMP_POWER, HIGH); //assumes sainsmart type relay where HIGH means off
//initialize the element pin
pinMode(ELEMENT_POWER, OUTPUT);
digitalWrite(ELEMENT_POWER, LOW);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
Serial.begin(9600);
encoder = new ClickEncoder(A1, A0, A2);
//startup screen
lcd.begin(LCD_CHARS, LCD_LINES);
lcd.clear();
lcd.print(" RedShirt Brewing");
lcd.setCursor(0,1);
lcd.print(" RIMS Mash Temp");
lcd.setCursor(0,2);
lcd.print(" Controller");
lcd.setCursor(0,3);
lcd.print(" v1.0");
delay(3000);
screenInitialize();
Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
last = -1;
sensors.requestTemperatures(); //request temperatures early in order to initialize the starting temperature
}
void loop() {
//check rotary encoder and set setpoint
setP += encoder->getValue();
if (setP != last) {
last = setP;
Serial.print("Setpoint: ");
Serial.println(setP);
}
//Update temperatures
sensors.requestTemperatures();
rTemp = sensors.getTempCByIndex(0) * 1.8 + 32;
mTemp = sensors.getTempCByIndex(1) * 1.8 + 32;
//Serial.print(rTemp);
//Serial.print(mTemp);
//check the pump switch
reading = digitalRead(PUMP_SWITCH);
//
if(reading != lastButtonState){
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay){
if (reading != buttonState){
buttonState = reading;
//only toggle the pump if the new button state is HIGH
if(buttonState == HIGH){
pumpState = !pumpState;
}
}
}
lastButtonState = reading;
digitalWrite(PUMP_POWER, pumpState);
//check the encoder button
ClickEncoder::Button b = encoder->getButton();
if (b != ClickEncoder::Open) {
if (ClickEncoder:
ressed){
if (elementArmed == LOW)
elementArmed = HIGH;
else if (elementArmed == HIGH)
elementArmed =LOW;
}
}
screenDraw();
controlSystems();
}
/* int SetPoint(){
Serial.print("Held!");
ClickEncoder::Button b = encoder->getButton();
while(b != ClickEncoder::Held){
//Serial.print("ClickEncoder");
value += encoder->getValue();
if (value != last) {
last = value;
Serial.print("Encoder Value: ");
Serial.println(value);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(value);
}
}
} */
void screenInitialize(){
lcd.clear();
lcd.print("Stat: Pump:");
lcd.setCursor(0,1);
lcd.print(" :Setpoint");
lcd.setCursor(0,2);
lcd.print(" :RIMS Temp");
lcd.setCursor(0,3);
lcd.print(" :Mash Temp");
}
void screenDraw(){
if (demandHeat == LOW)
demandHeatText = "REST";
else demandHeatText = "HEAT";
if (pumpState == HIGH) pumpText = "OFF";
else pumpText = " ON";
if (elementArmed == LOW) elementArmedText = " OFF";
else elementArmedText = "ARMED";
lcd.setCursor(5,0);
lcd.print(" ");
lcd.setCursor(5,0);
lcd.print(demandHeatText);
lcd.setCursor(17,0);
lcd.print(" ");
lcd.setCursor(17,0);
lcd.print(pumpText);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(15,1);
lcd.print(elementArmedText);
lcd.setCursor(0,1);
lcd.print(setP);
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(rTemp,1);
lcd.setCursor(0,3);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(mTemp,1);
}
/*int pumpToggle(int buttonPin){
//variables
int reading = 0;
int buttonState;
int lastButtonState;
long lastDebounceTime = 0;
long debounceDelay = 50;
reading = digitalRead(buttonPin);
//
if(reading != lastButtonState){
Serial.print(reading);
Serial.print(lastButtonState);
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay){
if (reading != buttonState){
buttonState = reading;
//only toggle the pump if the new button state is HIGH
if(buttonState == HIGH){
pumpState = !pumpState;
}
}
}
lastButtonState = reading;
//Serial.print(pumpState);
return pumpState;
}
*/
void controlSystems(){
if (pumpState == LOW)
digitalWrite(PUMP_POWER, LOW);
if (rTemp<setP && mTemp<setP && elementArmed && !pumpState)
demandHeat = HIGH;
else demandHeat = LOW;
digitalWrite(ELEMENT_POWER, demandHeat);
}