Pages: [1]   Go Down
Author Topic: Slight bug in my code?  (Read 530 times)
0 Members and 1 Guest are viewing this topic.
South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 585
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
//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(" ");
  }



}







« Last Edit: December 05, 2012, 09:01:27 pm by dtokez » Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi dtokez,

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

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
lcd.write((uint8_t)0);

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

Code:
lcd.write(0);
Logged

From Idea To Invention

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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));

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 585
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: