Go Down

Topic: how do i limit temp display to whole # only? (Read 667 times) previous topic - next topic

mitaccio

I am printing 3 temps and humidity to an lcd and need to restrict the temps to whole numbers only.  I don't know how to code it.  Do I need to post my code to get help or is there a standard code line I can use?

Morris Dovey

It would help to see the applicable declarations and the specific code - but, in general, you can cast a float to an integer type to get just the whole part. if you want to apply rounding, you can do that in the cast expression. For example

#define sign(x) = ((x > 0) - (x < 0)) /* macro */
float e = -2.718;

and then output

(int)(e + sign(e) * 0.5)

to your display to show a rounded whole value.
There's always a better way!

gardner

Another option is round()

Code: [Select]
#include <math.h>

float foo = round(bar);

mitaccio

For the float foo, do I put that in the setup or definition section? :supernoob:

Morris Dovey

Put it in the function where you're going to use it. If you need to access the value from within multiple functions, you can make it global (declare it outside all functions).
There's always a better way!

mitaccio

Here is my sketch.  I am trying to get the lcd.print value for the dht11 temp, humidity, dew point and the dalla onewire temps (OTA and EP) to read in whole number only.

Code: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <dht11.h>
#include <math.h>


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
dht11 DHT11;

#define ONE_WIRE_BUS 6
#define DHT11PIN 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

DeviceAddress OTA={0x28, 0x94, 0xBB, 0x60, 0x03, 0x00, 0x00, 0x38};
DeviceAddress EP={0x28, 0xFE, 0xF6, 0xB6, 0x03, 0x00, 0x00, 0x39};

void setup(void)
{
sensors.begin();
sensors.setResolution(OTA, 10);
sensors.setResolution(EP, 10);

lcd.begin(16,2);
lcd.clear();
}

void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("XX");
} else {
lcd.print(DallasTemperature::toFahrenheit(tempC));
}
}

void loop(void)
{
  {
//Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
//Serial.print("Read sensor: ");
switch (chk)
{
case 0: Serial.println("OK"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
{
delay(1000);

lcd.setCursor(0,0);
lcd.print("T");
lcd.print(DHT11.fahrenheit(), DEC);
lcd.setCursor(6,0);
lcd.print("H");
lcd.print((float)DHT11.humidity, DEC);
lcd.setCursor(10,0);
lcd.print("Dew");
lcd.print((DHT11.dewPointFast()*1.8+32), DEC);

sensors.requestTemperatures();
lcd.setCursor(0,1);
lcd.print("OTA");
printTemperature(OTA);
lcd.setCursor(9,1);
lcd.print("EP");
printTemperature(EP);
}

delay(1000);
}
}

hellonearthis

Try changing the lcd.print line to cast the result as an int.
Code: [Select]
lcd.print( (int) DallasTemperature::toFahrenheit(tempC));

technobabologist

put the number of decimal places you want to have after writing the data.

like this

lcd.print(DHT11.fahrenheit(), 0);

Go Up