This is my first post so be patient.
I have two DS18B20 modules and an RTC DS3231. The two temps and time are being sent to an sd card for further analysis. I also want to show temps and time on 16x2 I2C LCD, so space is at a premium. How can I lcd.print the temps as integers(?), XX, rather than the default XX.XX?
Google cast
float myFloat = 10.11;
int myInt = (int) myFloat;
If you want rounding, do that first.
Does anybody know how to output the RAW 12-bit temp data from the DS18B20.
That would fit in an int, and would preserve the full resolution of the DS18B20.
I saw a ReadTemp12 command somewhere.
The sensor has a 1/16th degreeC resolution,
so a crude way would be to multiply float tempC by 16 and store that in an int.
Then divide it by 16 when read back.
float myFloat = 10.11;
int myInt = myFloat >> 4; // multiply by 2^4
Leo..
Hello
Take a view into the libary and search for "raw".
You guys are way ahead of me. I've been at this only four weeks.
Here is my display statement that may be pertinent:
lcd.print(sensors.getTempCByIndex(0));
It is displaying XX.XX.
Does this: float myFloat = 10.11;
int myInt = (int) myFloat;
need to be done way up at the beginning of the sketch? If so, how do I relate 'myFloat' or 'int' to 'sensors.getTempCByIndex(0)'?
And the two libraries that may relate to my question:
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>.
This is how you may go --
sensors.requestTemperatures(); // Send the command to get temperatures
float myTemp = sensors.getTempCByIndex(0);
lcd.setCursor(0, 1); //left-most position of Bottom line
lcd.print((int)myTemp);
For Nano:
#include <OneWire.h>
OneWire ds(7); // on pin 7 (a 4.7K resistor is necessary)
unsigned long start;
int end = 10000;
byte addr[8],
data[12];
void setup()
{
Serial.begin(9600);
Serial.println("\n" __FILE__"\n");
ds.search(addr);
}
void loop()
{
if(millis() - start > end)
{
start += end;
int val, newVal;
static byte cntr = 0;
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (byte i = 0; i < 9; i++) // we need 9 bytes
data[i] = ds.read();
ds.reset();
ds.select(addr);
ds.write(0x44); // start conversion
newVal = data[1] << 8 | data[0]; // 12 bit raw data <<<<<<<<<<
val = antiDither(newVal);
if(++cntr > 15)
{
cntr = 0;
Serial.println("tempC");
}
Serial.print(val >> 4);
Serial.print("_");
Serial.println(val & 0x000F,HEX);
}
// do other programming here
//...
//......
//.........
}
int antiDither(int newVal) // prevents +1 <-> -1 dither
{
static int val = 0, oldVal = 0;
if(newVal != val && newVal != oldVal)
{
oldVal = val;
val = newVal;
}
return val;
}
GolamMostafa Works great.
Others, thanks for responses. |
Sorry, my bad, I thought you wanted to save space on the SD card (two instead of four bytes).
To print to an LCD without decimal places,
you simply add how many decimal places you like to print (default is two).
No casting to int needed.
lcd.print (tempC, 3); // prints three decimal places
lcd.print (tempC, 0); // no decimal places
Same to print to the serial monitor.
Leo..
Even better. Thanks.
How does one learn stuff like that? Is it described in the library somewhere?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.