Trimming a float

I have my sketch working correctly and displaying Temperature and Humidity from a DHT-22 sensor on a 16x2 LCD display. However, the code that reads the sensor uses a floating point variable which displays as xx.yz where z is always zero, and I would therefore like to remove this character so that the number is displayed as xx.y I'm not very familiar with C++ so I'm asking the forum whether there is a simple command to specify the length of the floating point number in some way, or whether I need to write code to change the float to a string and then truncate the string's last character. Of course, any code samples would be most welcome.

The segment of code I am using at the moment looks like this:

void loop() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(t) || isnan(h)) { //Serial.println("Failed to read from DHT"); } else { lcd.setCursor(10, 0); lcd.print(t);lcd.print("C"); lcd.setCursor(10, 1); lcd.print(h);lcd.print("%"); }

Thanks in advance Ed

try

 lcd.print(t);lcd.print("C");

to

 lcd.print(t,1);lcd.print("C");

within the print you can specify the number of decimals to print of a float

Thanks - that solved my problem permanently. Did not think to look at the print command!

Have a further question - not sure if it's a programming question or a hardware thing. When the code is running, and I edit the source and then try to upload it, it tells me that COM 13 is in use and I should close any process using it. The only thing on COM 13 is the Arduino Uno, and I cannot see any specific debugging stuff running or trying to access COM 13, nor can I see any obvious configuration options to control COM 13 usage. I therefore have to unplug the Arduino from the USB port and close the Arduino editor, plug the board back in to the USB port, restart the editor, reload my code, and then I can upload it successfully. Pressing reset on the Arduino board does not make any difference - I still need to unplug and reconnect as above. Any help on this one would be most welcome! Cheers Ed

Any help on this one would be most welcome!

What version of the IDE? On what operating system? Talking to which Arduino?

What version of the IDE? 1.0.5 On what operating system? Win 7 64 bit SP1 Talking to which Arduino? Arduino Uno R3

Thanks Ed

Now, you need to post ALL of your code. It is necessary, in order for us to understand how your hardware is connected.

Are you closing the Serial Monitor first? You shouldn't need to (I don't have to), but YMMV. Are you using any bluetooth stuff?.

How do I close the serial monitor?
For reference, the code follows. I have remarked out any references to serial outputs as I’m using the LCD for display purposes.
The button code is not currently being used, but will be when I get around to extending the program to allow thresholds to be defined.

/*********************
Example code for the Adafruit RGB Character LCD Shield and Library
This code displays text on the shield, and also reads the buttons on the keypad.
When a button is pressed, the backlight changes color.
**********************/
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include “DHT.h”
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can’t use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

int TEMP=00;
int HUMIDITY=00;

#define DHTPIN 2 // what pin we’re connected to

// Uncomment whatever type you’re using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

void setup() {
// Debugging output
//Serial.begin(9600);
//Serial.println(“DHTxx test!”);
dht.begin();
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);

// Print a message to the LCD. We track how long it takes since
// this library has been optimized a bit and we’re proud of it :slight_smile:
//int time = millis();
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print(“Humidity: “);
lcd.setCursor(10, 0);
lcd.print(TEMP);lcd.print(“C”);
lcd.setCursor(10, 1);
lcd.print(HUMIDITY);lcd.print(”%”);
//time = millis() - time;
//Serial.print(“Took “); Serial.print(time); Serial.println(” ms”);
lcd.setBacklight(WHITE);
}

uint8_t i=0;
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
//Serial.println(“Failed to read from DHT”);
} else {
// Serial.print(“Humidity: “);
// Serial.print(h);
// Serial.print(” %\t”);
// Serial.print(“Temperature: “);
// Serial.print(t);
// Serial.println(” *C”);
lcd.setCursor(10, 0);
lcd.print(t,1);lcd.print(" C");
lcd.setCursor(10, 1);
lcd.print(h,1);lcd.print(" %");

}

// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
//lcd.setCursor(0, 1);
// print the number of seconds since reset:
//lcd.print(millis()/1000);
uint8_t buttons = lcd.readButtons();

if (buttons) {
// lcd.clear();
if (buttons & BUTTON_UP) {
lcd.setCursor(10,0);
TEMP=TEMP++;
lcd.print(TEMP);lcd.print(" C ");
delay(400);
// lcd.print("UP “);
// lcd.setBacklight(RED);
}
if (buttons & BUTTON_DOWN) {
lcd.setCursor(10,0);
TEMP=TEMP–;
lcd.print(TEMP);lcd.print(” C ");
delay(400);
// lcd.print("DOWN ");
// lcd.setBacklight(YELLOW);
}
if (buttons & BUTTON_LEFT) {
lcd.print("LEFT ");
lcd.setBacklight(GREEN);
}
if (buttons & BUTTON_RIGHT) {
lcd.print("RIGHT ");
lcd.setBacklight(TEAL);
}
if (buttons & BUTTON_SELECT) {
lcd.print("SELECT ");
lcd.setBacklight(VIOLET);
}
}
}