I have a strange problem and can't find the cause.
Using the extracted code below, I am getting readings from 2 sensors and displaying on an LCD, and also recording to an SD card.
Working as expected and getting the correct temperatures.
Here's the strange bit :
On the LCD all temperatures are displayed with 2 decimal places.
The serial . print sections are also showing the 2 decimal places in the serial monitor.
However, the data written to the SD card has 4 decimal places.
I understand that the code in the dtostrf line ( with DecCount = 4 ) causes the 4 decimal places.
But why does the other displays ( Serial Monitor and LCD ) show only 2 places, and not 4 decimals ? The 2 decimals must be set somewhere, as 24 degrees is shown as 24.00
So the question is : what causes the monitor and LCD to force to display 2 decimal places ?
I actually need to trim the LCD code to 1 decimal, but without understanding the cause of the above, I don't know how to do this.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
//for the LCD
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //pin numbers for LCD display
const int chipSelect = 53; // the CS pin number as defined by the board specs
//for the Temp sensors
#define ONE_WIRE_BUS 11 // Temperature Data wire is plugged into pin 11 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
//temp sensor unique addresses
DeviceAddress outsideThermometer = { 0x28, 0x3C, 0xC0, 0x08, 0x03, 0x00, 0x00, 0x54 };
DeviceAddress insideThermometer = { 0x28, 0xCC, 0xD1, 0xE1, 0x02, 0x00, 0x00, 0xE6 };
float Tmax = 0, Tmin = 99, LastTempC1 = -1, LastTempC2 = -1, tempC1 = 0, tempC2 = 0; // temp tracking vars
long intervalTemp = 10000; // ms to wait between temp readings
long pMilTemp = 0; // last ms of last temp reading
void setup(void) {
SetupLCD();
SetupSensors();
} // end of Setup
void loop(void){
//+++++++++++++++++++++++++++++++++++++++++++++ TEMPERATURE SENSORS start
unsigned long TempMilNow = millis();
if(pMilTemp == 0 || TempMilNow - pMilTemp >= intervalTemp) {
pMilTemp = TempMilNow;
sensors.requestTemperatures();
//convert the reading into a usable format
float tempC1 = readTemperature(outsideThermometer);
float tempC2 = readTemperature(insideThermometer);
LogItC("Temp In : ",0);
LogItF(tempC2,0,4);
LogItC(" -- Out : ",0);
LogItF(tempC1,1,4);
if (tempC1 != LastTempC1){
if (tempC1 > 1){
LastTempC1 = tempC1;
if (tempC1 < Tmin) Tmin = tempC1;
if (tempC1 > Tmax) Tmax = tempC1;
LCDtemps (tempC1, tempC2, Tmax, Tmin);
}
}
if (tempC2 != LastTempC2){
if (tempC2 > 1){
LastTempC2 = tempC2;
LCDtemps (tempC1, tempC2, Tmax, Tmin);
}
}
}
//+++++++++++++++++++++++++++++++++++++++++++++ TEMPERATURE SENSORS end
} //end of LOOP
void LogItC(char* LogTxt,int LineEnd){
if(LineEnd != 1){
Serial.print(LogTxt);
SDWRfile("datalog.txt", LogTxt) ;
}
if(LineEnd == 1){
Serial.println(LogTxt);
SDWRfile("datalog.txt", LogTxt) ;
}
}
void LogItF(float LogTxt,int LineEnd,int DecCount){
char buffer [100];
if(LineEnd != 1){
Serial.print(LogTxt);
//dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
dtostrf (LogTxt,10,DecCount,buffer);
SDWRfile("datalog.txt", buffer) ;
}
if(LineEnd == 1){
Serial.println(LogTxt);
dtostrf (LogTxt,10,DecCount,buffer);
SDWRfile("datalog.txt", buffer) ;
}
}
void SetupSensors(){
sensors.begin(); // Start up the temperature sensor library
sensors.setResolution(outsideThermometer, 11); // set the resolution to 9/10/11/12 bit
sensors.setResolution(insideThermometer, 11);
LCDtemps (0, 0, 0, 0); // display the starting temperatures
}
void SetupLCD(){
lcd.begin(16, 4); // init the LCD screen
Serial.begin(9600); // init the serial comms
}
void LCDpos(int Lcol, int Lrow) {
lcd.setCursor(Lcol, Lrow);
if(Lrow > 1) lcd.setCursor(Lcol + 16, Lrow - 2);
}
void LCDtemps(float tempC1, float tempC2, float Tmax, float Tmin){
lcd.clear();
LCDpos(0,0);
lcd.print("Inside = ");
lcd.print(tempC2);
LCDpos(0,1);
lcd.print("Outside = ");
lcd.print(tempC1);
LCDpos(0,2);
lcd.print("Max = ");
lcd.print(Tmax);
LCDpos(0,3);
lcd.print("Min = ");
lcd.print(Tmin);
}
float readTemperature(DeviceAddress deviceAddress)
{
float tempC = 0;
tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
return 0;
} else {
return tempC;
}
}