call dtostrf() with an int

Hi everyone, can you please help me with this code. What it does is sending the Humidity/Temp of DHT22 sensor, but it jumps from 21.0 to 22.0 and 23.0 …looks like secundary decimals are missing. I asked this issue on Internacional Arduino Forum but the thing is still in the air. (they said to call dtostrf with a int), wich i did and get same result. Here is my code:

#include <DHT22.h>

DHT22 dht22A(7);         // DHT is D7
unsigned int Temp_Val_A = 0;    
unsigned int Hum_Val_A = 0;     // integer of Humidity Sensor A

char numberString[10];  //  allows for 9 digits

void setup()
void loop()
  float tempCA = dht22A.getTemperatureC();  
  float humA = dht22A.getHumidity();
  Temp_Val_A = dht22A.getTemperatureC();   // read value and save it to variable
  Hum_Val_A = dht22A.getHumidity();
   if (Temp_Val_A != tempCA)
       dtostrf(Temp_Val_A, 4, 1, numberString);
       Serial.print("<TA");              // ex.: <Temperature of sensor A>
   if (Hum_Val_A != humA)
       dtostrf(Hum_Val_A, 4, 1, numberString);
       Serial.print("<HA");              //  <Humidity of sensor A>

void formatNumber( unsigned int number, byte digits)
     char tempString[10] = "\0";
    strcpy(numberString, tempString);
    // convert an integer into a acsii string
    itoa (number, tempString, 10);
    // create a string of '0' characters to pad the number  
    byte numZeros = digits - strlen(tempString) ;
    if (numZeros > 0)
       for (int i=1; i <= numZeros; i++)    { strcat(numberString,"0");  }

Can you please explain me were and how to modify the code. Thank you very much, regards.

You are requesting a formatted string with 2 digits an 1 decimal (4,1). An int has no decimal places so the result is absolutely correct.
If your numberstring contains a floating point number you might try atof() to convert it first to a float number and then use dtostrf().


So why do you end up asking this question in the Dutch forum, you being from Spain ?

And to the point:
You are converting an int to a string.
An integer will always end up with .0

You need to use another type, the same you are using somewhere else already.

Thank you Nicoverin for answering, can you please help me coding it, i don’t have much experience with it.
MAS3 i just came off from Breda and i thought it will be no problem, buy the way…if i live in Spain this does not mean i am spanish :slight_smile: , beside Netherlands people are very very kind, so i thought maybe someone can help me out. I will really apreciate your help. I am dealling with this problem for long time, changing codes and no result. I wish i had more experience coding, i don’t know what to change to the code. Thank you very much, and sorry if i am disturbing.

Change these into floats

unsigned int Temp_Val_A = 0;
unsigned int Hum_Val_A = 0;     // integer of Humidity Sensor A

like this

float Temp_Val_A = 0.0;   
float Hum_Val_A = 0.0;

Thank you for helping me.
If i change it to float Temp_Val_A = 0.0; i don’t get any value from serial port.

Also tried
tempCA = atof(numberString); // atof() convert numberstring to a float, then use dtostrf().
That’s why i need your expert opinion. Thank you.

The problem with your program is that it is not documented. Thus it is unclear what you actually want to do. This makes it very unclear for others to understand what your intentions are. That might just be the reason that even on the international forum they are still decyphering what you want to do.
In future I would suggest:
a) make clear comments what the program does
b) comment the steps you want to do in the program
c) make a clear constructive description of your problems.
You'd be surprised how many responses you might get.
The old statement "garbage-in = garbage-out" is in these days more valid due to sloppy communication.