xenoworld.org DIY and geekishness

Arduino Thermostat

After brewing our own beer for some time, a friend of mine and myself thought we should find a way to ferment the kegs of beer on a constant temperature and not just gambling on whatever temperature the basement would hold.

So, after being inspired by Sean Cotes' Arduino Beer Thermostat, I thought I might as well make one myself with a few alterations.

I'll upload the schematics when I get the chance...

The code is as follows:

// Jon Ivar Larsen/XeNo - www.xenoworld.org - 2009
// Code based on Sean Cotes' Arduino Beer Thermostat -
// http://www.uchobby.com/index.php/2007/10/08/arduino-beer-thermostat/
// and Carlyn Maw and Tom Igoe's shiftOutCode - http://www.arduino.cc/en/Tutorial/ShiftOut

//INCLUDES
#include <Wire.h>
#include <stdlib.h>

//VARIABLES
float tempC;
int tempHigh;
int tempLow;
int pot1Val;
int pot2Val;
int relayToggled;
char valueStr[5];
//For finding most- and least-significant bit
int msb;
int lsb;
//For choosing what to display in the 7-segments based on the state of the 3-way switch
int switchChosenValue;
//Variables for the state of each switchPin
int switchPin1State = 0;
int switchPin2State = 0;
int switchPin3State = 0;
//holders for infromation you're going to pass to shifting function
byte dataLeft7Segment;
byte dataRight7Segment;
byte dataArrayNumbers[10];

// INPUTS
int tempPin = 0;
int pot1Pin = 1;
int pot2Pin = 2;
int latchPin = 8; //Pin connected to ST_CP of 74HC595
int clockPin = 11; //Pin connected to SH_CP of 74HC595
int dataPin = 10; //Pin connected to DS of 74HC595
//Pins connected to the 3-way switch
int switchPin1 = 5;
int switchPin2 = 6;
int switchPin3 = 7;

//OUTPUTS
int relayPin = 12;

void setup()
{
Serial.begin(9600); //Opens serial port, sets data rate to 9600 bps
pinMode(relayPin, OUTPUT);
relayToggled = 0; //Set the relay status to not toggled

pinMode(latchPin, OUTPUT);  //set pins to output because they are addressed in the main loop

//3-way Switch
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
//sets the default (unpressed) state of the switchPins to HIGH
digitalWrite(switchPin1, HIGH);
digitalWrite(switchPin2, HIGH);
digitalWrite(switchPin3, HIGH);

//sets the array of hex-data for the numbers 0-9
dataArrayNumbers[0] = 0x3F; // 0
dataArrayNumbers[1] = 0x06; // 1
dataArrayNumbers[2] = 0x5B; // 2
dataArrayNumbers[3] = 0x4F; // 3
dataArrayNumbers[4] = 0x66; // 4
dataArrayNumbers[5] = 0x6D; // 5
dataArrayNumbers[6] = 0x7C; // 6
dataArrayNumbers[7] = 0x07; // 7
dataArrayNumbers[8] = 0x7F; // 8
dataArrayNumbers[9] = 0x67; // 9

//function that blinks all the LEDs
//gets passed the number of blinks and the pause time
blinkAll_2Bytes(2,500);
}

void loop()
{
checkPots();
checkTemp();
checkSwitch();
display7Segment();
printSerial();

toggle();
delay(200); //Wait 200ms
}

void checkSwitch()
{
//Read the states of the switchpins
switchPin1State = digitalRead(switchPin1);
switchPin2State = digitalRead(switchPin2);
switchPin3State = digitalRead(switchPin3);

if (switchPin1State == LOW){
switchChosenValue = tempLow;
}
if (switchPin2State == LOW){
switchChosenValue = tempC;
}
if (switchPin3State == LOW){
switchChosenValue = tempHigh;
}
}

void display7Segment()
{
//Divide up the numbers in most significant bit (msb) and least significant bit (lsb)
msb = switchChosenValue / 10;
lsb = switchChosenValue - (msb * 10);

//load the light sequence you want from array
dataLeft7Segment = dataArrayNumbers[msb];
dataRight7Segment = dataArrayNumbers[lsb];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
//move 'em out
shiftOut(dataPin, clockPin, dataRight7Segment);
shiftOut(dataPin, clockPin, dataLeft7Segment);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
}

void checkTemp()
{
int span = 20;
int aRead = 0;
for (int i = 0; i < span; i++) {
aRead = aRead + analogRead(tempPin);
}
aRead = aRead / span;
tempC = (4.96 * aRead * 100.0)/1024.0; //convert the analog data to temperature
}

void checkPots()
{
pot1Val = analogRead(pot1Pin);
tempHigh = (pot1Val / 30);

pot2Val = analogRead(pot2Pin);
tempLow = (pot2Val / 30);
}

void printSerial()
{
Serial.print(tempC); Serial.print(" "); Serial.print(tempLow);
Serial.print(" "); Serial.print(tempHigh); Serial.print(" ");
Serial.println(relayToggled);
}

void toggle()
{
if (tempC >= tempHigh) {
tooHot();
}

if (tempC <= tempLow) {
tooCold();
}
}

void tooHot()
{
digitalWrite(relayPin, HIGH);
relayToggled = 1;
}

void tooCold()
{
digitalWrite(relayPin, LOW);
relayToggled = 0;
}

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut){
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low

//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);

//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);

//for each bit in the byte myDataOut�
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--)  {
digitalWrite(myClockPin, 0);

//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}

//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}

//stop shifting
digitalWrite(myClockPin, 0);
}

//blinks the whole register based on the number of times you want to
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll_2Bytes(int n, int d){
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(200);
for (int x = 0; x < n; x++) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
digitalWrite(latchPin, 1);
delay(d);
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(d);
}
}
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

*

No trackbacks yet.