/* NOTE: External Thermostat using a DS18S20. This uses two relays hotwired into a cheap home automation RF remote to turn one channel on and off. planning to run this on an ATTiny45 by Non-ICE edit: needed to add some self check indicators since this still is a breadboardbuild edit II: this code is attiny45-ready edit III: screw tiny45; code is attiny25-ready */ #include int settemp=180; //wanted temperature in celsius times 10, was 260??? int DS18S20_Pin = 0; //DS18S20 Signal pin on digital 0 int offpin = 3; //relay for offbutton attached here, also connected a red LED int onpin = 4; //relay for onbutton attached here int switchdelay = 5; //minimum delay in seconds between each toggle. //no changes needed below this point. int modechange = 15; //toggle timer counter, 15 seconds after boot int heatstate; //toggle state holder //Temperature chip i/o OneWire ds(DS18S20_Pin); // on digital pin 0 void setup(void) { pinMode(onpin, OUTPUT); pinMode(offpin, OUTPUT); } void loop(void) { float temperature = getTemp(); if (temperature > 40 || temperature < 0) { //if input data is way off something is wrong give some indication digitalWrite(offpin, true); delay(5000); digitalWrite(offpin, false); } else { // all ok, do heater toggle if (temperature < (settemp/10) && heatstate != onpin) { heatstate=onpin; heatswitch(); } else { heatstate=offpin; heatswitch(); } } //if (modechange>0) {modechange=modechange-1;} delay(999); } void heatswitch(){ for (int repeats = 0; repeats <= 3; repeats++) { digitalWrite(heatstate, true); delay(1500); digitalWrite(heatstate, false); delay(2000); } modechange = switchdelay; } float getTemp(){ //returns the temperature from one DS18S20 in DEG Celsius byte data[12]; byte addr[8]; // error checking 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; } //read DS 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; }