Hi,
I'm experiencing an inexplicable behavior from my DS18B20 temperature sensor,
It seems to return an error value (-127) if the float variable storing the temperature is not
printed out before it's use.
Not working code slimmed down:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(){
Serial.begin(9600);
while(!Serial);
sensors.begin();
Serial.println(sensors.getDeviceCount());
}
void log(int num,float v){
char s[20];
//Serial.println(v);//commented print
dtostrf(v,0,2,s);
Serial.print(s);
snprintf(s,19,"%d;%s\r\n",num,s);
...
}
float getTemperature(){
float t;
sensors.requestTemperatures();
t=sensors.getTempCByIndex(0);
//Serial.println(t);//commented print
return t;
}
Perfectly working code:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(){
Serial.begin(9600);
while(!Serial);
sensors.begin();
Serial.println(sensors.getDeviceCount());
}
void log(int num,float v){
char s[20];
Serial.println(v);// !!!not commented commented print
dtostrf(v,0,2,s);
Serial.print(s);
snprintf(s,19,"%d;%s\r\n",num,s);
...
}
float getTemperature(){
float t;
sensors.requestTemperatures();
t=sensors.getTempCByIndex(0);
//Serial.println(t);//commented print
return t;
}
Everything is of course part of a much bigger program, but the presence or not-presence
of that print makes all the difference.
There are two Serial.print(); ,any of them fixes the problem. (both too)
Without a print, the sensor doesn't even show up on deviceCount
To be fair I'm not exactly new to programming (especially in C), but here,
I've really ran out of ideas. I lost so much time after this so here I am,
begging desperately for help
Arduino Leonardo
Arduino IDE 1.8.4
Windows 10
Thank you