Thanks chris, I figured out array some time ago but I was busy working on other problems to update my post.
I decided to chuck the search function and I am now inputting my serial numbers manually, works every time. After some testing I found that the sample code was only CRC checking the serial number and not the result! So I copy pasted the CRC checker for the serial and made it work with the temp result. The only thing is, the CRC check is missing some bad results. First here is my hacked up code as it stands now:
#include <OneWire.h>
/* DS18S20 Temperature chip i/o
*/
OneWire ds(2); // on pin 2
void setup(void) {
// initialize inputs/outputs
// start serial port
delay(2000);
Serial.begin(9600);
digitalWrite(13,HIGH);
Serial.print("Going to loop()");
}
void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8]= {0x28,0x21,0x71,0x8A,0x01,0x00,0x00,0x56};
byte sensor2[8]= {0x28,0x0E,0x53,0x8A,0x01,0x00,0x00,0xBF};
int Temp;
/*
if ( !ds.search(addr)) {
//Serial.print("No more addresses.\n");
ds.reset_search();
//Serial.print("Reset complete");
return;
}
digitalWrite(13,LOW);
Serial.print("R=");
for( i = 0; i < 8; i++) {
Serial.print(addr[i], DEC);
Serial.print(" ");
}*/
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
/*
if ( addr[0] != 0x28) {
Serial.print("Device is not a DS18S20 family device.\n");
return;
}*/
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(750); // maybe 750ms is enough, maybe not
// 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
Serial.print("P=");
Serial.print(present,HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
if ( OneWire::crc8( data, 8) != data[8]) {
Serial.print("CRC Bad!");
}
else {
Serial.print("CRC A-OK!");
}
Temp=(data[1]<<8)+data[0];//take the two bytes from the response relating to temperature
Temp=Temp>>4;//divide by 16 to get pure celcius readout
//next line is Fahrenheit conversion
Temp=Temp*1.8+32; // comment this line out to get celcius
Serial.print("T=");//output the temperature to serial port
Serial.print(Temp);
Serial.print(" ");
Serial.print(" CRC=");
Serial.print( OneWire::crc8( data, 8), HEX);
Serial.println();
}
And here is some output, It is definatly not 185 degreese in here...
Going to loop()P=1 91 1 4B 46 7F FF F 10 25 CRC A-OK!T=77 CRC=25
P=1 92 1 4B 46 7F FF E 10 24 CRC A-OK!T=77 CRC=24
P=1 93 1 4B 46 7F FF D 10 32 CRC A-OK!T=77 CRC=32
P=1 95 1 4B 46 7F FF B 10 B CRC A-OK!T=77 CRC=B
P=1 98 1 4B 46 7F FF 8 10 22 CRC A-OK!T=77 CRC=22
P=1 FF FF FF FF FF FF FF FF FF CRC Bad!T=30 CRC=C9
P=1 FF FF FF FF FF FF FF FF FF CRC Bad!T=30 CRC=C9
P=1 FF FF FF FF FF FF FF FF FF CRC Bad!T=30 CRC=C9
P=1 FF FF FF FF FF FF FF FF FF CRC Bad!T=30 CRC=C9
P=1 FF FF FF FF FF FF FF FF FF CRC Bad!T=30 CRC=C9
P=1 FF FF FF FF FF FF FF FF FF CRC Bad!T=30 CRC=C9
P=1 50 5 4B 46 7F FF C 10 1C CRC A-OK!T=185 CRC=1C
P=1 A2 1 4B 46 7F FF E 10 D8 CRC A-OK!T=78 CRC=D8
P=1 A3 1 4B 46 7F FF D 10 CE CRC A-OK!T=78 CRC=CE
P=1 A4 1 4B 46 7F FF C 10 DA CRC A-OK!T=78 CRC=DA
P=1 A4 1 4B 46 7F FF C 10 DA CRC A-OK!T=78 CRC=DA
P=1 A5 1 4B 46 7F FF B 10 F7 CRC A-OK!T=78 CRC=F7
P=1 A4 1 4B 46 7F FF C 10 DA CRC A-OK!T=78 CRC=DA
P=1 A4 1 4B 46 7F FF C 10 DA CRC A-OK!T=78 CRC=DA
P=1 A3 1 4B 46 7F FF D 10 CE CRC A-OK!T=78 CRC=CE
I guess I will have to live with this false crc, its better than no crc I suppose. I made these errors by jamming my sensor in and out of the breadboard, not really a real life scenario....