Pages: [1]   Go Down
Author Topic: how do i limit temp display to whole # only?  (Read 592 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

There's always a better way!

Ontario
Offline Offline
God Member
*****
Karma: 24
Posts: 862
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another option is round()

Code:
#include <math.h>

float foo = round(bar);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

There's always a better way!

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
}
Logged

New Zealand
Offline Offline
Full Member
***
Karma: 1
Posts: 105
Just this guy you know.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Scotland
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

like this

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

Pages: [1]   Go Up
Jump to: