I'm working on paring a sketch for space as I'm placing it on an ATTiny45, and so I've been deleting code left and right, replacing function calls with hard coded data.
Also... the data byte array in the example says it needs 9 bytes, but it only utilizes 2? I chopped it down to a 2 byte array and it worked, but I don't know if I'm somehow wrong for doing that.
One more thing... when I first plug in my sensor (doesn't happen when it's simply reset) I get a junk reading of 185.0f for my first reading. Not a big problem, but annoying.
I've come to the point where I'm not sure I can do any more. Help me chop!
Here is the getTemp function:
float getTemp(){
resetDS();
writeByte(0x44); // start conversion, without parasite power on at the end
resetDS();
writeByte(0xBE); // Read Scratchpad
return (readByte()|(readByte() << 8))*0.1125+32;//convert to deg f
}
void resetDS()
{
pinMode(DS18S20_Pin, OUTPUT);
digitalWrite(DS18S20_Pin, LOW);
delayMicroseconds(255);//480u reqd
pinMode(DS18S20_Pin, INPUT);
delayMicroseconds(255);//480u reqd
writeByte(0xCC);//ROM skip as only one sensor
}
void writeByte(byte data){
pinMode(DS18S20_Pin, OUTPUT);
for (byte mask = 00000001; mask>0; mask <<= 1){
if (data & mask ){ // send 1
digitalWrite(DS18S20_Pin,LOW);
delayMicroseconds(10);//15u reqd
digitalWrite(DS18S20_Pin,HIGH);
delayMicroseconds(45);//60u total
}
else{ //if bitwise and resolves to false
digitalWrite(DS18S20_Pin,LOW); // send 0
delayMicroseconds(50);
digitalWrite(DS18S20_Pin,HIGH);
//delayMicroseconds(5);//60u total is slow enough to negate needing this
}
}
}
byte readByte(){
byte r = 0;
for (byte mask = 00000001; mask>0; mask <<= 1){
pinMode(DS18S20_Pin, OUTPUT);
digitalWrite(DS18S20_Pin, LOW);
//delayMicroseconds(3);//1u reqd pinMode() is slow enough to negate needing this
pinMode(DS18S20_Pin, INPUT); // let pin float, pull up will raise
delayMicroseconds(10);//15u reqd
if(digitalRead(DS18S20_Pin))r|=mask;
delayMicroseconds(40);//60u complete cycle
}
return r;
}
Here is the rest of the program...
#include <SPI.h>
#include <MyLiquidCrystal.h>
int DS18S20_Pin = 5; //DS18S20 thermometer pin*/
byte heatingStage=0;
const int q0 = 1;//first unused 74HC595 pin
const int q2 = 4;//second unused 74HC595 pin
LiquidCrystal lcd(0);// initialize LCD with the number of the ss/latch
void setup() {
lcd.begin(16, 2);// set up the LCD's number of columns and rows:
}
void loop() {
noInterrupts();
float temperature = getTemp();//temperature data
interrupts();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(temperature,1);
lcd.print("f");
if(temperature < 72 && heatingStage!=q0+q2){//stage two heating
lcd._bitString=heatingStage=q0+q2;
lcd.spiSendOut();
lcd.setCursor(0,1);
lcd.print("Stage 2 Heating ");
}
if(temperature < 78 && temperature > 72 && heatingStage!=q0){//stage one heating
lcd._bitString=heatingStage=q0;
lcd.spiSendOut();
lcd.setCursor(0,1);
lcd.print("Stage 1 Heating");
}
if(temperature > 78){//heat off
lcd._bitString=heatingStage=0;
lcd.spiSendOut();
lcd.setCursor(0,1);
lcd.print("Heating Off ");
}
}