Reputation: 3
i have been trying to get a DS18B20 waterproof temperature sensor to work with my arduino. this is the code i am using:
#include <OneWire.h>
int SensorPin = 2;
OneWire ds(SensorPin);
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temp = getTemp();
Serial.println(temp);
delay(100);
}
float getTemp(){
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);
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for (int i = 0; i < 9; i++) {
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float TRead = ((MSB 8) | LSB);
float Temperature = TRead / 16;
return Temperature;
}
i keep getting the following errors:
In funtion 'float gerTemp()': error: expected ')' before numeric constant error: expected ')' before ';' token
i cant figure out what these mean or how to fix it. can someone help me? thanks!
Upvotes: 0
Views: 850
Reputation: 3739
Well, if you take a look at the lines with the error:
float TRead = ((MSB 8) | LSB);
You can see MSB 8
- there is no operator between these arguments. According to the context, it should be:
float TRead = ((MSB << 8) | LSB);
Maybe even (just to be sure):
float TRead = (((uint16_t)MSB << 8) | LSB);
Upvotes: 1