Code is outputting "?" for dht readings

my main goal is to print readings from ldr and dht 22/11 onto a lcd screen but im having one trouble. the code below shows a snippet of the code im testing. i want it to combine two float values into strings so i can easily create a function to print the temp and humidity. although i was testing an added Serial.Print(Temp); which only prints "Temp: ?" but i want it to print "Temp: (insert Temp)"

here is the snippet of code.

    float f = dht.readTemperature(true);
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    char Temp[16] = "Temp: ";
    char H[16] = "Humi: ";
    char t_str[16];
    char h_str[16];
snprintf(t_str, sizeof(t_str), "%.2f", t);
snprintf(h_str, sizeof(h_str), "%.2f", h);
strncat(Temp, t_str, sizeof(Temp)-strlen(Temp)-1);
strncat(H, h_str, sizeof(H)-strlen(H)-1);

Serial.print(Temp);

Many Arduino boards do not support the use of floats with snprintf() and related functions so you need to use dtostrf() to convert the floats to strings first.

Which board are you using ?

Which type of board you are trying to use

@hollandmab
If it is code-legal, replace:

    snprintf(t_str, sizeof(t_str), "%.2f", t);
    snprintf(h_str, sizeof(h_str), "%.2f", h);

with

        /*float*/,  /*float.width*/, /*decimals*/, /*buffer*/);
dtostrf(    t    ,         5       ,      2      ,    t_str  );
dtostrf(    h    ,         5       ,      2      ,    h_str  );

Here is a simulation (link).

Arduino uno

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.