Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Code accompanying my blog post on my kegerator setup, found here:
// http://www.dingostick.com/2018/12/kegerator-v10.html
// 1. https://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>
// 2. https://github.com/milesburton/Arduino-Temperature-Control-Library
#include <DallasTemperature.h>
#define LOGGING
// INPUT
const int tempSensor = A3;
OneWire oneWire(tempSensor);
DallasTemperature sensors(&oneWire);
// OUTPUT
// PWM-capable pins for the LEDs
const int blueLed = 8,
greenLed = 9,
redLed = 10;
const int relay = A0;
// SETTINGS
// Stout should be nearer 50°
const float targetTemperatureF = 48.0;
const float upperTempLimitF = targetTemperatureF + 3.0; // Above this temp, relay turns on -> start cooling
const float lowerTempLimitF = targetTemperatureF - 2.0; // Below this temp, relay turns off -> stop cooling
const int loopMsec = 1000 * 5;
// VARIABLES
int relayStatus = 0;
// All the important stuff
void setup() {
pinMode(blueLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(tempSensor, INPUT);
#ifdef LOGGING
Serial.begin(9600);
Serial.println("Initializing thermostat");
#endif
// start OneWire library
sensors.begin();
}
void loop() {
#ifdef LOGGING
//Serial.println("Requesting temperatures...");
#endif
// read temperature
sensors.requestTemperatures();
float currentTemp = sensors.getTempFByIndex(0);
#ifdef LOGGING
Serial.print("Read currentTemp = ");
Serial.println(currentTemp);
#endif
if (currentTemp < -150)
{
// The thermistor + OneWire will typically read about -196.60 if not properly wired up.
// Instead of treating this as a "too cold" reading, give a visual indication that it's bad.
// Here, we'll still keep the same delay but will flash alternating red/blue.
#ifdef LOGGING
Serial.print("Bad temperature reading (");
Serial.print(currentTemp);
Serial.println(")");
#endif
for(int i = 0; i < loopMsec; i+= 1000)
{
digitalWrite(blueLed, HIGH);
digitalWrite(redLed, LOW);
delay(500);
digitalWrite(blueLed, LOW);
digitalWrite(redLed, HIGH);
delay(500);
}
return;
}
else if (currentTemp < lowerTempLimitF)
{
// too cold. set the blue light
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, HIGH);
// and turn off the relay if it's running
if (relayStatus)
{
#ifdef LOGGING
Serial.println("Turning off the relay");
#endif
digitalWrite(relay, LOW);
relayStatus = 0;
}
}
else if (upperTempLimitF < currentTemp)
{
// too warm
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
if (!relayStatus)
{
#ifdef LOGGING
Serial.println("Turning on the relay");
#endif
digitalWrite(relay, HIGH);
relayStatus = 1;
}
}
else
{
// goldilocks zone. let the relay keep (not) doing its thing for now, but set the LEDs
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
digitalWrite(blueLed, LOW);
}
delay(loopMsec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.