Created
June 16, 2013 21:32
-
-
Save ryantenney/5793505 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <OneWire.h> | |
#include <SoftwareSerial.h> | |
float temp = -1; | |
float setTemp = 38; | |
boolean compressor = false; | |
boolean compressorDelayed = false; | |
float compressorOffTime = 0; // compressor should be off initially | |
const int compressorDelay = 300; // 5 minutes | |
const unsigned long MAX_LONG = 4294967265L; | |
unsigned long lastTimeMillis = 0; | |
float time; | |
SoftwareSerial mySerial(3, 13); // pin 2 = TX, pin 3 = RX (unused) | |
OneWire ds(5); | |
void setup(void) { | |
Serial.begin(9600); | |
setupLCD(); | |
} | |
void setupLCD() { | |
mySerial.begin(9600); // set up serial port for 9600 baud | |
delay(500); // wait for display to boot up | |
mySerial.write(254); // cursor to beginning of first line | |
mySerial.write(128); | |
mySerial.write("Temp: "); | |
mySerial.write("Compressor: "); | |
} | |
void loop(void) { | |
time = getTime(); | |
temp = getTemp() * 1.8 + 32; | |
if (compressor) { | |
// compressor currently on | |
if (temp <= setTemp) { | |
Serial.println("Compressor turned off!"); | |
compressor = false; | |
compressorOffTime = time; | |
} | |
} else { | |
// compressor currently off | |
if (temp > setTemp) { | |
Serial.println("Too warm!"); | |
if (time - compressorOffTime >= compressorDelay) { | |
Serial.println("Compressor turned on!"); | |
compressor = true; | |
compressorDelayed = false; | |
} else { | |
compressorDelayed = true; | |
Serial.print("Compressor delay: "); | |
Serial.print(time - compressorOffTime); | |
Serial.print("/"); | |
Serial.println(compressorDelay); | |
} | |
} else { | |
compressorDelayed = false; | |
} | |
} | |
mySerial.write(254); | |
mySerial.write(136); | |
mySerial.write(" "); | |
mySerial.write(254); | |
mySerial.write(temp >= 100.0 ? 136 : temp >= 10.0 ? 137 : 138); | |
mySerial.print(temp); | |
mySerial.write(223); // degree symbol | |
mySerial.print("F"); | |
mySerial.write(254); | |
mySerial.write(204); | |
mySerial.write(" "); | |
mySerial.write(254); | |
mySerial.write(204); | |
mySerial.write(compressor ? " ON" : (compressorDelayed ? "WAIT" : " OFF")); | |
delay(1000); | |
Serial.print("Time: "); | |
Serial.println(getTime()); | |
Serial.print("Temp: "); | |
Serial.print(temp); | |
Serial.println(" degF"); | |
} | |
float getTime() { | |
unsigned long timeMillis = millis(); | |
if (timeMillis != lastTimeMillis) { | |
if (timeMillis < lastTimeMillis) { | |
time += (float)(MAX_LONG - lastTimeMillis + timeMillis) / 1000.0; | |
} else { | |
time += (float)(timeMillis - lastTimeMillis) / 1000.0; | |
} | |
lastTimeMillis = timeMillis; | |
} | |
return time; | |
} | |
float getTemp(){ | |
//returns the temperature from one DS18S20 in DEG Celsius | |
byte data[12]; | |
byte addr[8]; | |
if ( !ds.search(addr)) { | |
//no more sensors on chain, reset search | |
ds.reset_search(); | |
return -1000; | |
} | |
if ( OneWire::crc8( addr, 7) != addr[7]) { | |
Serial.println("CRC is not valid!"); | |
return -1000; | |
} | |
if ( addr[0] != 0x10 && addr[0] != 0x28) { | |
Serial.print("Device is not recognized"); | |
return -1000; | |
} | |
ds.reset(); | |
ds.select(addr); | |
ds.write(0x44,1); // start conversion, with parasite power on at the end | |
byte present = ds.reset(); | |
ds.select(addr); | |
ds.write(0xBE); // Read Scratchpad | |
for (int i = 0; i < 9; i++) { // we need 9 bytes | |
data[i] = ds.read(); | |
} | |
ds.reset_search(); | |
byte MSB = data[1]; | |
byte LSB = data[0]; | |
float tempRead = ((MSB << 8) | LSB); //using two's compliment | |
float TemperatureSum = tempRead / 16; | |
return TemperatureSum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment