How to modify dht sensor library to display a result without decimals ?

Hello everyone,

I am recently making a project using Arduino Uno , involving DHT11 temperature and humidity sensor, i

found many sketches and helpful libraries, but couldnt find a specific one ; in my project i have to

display the temp and humid,but the problem is that i dont have much place on my LCD to display the

result with decimals ex( 23.00 *C),

is there any sketch that i can modify it so the result can be displayed just ( 23 *C) without the decimals ?

Thank you !

is there any sketch that i can modify it so the result can be displayed just ( 23 *C) without the decimals ?

Yes. Yours.

If you had posted it, maybe we could help.

There are two possibilities. First, even though the function to get the temperature returns a float, you can store the return value in an int, resulting in truncation.

Second, the function that shows the float as text is probably derived from Print, so it takes a second argument to define how many digits after the decimal point to show.

PaulS,

Thank You for reply,

In sorry that im not a programmer , some issues i can fix in the sketch, if functions existed; but modifying

libraries its a bit confusing :-( , i think some practice will be helpful for future,

in the link below is the library that i found on adafruit

https://github.com/adafruit/DHT-sensor-library ,

Thank you !!

sam0202: in the link below is the library that i found on adafruit

https://github.com/adafruit/DHT-sensor-library ,

The default number of digits when formatting a 'float' value with the "print()" function is 2 digits.

But you can override it and use the print() function with any number of digits while formatting a 'float'.

I.e. 'float' formatting with 0 digits:

Serial.print("Temperature: ");
Serial.print(t,0);

Or with 4 digits:

Serial.print("Temperature: ");
Serial.print(t,4);

jurs,

that was superb , its working !

thank you a lot !

Hello everyone ! ,

I’ve tried to complete my system, but unfortunately newer problem occurred, in fact im trying to

control Peltier based fridge usig a TEC , Fans, sinc and DHT 11 sensor, The purpose is to control this

system via LCD keypad Shield in 2 schedules:

1st - To control manually by increasing the power of TEC and FAN through pwm pins

2nd - Automatic switch based on entry level number

with my sketch i could control it manually and display it in fine way using the " switch…case"

commands

to determine the pressed key and to do action ( increase or decrease power), but when it comes to

include the automatic sketch in the 5-th case (or button) , it didnt work perfectly , and i think the

problem is in how to save this mode ( i included (delay) the auto mode worked just when pressing the

key, worked during that delay and i had to push the button each time after that the delay

ends to make auto mode works;

so please any suggestions and solutions how to save the auto mode , also how to adjust the auto

mode power or temperature reference ?

This is my sktech :

<#include <dht.h> // include DHT library

#include <LiquidCrystal.h> // Include LCD 16 x2 Library
#define dht_apin A1 // Analog Pin sensor is connected to
LiquidCrystal lcd(8,9,4,5,6,7); // introduce LCD digital pins
 
 dht DHT;
 
 int fan= 3; //The N-Channel MOSFET connected to fan 
//is on digital pin 3
int powerfan= 0; //fan Power level fro 0 to 99%
int fan_level = map(powerfan, 0, 100, 0, 255); // convert fan power to percentage (0_100%)
int tec = 11; // N -Channel Mosfet connected to digital pin 11
int powertec = 0; // TEC power level from 0 to 100%
int tec_level = map(powertec, 0, 100, 0, 255); // convert TEC power to percentage(0_100%)

int temp=DHT.temperature;
int val; // value of the pressed key on keypad shield 

byte degree[8] =  // Create (') character before ("C") temperature unit
              {
                0b00011,
                0b00011,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000
              };

 
 void setup() {
  // put your setup code here, to run once:
pinMode(fan, OUTPUT);
pinMode(tec, OUTPUT);
lcd.begin(16,2);
lcd.createChar(1, degree);
lcd.setCursor(0,0);
lcd.print("PAINT SAFE");
lcd.setCursor(0,1);
lcd.print("  by SAMER");
delay(1000);
lcd.clear();

}
byte key(){ // sel - 637 , lef - 405, dow - 253, up - 97, righ - 0
 int val = analogRead(0);
  if (val < 50 ) return 5;
  else if (val < 150 ) return 4;
  else if (val < 300) return 3;
  else if (val < 500 ) return 2;
  else if (val < 750) return 1;
  else if ( val <= 1023) return 0;
  
}

void loop() {

  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Fan:");
  lcd.print(powerfan);
  lcd.print("%");
  lcd.setCursor(0,1);
  lcd.print("COOL:");
  lcd.print(powertec);
  lcd.print("%");
  lcd.setCursor( 10,0);
  lcd.print("H:");
  lcd.print(DHT.humidity,0);
  lcd.print(" %");
  lcd.setCursor(10,1);
  lcd.print("T:");
  lcd.print(DHT.temperature, 0);
  lcd.write(1);
  lcd.print("C");
  delay(250);
  
  
  
     
     

    switch (key()){
     case 5:
      powertec += 10; //increase tec power by 10 %
      
      
      break;
     case 4:
      powerfan += 10; // increase fan power by 10 %
      
      
      
      break;
     case 3:
     powerfan -= 10; // decrease fan power by 10 %
     
     
     
     break;
    case 2:
     powertec -= 10; // decrease tec by 10 %
      break;
     
     case 1:

lcd.clear();
lcd.print ("AUTO");
lcd.setCursor(10,0);
lcd.print("T:");
lcd.print(DHT.temperature,0);
lcd.setCursor(10,1);
lcd.print(DHT.humidity,0);
lcd.print("%");


if (DHT.temperature <=23) {
  powerfan -=100;
  powertec -=100;
}
     else if (DHT.temperature > 23) {
      powerfan +=100;
      powertec +=100;
     }
lcd.setCursor(0,0);
lcd.print("AUTO");
lcd.setCursor(0,1);
lcd.print("       ");
 
delay(2000);
break;
     
     
     
    }
     if(powerfan > 100) powerfan =100;
     if(powerfan < 0) powerfan =0;
     fan_level = map(powerfan, 0, 100, 0, 255);

     if (powertec > 100) powertec = 100;
     if (powertec <0) powertec = 0;
     tec_level = map(powertec,0 ,100, 0, 200);
    
     analogWrite(fan, fan_level);
     analogWrite(tec, tec_level);
  delay(50);

  DHT.read11(dht_apin);
  
  // put your main code here, to run repeatedly:

}
>

Thank you Kind people !!

Hello everyone ! ,

I’ve tried to complete my system, but unfortunately newer problem occurred, in fact im trying to
control Peltier based fridge usig a TEC , Fans, sinc and DHT 11 sensor, The purpose is to control and display this system via LCD keypad Shield in 2 schedules:

1st - To control manually by increasing the power of TEC and FAN through pwm pins

2nd - Automatic switch based on entry level number

with my sketch i could control it manually and display it in fine way using the " switch…case"

commands to determine the pressed key and to do action ( increase or decrease power), but when it comes to include the automatic sketch in the 5-th case (or button) , it didnt work perfectly , and i think the problem is in how to save this mode ( i included (delay) the auto mode worked just when pressing the key, worked during that delay and i had to push the button each time after that delay ends to make auto mode works);

so please any suggestions and solutions how to save the auto mode , also how to make adjustable auto mode power number or temperature reference ?

This is my sktech :

#include <dht.h>                       // include DHT library

#include <LiquidCrystal.h>        // Include LCD 16 x2 Library
#define dht_apin A1                // Analog Pin sensor is connected to
LiquidCrystal lcd(8,9,4,5,6,7); // introduce LCD digital pins
 
 dht DHT;
 
 int fan= 3; //The N-Channel MOSFET connected to fan 
                 //is on digital pin 3

int powerfan= 0;   //fan Power level fro 0 to 99%

int fan_level = map(powerfan, 0, 100, 0, 255);       // convert fan power to percentage (0_100%)

int tec = 11;     // N -Channel Mosfet connected to digital pin 11

int powertec = 0;   // TEC power level from 0 to 100%

int tec_level = map(powertec, 0, 100, 0, 255);  // convert TEC power to percentage(0_100%)

int temp=DHT.temperature;

int val; // value of the pressed key on keypad shield 

byte degree[8] =         // Create (') character before ("C") temperature unit
              {
                0b00011,
                0b00011,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000
              };

 
 void setup() {
           // put your setup code here, to run once:
pinMode(fan, OUTPUT);
pinMode(tec, OUTPUT);
lcd.begin(16,2);
lcd.createChar(1, degree);
lcd.setCursor(0,0);
lcd.print("PAINT SAFE");
lcd.setCursor(0,1);
lcd.print("  by SAMER");
delay(1000);
lcd.clear();

}
byte key(){ // sel - 637 , lef - 405, dow - 253, up - 97, righ - 0
 int val = analogRead(0);
  if (val < 50 ) return 5;
  else if (val < 150 ) return 4;
  else if (val < 300) return 3;
  else if (val < 500 ) return 2;
  else if (val < 750) return 1;
  else if ( val <= 1023) return 0;
  
}

void loop() {

  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Fan:");
  lcd.print(powerfan);
  lcd.print("%");
  lcd.setCursor(0,1);
  lcd.print("COOL:");
  lcd.print(powertec);
  lcd.print("%");
  lcd.setCursor( 10,0);
  lcd.print("H:");
  lcd.print(DHT.humidity,0);
  lcd.print(" %");
  lcd.setCursor(10,1);
  lcd.print("T:");
  lcd.print(DHT.temperature, 0);
  lcd.write(1);
  lcd.print("C");
  delay(250);
  
  
  
     
     

    switch (key()){
     case 5:
      powertec += 10;  //increase tec power by 10 %
      
      
      break;
     case 4:
      powerfan += 10;  // increase fan power by 10 %
      
      
      
      break;
     case 3:
     powerfan -= 10;  // decrease fan power by 10 %
     
     
     
     break;
    case 2:
     powertec -= 10;   // decrease tec by 10 %
      break;
     
     case 1:

lcd.clear();
lcd.print ("AUTO");
lcd.setCursor(10,0);
lcd.print("T:");
lcd.print(DHT.temperature,0);
lcd.setCursor(10,1);
lcd.print(DHT.humidity,0);
lcd.print("%");


if (DHT.temperature <=23) {
  powerfan -=100;
  powertec -=100;
}
     else if (DHT.temperature > 23) {
      powerfan +=100;
      powertec +=100;
     }
lcd.setCursor(0,0);
lcd.print("AUTO");
lcd.setCursor(0,1);
lcd.print("       ");
 
delay(2000);
break;
     
     
     
    }
     if(powerfan > 100) powerfan =100;
     if(powerfan < 0) powerfan =0;
     fan_level = map(powerfan, 0, 100, 0, 255);

     if (powertec > 100) powertec = 100;
     if (powertec <0) powertec = 0;
     tec_level = map(powertec,0 ,100, 0, 200);
    
     analogWrite(fan, fan_level);
     analogWrite(tec, tec_level);
  delay(50);

  DHT.read11(dht_apin);
  
  // put your main code here, to run repeatedly:

}

Please re-format your Post to get rid of the unnecessary blank lines. I literally cannot make sense of it at the moment.

Also, use the AutoFormat tool to lay your code out properly for easier reading.

Why have you chunks of lcd.print() stuff in three different places in your program. If you just have one function that does all the lcd stuff it will be easier to maintain and debug.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking.

...R

sam0202: to determine the pressed key and to do action ( increase or decrease power), but when it comes to include the automatic sketch in the 5-th case (or button) , it didnt work perfectly

You cannot use "delay()" commands (which means "block every program execution") in your sketch, if you want to create an interactive program that responds to a pressed key at any time a key is pressed.

A call of "delay(2000)" within the loop would mean: - you have to press the key up to two seconds, or the press might not be realized - the reaction to a pressed key may be 2 seconds delayed

If your program has to do several tasks, some of them running all the time (check for pressed button), some of them running 4 times per second and some of them running perhaps once every 2 seconds, then you have to use just "millis()" to detect the time to start your "rare actions", but you have to check your button all the time - without using "delay()" function calls.

Thank you Robin2 for your note i didnt noticed the format mal propre , in fact im new in arduino programming and dont know that is possible to replay lcd.print() with other function :$!!

please what is the function that i could replace ?

im now rading your link about millis()

thank you !! its very helpful !!

Cross-posting is a short-cut to the sin-bin

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.

sam0202: dont know that is possible to replay lcd.print() with other function :$!!

please what is the function that i could replace ?

I did not say that you could replace lcd.print() with another function. I suggested that you put all the code for the LCD into a function that you create so that you don't have a spaghetti tangle of code.

Have a look at Planning and Implementing a Program

...R

maybe something like this. I haven’t compiled as I don’t have the same library’s.

I also added some notes

#include <dht.h>                       // include DHT library

#include <LiquidCrystal.h>        // Include LCD 16 x2 Library
#define dht_apin A1                // Analog Pin sensor is connected to
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // introduce LCD digital pins

dht DHT;

int fan = 3; //The N-Channel MOSFET connected to fan
//is on digital pin 3

int powerfan = 0;  //fan Power level fro 0 to 99%

int fan_level = map(powerfan, 0, 100, 0, 255);       // convert fan power to percentage (0_100%)

int tec = 11;     // N -Channel Mosfet connected to digital pin 11

int powertec = 0;   // TEC power level from 0 to 100%

int tec_level = map(powertec, 0, 100, 0, 255);  // convert TEC power to percentage(0_100%)

int temp = DHT.temperature;

int val; // value of the pressed key on keypad shield

unsigned long previousMillis = 0;
unsigned long interval = 1000;
byte  automode = 0;

byte degree[8] =         // Create (') character before ("C") temperature unit
{
  0b00011,
  0b00011,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};


void setup() {
  // put your setup code here, to run once:
  pinMode(fan, OUTPUT);
  pinMode(tec, OUTPUT);
  lcd.begin(16, 2);
  lcd.createChar(1, degree);
  lcd.setCursor(0, 0);
  lcd.print("PAINT SAFE");
  lcd.setCursor(0, 1);
  lcd.print("  by SAMER");
  delay(1000);
  lcd.clear();

}
byte key() { // sel - 637 , lef - 405, dow - 253, up - 97, righ - 0
  int val = analogRead(0);
  if (val < 50 ) return 5;
  else if (val < 150 ) return 4;
  else if (val < 300) return 3;
  else if (val < 500 ) return 2;
  else if (val < 750) return 1;
  else if ( val <= 1023) return 0;

}

void loop() {

  unsigned long currentMillis = millis();
  DHT.read11(dht_apin);

  //need to look at state change so one button press equals
  //one addition or subtraction

  switch (key()) {
    case 5:
      automode = 0;
      powertec += 10;  //increase tec power by 10 %
      break;
    case 4:
      automode = 0;
      powerfan += 10;  // increase fan power by 10 %
      break;
    case 3:
      automode = 0;
      powerfan -= 10;  // decrease fan power by 10 %
      break;
    case 2:
      automode = 0;
      powertec -= 10;   // decrease tec by 10 %
      break;
    case 1:
      automode = 1;
      break;
  }
  if (currentMillis - previousMillis >= interval) {
    if (automode == 0) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Fan:");
      lcd.print(powerfan);
      lcd.print("%");
      lcd.setCursor(0, 1);
      lcd.print("COOL:");
      lcd.print(powertec);
      lcd.print("%");
      lcd.setCursor( 10, 0);
      lcd.print("H:");
      lcd.print(DHT.humidity, 0);
      lcd.print(" %");
      lcd.setCursor(10, 1);
      lcd.print("T:");
      lcd.print(DHT.temperature, 0);
      lcd.write(1);
      lcd.print("C");
      delay(250);
    }
    else
    {
      lcd.clear();
      lcd.print ("AUTO");
      lcd.setCursor(10, 0);
      lcd.print("T:");
      lcd.print(DHT.temperature, 0);
      lcd.setCursor(10, 1);
      lcd.print(DHT.humidity, 0);
      lcd.print("%");

      // in my experience this wont work on a peltier as they are very slow and do not
      //work linier

      // if you shut a peltier down it will transfer heat back so you want to leave it
      //running at a very low level

      //better to see how far you are from set point then 100% until 0.5 degrees then
      // 60% until 0.2 degrees then 10% once set point is reached.
      // the movements should be smoothed out rather than 100 to 60 or it will mess with
      //the dht11

      if (DHT.temperature <= 23) {
        powerfan -= 3;
        powertec -= 3;
      }
      else if (DHT.temperature > 23) {
        powerfan += 3;
        powertec += 3;
      }
      lcd.setCursor(0, 0);
      lcd.print("AUTO");
      lcd.setCursor(0, 1);
      lcd.print("       ");

    }
    digitalWrite(ledPin, ledState);
  }

  //constrain would be better code than "if"
  // i wouldnt take the fan lower than 20% to avoid motor stall

  if (powerfan > 100) powerfan = 100;
  if (powerfan < 0) powerfan = 0;
  fan_level = map(powerfan, 0, 100, 0, 255);

  if (powertec > 100) powertec = 100;
  if (powertec < 0) powertec = 0;
  tec_level = map(powertec, 0 , 100, 0, 200);

  analogWrite(fan, fan_level);
  analogWrite(tec, tec_level);

}