dtostrf question

I wrote this code which should display temperature with 2 digits after the decimal point and humidity with none on both a lcd and serial monitor. It displays correctly as written on the lcd
71.23
29
the space in front of the 7 is to make room for a - sign, but on the serial monitor you get:
71.2384
298224
I am using the definition of dtostrf given at http://bradsduino.blogspot.com/2013/05/arduino-c-code-to-convert-float-to.html
I can't understand why the two displays are displaying different.
Jim

We can't do much unless you post your code. Format it first in the IDE (Ctrl-T) and then post it using the code tags (the # icon). However, if I had to bet, my guess is that you set the width parameter to dtostrf() larger than you want.

econjack:
We can't do much unless you post your code. Format it first in the IDE (Ctrl-T) and then post it using the code tags (the # icon). However, if I had to bet, my guess is that you set the width parameter to dtostrf() larger than you want.

/**
 * ReadSHT1xValues
 *
 * Read temperature and humidity values from an SHT1x-series (SHT10,
 * SHT11, SHT15) sensor.
 *
 * Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
 * www.practicalarduino.com
 */

#include <SHT1x.h>
#include <Wire.h>
#include <UTFT.h>\
#include <stdlib.h>



// Specify data and clock connections and instantiate SHT1x object
#define dataPin  15
#define clockPin 14
#define SLAVE_ADDRESS 0x04
extern uint8_t BigFont[];
char tStr[7];
char hStr[4];

UTFT    myGLCD(ITDB32S,38,39,40,41);
SHT1x   sht1x(dataPin, clockPin);


void setup()
{
  Serial.begin(115200); // Open serial connection to report values to host
  Serial.println("Starting up");
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 255);
  Wire.begin(0x04); // join i2c bus
}

void loop()
{
  float temp_c;
  float temp_f;
  float humidity;

  // Read values from the sensor
  temp_c = sht1x.readTemperatureC();
  temp_f = sht1x.readTemperatureF();
  humidity = sht1x.readHumidity();


  dtostrf(temp_f, 6, 2, tStr);
  dtostrf(humidity, 2, 0, hStr);
  Serial.print(tStr);
  Serial.println('   ');
  Serial.print(hStr);
  Serial.println('   ');
  myGLCD.print("Temperature: ",LEFT,0);
  myGLCD.print(tStr,15*13,0);
  myGLCD.print("Humidity: ",LEFT,15);
  myGLCD.print(hStr,12*13,15);
  delay(2000);
}

// callback for received data
void receiveData() 
{
  char c = Wire.read();
///  Serial.println(c);
}



void writeData (char* data)
{
  Wire.beginTransmission(04); // transmit to device #04
  // device address is specified in datasheet
  Wire.write(data);             // sends value byte  
  Wire.endTransmission();     // stop transmitting

  delay(500);
}

// callback for received data
void readData ()
{
  int number;
  while(Wire.available())
  {
    number = Wire.read();
    switch (number)
    {
    case 1:
      writeData(tStr);
      break;
    case  2:
      writeData(hStr);
      break;
    }
  }
}

Relevant part is is loop function

Jim

Serial.println('  ');What's that?

AWOL:
Serial.println('   ');What's that?

A blank line separator

Jim

alfiesty:

AWOL:
Serial.println('   ');What's that?

A blank line separator

Jim

Novel.

The argument to Serial.print() should be a double-quoted string when you wish to print a string literal, such as:

Serial.println("I'm here");

The "ln" at the end of Serial.println() means the next thing printed is on a new line. No need for the redundant blank space. Indeed, I'm not sure what the compiler does with the single-quote space.

Or, indeed, multiple single-quote spaces.

AWOL:
Or, indeed, multiple single-quote spaces.

So spank me and send me to bed without dinner. What does this have to do with my question!

Jim

alfiesty:

AWOL:
Or, indeed, multiple single-quote spaces.

So spank me and send me to bed without dinner. What does this have to do with my question!

Jim

Didn't you wonder where the strange numbers were coming from before the newline?