Updated again to include LCD
It now shows whats going on using the LCD
Heres the code;
/* This is the sketch to go along with the Coleman 12Volt TEC Cooled Cooler.
by Michael Illingby 3/10/2014
I was tired of having it freeze my beverages, which it did in a couple hours.
This is intended to monitor a OneWire temp sensor, and activate a 5v relay
that is interupting the power to the cooling system. Hopefully I will now
just have COLD Red Bull, instead of Red Bull Popsicles. :)
Updated 5-15-14 : Now outputs current temp on a 16x2 LCD so I know just how cold everything is in there.
Lines 109-125 are where I set the temperature and padding.
*/
// The DallasTemperature library
// http://milesburton.com/Dallas_Temperature_Control_Library
#include <OneWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
OneWire ds(A3); // on pin 10 (a 4.7K resistor is necessary)
int relay = A0; // Pin that relay signal pin is connected to
void setup(void) {
Serial.begin(9600); // Open serial monitor to watch temp for debugging
pinMode(relay, OUTPUT); // this is the signal wire to relay board 5v
lcd.begin(16,2);
}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
for( i = 0; i < 8; i++) {
Serial.write(' ');
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
default:
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // delay between polling OneWire Chip 750ms is MIN, I choose between 10-60 seconds
// Longer delay reduces
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0; // convert output from sensor to Celsius
if (celsius <= 6) {
digitalWrite(relay, LOW); //De-energize the relay, we are at or below temp
Serial.print("Standby Mode - Current Temp in Celsius ");
lcd.setCursor(11,1);
lcd.print("Done!");
}
else if ((celsius >= 6.01) && (celsius <= 6.49)){
Serial.print("Standby Mode - Current Temp in Celsius ");
lcd.setCursor(10,1);
lcd.print(" Cold ");
delay(500);
lcd.setCursor(10,1);
lcd.print(" ");
delay(250);
}
else if (celsius >= 6.5){
digitalWrite(relay, HIGH); //Energize the relay for cooling, we are above temp
Serial.print("Cooling Mode - Current Temp in Celsius ");
lcd.setCursor(9,1);
lcd.print("Cool ");
delay(400);
lcd.setCursor(9,1);
lcd.print(" Cool ");
delay(400);
lcd.setCursor(9,1);
lcd.print(" Cool ");
delay(400);
lcd.setCursor(9,1);
lcd.print(" Cool");
delay(400);
lcd.setCursor(9,1);
lcd.print(" Cool ");
delay(400);
lcd.setCursor(9,1);
lcd.print(" Cool ");
delay(400);
lcd.setCursor(9,1);
lcd.print(" Cool ");
delay(400);
lcd.setCursor(9,1);
lcd.print("Cool");
delay(400);
lcd.setCursor(9,1);
lcd.print(" ");
delay(400);
}
// print the analog value:
Serial.println(celsius); // Print debugging info to serial monitor showing mode and temp
lcd.setCursor(0,0);
lcd.print("*Coleman Cooler*");
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.print(celsius);
}