Code NOT Working

Hello,
This is my code It will NOT work having error messages:

#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN 8
#define DHTTYPE DHT11
void setup() {
  pinMode(7, OUTPUT);// connected to S terminal of Relay
  DHT dht(DHTPIN, DHTTYPE);
dht.begin();
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
lcd.print("Temp:  Humidity:");
lcd.setCursor(0,1);
  float h = dht.readHumidity();
  float t = dht.readTemperature(false);
   if (isnan(h))  (isnan(t));
    lcd.print("ERROR");
    return;
   lcd.print(t);
  lcd.setCursor(7,1);
  lcd.print(h); 
  
}
void loop() {
  int T_threshold = 25;
T_threshold = T_threshold + 1;
digitalWrite(7,HIGH);// turn relay ON
}
int T_threshold = 25;
T_threshold = T_threshold - 1;
digitalWrite(7, LOW);// turn relay OFF
}
}  /code]

This is the error message:

Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

error: 'T_threshold' does not name a type
 T_threshold = T_threshold - 1;
 ^
error: expected constructor, destructor, or type conversion before '(' token
 digitalWrite(7, LOW);// turn relay OFF
             ^
error: expected declaration before '}' token
 }
 ^
Multiple libraries were found for "DHT.h"

Thank You
Natha

That first } after you open loop() with the { closes loop() so what follows is not in any function.

Can we assume you only want to use the LCD and DHT inside setup()?

int T_threshold = 25;
T_threshold = T_threshold - 1;
digitalWrite(7, LOW);// turn relay OFF

The second time this code appears in the program it is outside of a function

Delete the duplicate code then look carefully at where the loop() function ends

And when you fix that it will moan about redeclaring T_threshold so lose the "int" on the second one.

So many problems. So little time !

Thank You Everyone:
Every thing works accept the Lcd Screen

Thank You

Natha

Hello
Can anyone help me?

Only LCD that dose not work.

Thank You

Natha

Only LCD that dose not work.

Did you try the test sketches that will be with that LCD library, just to check that the LCD is ok?

  • If not, do :wink:
  • If you did, and it works, make sure you take all the appropriate lines into your DHT sketch.
  • If you did, and it doesn't work, report back and let's try get just the LCD sorted by itself.

Thank You
The lcd screen works but the screen dose not.

#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN 8
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
dht.begin();
lcd.print("Temp:  Humidity:");
lcd.setCursor(0,1);
float h = dht.readHumidity();
float t = dht.readTemperature(false);
void setup() {
  pinMode(7, OUTPUT);// connected to S terminal of Relay
  


  
   if (isnan(h))  (isnan(t));
    lcd.print("ERROR");
    return;
   lcd.print(t);
  lcd.setCursor(7,1);
  lcd.print(h); 
  
}
void loop() {
  int T_threshold = 25;
T_threshold = T_threshold + 1;{
digitalWrite(7,HIGH);// turn relay ON

T_threshold = T_threshold + 1;{
digitalWrite(7,LOW);// turn relay ON
}
}
}

This is the code.

Thank You

Natha

dht.begin();
lcd.print("Temp:  Humidity:");
lcd.setCursor(0,1);

That code needs to be in a function. I’m surprised that compiled.

You probably also need an lcd.begin somewhere in setup before you try to use the lcd.

Natha:
The lcd screen works but the screen dose not.

What does that mean?

meltDown:
What does that mean?

Sounds like the screen is working and not working at the same time.

srnet:
Sounds like the screen is working and not working at the same time.

Schrodinger's screen.

Nah, just a small dose of the screen isn't working.

Here is your code with a lot of comments added to highlight the problems. Don’t have the hardware here to actually try it, but it should be closer to working.

#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN 8
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

//dht.begin(); //should be in setup
//lcd.print("Temp:  Humidity:"); //should be in setup
//lcd.setCursor(0, 1); //should be in setup

//need to declare the variables here, then set equal to the dht readings in your code
//float h = dht.readHumidity();
//float t = dht.readTemperature(false);
float h;
float t;

void setup() {
  pinMode(7, OUTPUT);// connected to S terminal of Relay

  dht.begin(); //moved from above
  lcd.print("Temp:  Humidity:"); //moved from above
  lcd.setCursor(0, 1); //moved from above

  h = dht.readHumidity(); //moved from above
  t = dht.readTemperature(); //moved from above

  //if (isnan(h))  (isnan(t)); //this statement says "if h is not a valid number, then see if t is a valid number, then do nothing based on the answer"
  if ( isnan(h) || isnan(t) ) { //this statesent says "if h OR t is not a valid number, then display "ERROR" and skip the rest of setup"
    lcd.print("ERROR");
    return;
  }
  lcd.print(t);
  lcd.setCursor(7, 1);
  lcd.print(h);

}

void loop() {
  int T_threshold = 25; //create a variable called T_threshold and set it to 25
  T_threshold = T_threshold + 1; { //add 1 to T_threshold, will always equal 26 because T_threshold is set to 25 every time loop is executed
                                   // the "{" is useless in this context, just groups together the following lines for no apparent reason
    digitalWrite(7, HIGH); // turn relay ON 

    T_threshold = T_threshold + 1; { //add 1 to T_threshold, will always equal 27
                                     // the "{" is useless in this context, just encloses the following line for no apparent reason
      digitalWrite(7, LOW); // turn relay ON //is this ON or OFF?  both digitalWrite statments can't be turning relay ON 
    }
  }
}

Thank You
I will check if that works.
Thank You for your help

Natha

Thank You for your help.
This is the code now.

#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTPIN 8
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
dht.begin();
lcd.print("Temp:  Humidity:");
lcd.setCursor(0,1);

void setup() {
  pinMode(7, OUTPUT);// connected to S terminal of Relay
  

lcd.begin();
  float h = dht.readHumidity();
float t = dht.readTemperature(false);
   if (isnan(h))  (isnan(t));
    lcd.print("ERROR");
    return;
   lcd.print(t);
  lcd.setCursor(7,1);
  lcd.print(h); 
  
}
void loop() {
  int T_threshold = 25;
T_threshold = T_threshold + 1;{
digitalWrite(7,HIGH);// turn relay ON

T_threshold = T_threshold + 1;{
digitalWrite(7,LOW);// turn relay ON
}
}
}/code]

Natha

Thank You it works now.

Thank You

Natha