Slight bug in my code?

Hi all, I cobbled together a 328p on strip board, wired a lcd display and connected some DHT22's up (in/out) earlier in the year.

Works nicely, but the weather has just dipped here and dropping below freezing. I'm noticing my LCD is telling me it is -0 ouside -1 yes but -0? is there such a thing? I would like to get rid of the - symbol ideally?

//parameters
const int minLight = 40;        // at or below this light level, use minimum backlight intensity
const int maxLight = 900;        // at or above this light level, use maximum backlight intensity
const int minBacklight = 30;      // lowest backlight intensity to use
const int maxBacklight = 255;    // highest backlight intensity to use


const int LDRPin = A1;      // Analog input pin that the lDR is attached to
const int LCDBackLight = 3;      // Analog (PWM) output pin that the BACKLIGHT is attached to

int LDRRead = 0;         // value read from the LDR
int outputValue = 0;     // value output to the PWM (analog out)

// make some custom characters:
byte rh[8] = {
  0b00101,
  0b00111,
  0b10101,
  0b01000,
  0b00100,
  0b11010,
  0b10001,
  0b10000
};

byte degree[8] = {
  0b00111,
  0b00101,
  0b00111,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

byte c[8] = {
  0b00000,
  0b00000,
  0b00000,
  0b11100,
  0b10000,
  0b10000,
  0b10000,
  0b11100
};




#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTAPIN 5     // what pin we're connected to in
#define DHTBPIN 6     // what pin we're connected to out


// 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_in(DHTAPIN, DHTTYPE);
DHT dht_out(DHTBPIN, DHTTYPE);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


void setup() {

  lcd.begin(16, 2);
  delay(100);

  dht_in.begin();
  dht_out.begin();

  pinMode(3, OUTPUT);   //LCD BACKLIGHT

    // create new characters
  lcd.createChar(0, rh);
  lcd.createChar(1, degree);
  lcd.createChar(2, c);


}

void loop() {
  // LDR controlled LCD backlight via PWM part
  int sum = 0;
  for (int i=0; i<16; i++) // take 16 samples from the analog input
  {
    sum += analogRead(LDRPin);
  }
  LDRRead = sum/16; // divide by the amount of samples for the average value

  // map it to the range[0..255] of the analog output that the LCD backlight it connected to
  outputValue = LDRRead/4;   // alternative to map function for scaling down from analog input
  outputValue = map(constrain(LDRRead, minLight, maxLight), minLight, maxLight, minBacklight, maxBacklight );

  // change the analog out value to adjust LCD backlight intensity
  analogWrite(LCDBackLight, outputValue);   





  // 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_in = dht_in.readHumidity();
  float t_in = dht_in.readTemperature();

  float h_out = dht_out.readHumidity();
  float t_out = dht_out.readTemperature();

  //Inside readings
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t_in) || isnan(h_in)) {

    lcd.setCursor(0, 0);
    lcd.print(" No connection  ");
  } 
  else {

    //write temp data
    lcd.setCursor(0, 0);
    lcd.print("In : ");
    lcd.print(t_in, 0);
    lcd.write((uint8_t)1);
    lcd.write((uint8_t)2);
    lcd.print(" ");

    //write humidity data
    lcd.print(h_in, 0);
    lcd.print("% ");
    lcd.write((uint8_t)0);
    lcd.print(" ");

  }

  //Outside readings
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t_out) || isnan(h_out)) {

    lcd.setCursor(0, 1);
    lcd.print(" No connection  ");
  } 
  else {

    //write temp data
    lcd.setCursor(0, 1);
    lcd.print("Out: ");
    lcd.print(t_out, 0);
    lcd.write((uint8_t)1);
    lcd.write((uint8_t)2);
    lcd.print(" ");

    //write humidity data
    lcd.print(h_out, 0);
    lcd.print("% ");
    lcd.write((uint8_t)0);
    lcd.print(" ");
  }



}

Hi dtokez,

Could be a rounding or truncation error. Check for anything between 0 and -1 and report 0.

lcd.write((uint8_t)0);

in this line is there really need of (uint8_t).

lcd.write(0);

lcd.print(t_in, 0);

should be lcd.print(t_in, 1);

the -0 you see is for example -0.2 so truncating the output to zero decimals has the effect of -0

If you do not want the decimals convert the float to an int

lcd.print(int(t_in));

Thanks all for the help, and thanks Rob for the explanation

Only trouble is its +5 above freezing tonight so I will have to wait and see if it works :slight_smile:

Fake it - either set t_in to -0.2 or subtract 5.0 from it when you display it.