Newbie in trouble! :)

Hi guys, i´m new in this Arduino world, and i´m having a issue, that i am not able to solve by my self, maybe you can help:

I have this code here:

#include <LiquidCrystal.h>
#include <dht_nonblocking.h>

#define VELOCIDAD 300
#define DHT_SENSOR_TYPE DHT_TYPE_11

LiquidCrystal lcd(12,11,5,4,3,2);

const int Bombapin =10;
const int Waterpin =A0;
int thresholdValue = 800;
const float temperature;
const float humidity;


static const int DHTpin = 6;
DHT_nonblocking dht_sensor( DHTpin, DHT_SENSOR_TYPE );


//Texto
String texto_humedad = "Humedad incorrecta, activando riego";
String texto_optimo = "Humedad de tierra optima";




void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(Bombapin, OUTPUT);
  
}
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }
 return( false );
}


void loop() {
  int tam_texto1=texto_humedad.length();
  int tam_texto2=texto_optimo.length();

  int sensorValue = analogRead(Waterpin);

  if( measure_environment( &temperature, &humidity ) == true )
  {
  Serial.print("\nt = ");
  Serial.print(temperature, 1);
  Serial.print(" - H = ");
  Serial.print(humidity,1);
  Serial.print("%");
  Serial.print("dentro");
  }
   
  Serial.print("fuera");
  lcd.setCursor(0,0);
  lcd.print("Hum Tierr:");
  lcd.print(sensorValue);

I have the function: “measure_environment” is a boolean, so nothing complicated…

The thing is that in the void loop i have a condition to print “dentro” =in and “fuera”=out, it always prints out…that means that the function is returning a false value…the interesting part is that when i only have this part of code, this works just fine…anyone could please help me?

Topic moved here where it is more appropriate

Can I suggest that you change title to English

Your code is incomplete

UKHeliBob:
Topic moved here where it is more appropriate

Can I suggest that you change title to English

Sorry, It is corrected now...it is the habit!

Do you use this library ? GitHub - olewolf/DHT_nonblocking: Non-blocking Arduino library for the DHT11, DHT21, and DHT22 sensors.
It has only one example: DHT_nonblocking.ino.

In that example, you see that 'temperature' and 'humidity' are local variables in the loop().
I suggest that you do the same.
You have made them global and also 'const'. The 'const' keyword tells the compiler that the variable is a constant and will never change.

Can you add a delay(200); at the end of the loop(). You are sending far to many "fuera" messages to the serial port.
Why would you send "fuera" so often to the serial port ? You can prevent such problems by making the text of the code look better. Press Ctrl+T in the Arduino IDE and then put every space, every comma, every indent, every new line at the right place. The perfect sketch shows its structure of the code with a single glance at the code.

What is your programming experience and in which language ?

Can I suggest that you change the title of the topic to something meaningful

it always prints out...

No surprise there because it is printed unconditionally

  if ( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print("\nt = ");
    Serial.print(temperature, 1);
    Serial.print(" - H = ");
    Serial.print(humidity, 1);
    Serial.print("%");
    Serial.print("dentro");
  }
  Serial.print("fuera");
etc, etc

Koepel:
Do you use this library ? GitHub - olewolf/DHT_nonblocking: Non-blocking Arduino library for the DHT11, DHT21, and DHT22 sensors.
It has only one example: DHT_nonblocking.ino.

In that example, you see that 'temperature' and 'humidity' are local variables in the loop().
I suggest that you do the same.
You have made them global and also 'const'. The 'const' keyword tells the compiler that the variable is a constant and will never change.

Can you add a delay(200); at the end of the loop(). You are sending far to many "fuera" messages to the serial port.
Why would you send "fuera" so often to the serial port ? You can prevent such problems by making the text of the code look better. Press Ctrl+T in the Arduino IDE and then put every space, every comma, every indent, every new line at the right place. The perfect sketch shows its structure of the code with a single glance at the code.

What is your programming experience and in which language ?

Hi, thanks for your reply!,
I had also humidity, and temperature as locals, but same result, that is why i change them, just to see if does change anything. But you are right i will put them just as it was.
actually i send the messages "fuera"& "dentro" just to see if it gets printed to verify where the code reads...since we don´t have break points or any other tool to follow the coding...

UKHeliBob:
Can I suggest that you change the title of the topic to something meaningful

it always prints out...

No surprise there because it is printed unconditionally

  if ( measure_environment( &temperature, &humidity ) == true )

{
   Serial.print("\nt = “);
   Serial.print(temperature, 1);
   Serial.print(” - H = “);
   Serial.print(humidity, 1);
   Serial.print(”%");
   Serial.print(“dentro”);
 }
 Serial.print(“fuera”);
etc, etc

Actually this is the problem… it does NOT get printed although it should, it is like if the function " measure_environment" returns false, but the DHT11 works, since i can run without any problems the example of this sensor.
Maybe it helps if i post the whole code that i have, is not that much:

#include <LiquidCrystal.h>
#include <dht_nonblocking.h>


#define DHT_SENSOR_TYPE DHT_TYPE_11
#define VELOCIDAD 300 

LiquidCrystal lcd(12,11,5,4,3,2);

const int Bombapin =10;
const int Waterpin =A0;
int thresholdValue = 800;
const float temperature;
const float humidity;

static const int DHTpin = 6;
DHT_nonblocking dht_sensor( DHTpin, DHT_SENSOR_TYPE );


//Texto
String texto_humedad = "Humedad incorrecta, activando riego";
String texto_optimo = "Humedad de tierra optima";


void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(Bombapin, OUTPUT);
}


static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }
 return( false );
}

void loop() {

  int tam_texto1=texto_humedad.length();
  int tam_texto2=texto_optimo.length();
  int sensorValue = analogRead(Waterpin);
  if( measure_environment( &temperature, &humidity ) == true )
  {
  Serial.print("\nt = ");
  Serial.print(temperature, 1);
  Serial.print(" - H = ");
  Serial.print(humidity,1);
  Serial.print("%");
  Serial.print("dentro");
  }
  Serial.print("fuera");
  lcd.setCursor(0,0);
  lcd.print("Hum Tierr:");
  lcd.print(sensorValue);
  delay(600);
  if(sensorValue > thresholdValue){
    digitalWrite(Bombapin,HIGH);
    lcd.clear();
     for(int i=16;i>=1;i--){
      lcd.clear();
      lcd.setCursor(i, 0);
      lcd.print(texto_humedad);
      delay(VELOCIDAD);
  } 
    for(int i=1; i<=tam_texto1 ; i++){
      String texto = texto_humedad.substring(i-1);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(texto);
      delay(VELOCIDAD);
  }
    //lcd.print("Tierra seca, regando"); 
    // delay(5000);
  }else{
   // lcd.print(" - Humedad de tierra optima\n");
   // delay(10000);
    digitalWrite(Bombapin,LOW);
     for(int i=16;i>=1;i--){
    lcd.clear();
    lcd.setCursor(i, 0);
    lcd.print(texto_optimo);
    delay(VELOCIDAD);
  }
  for(int i=1; i<=tam_texto1 ; i++){
    String texto = texto_optimo.substring(i-1);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(texto);
    delay(VELOCIDAD);
  }
   }
}

Actually this is the problem... it does NOT get printed although it should

Are you saying that your code never prints the word fuera on the Serial monitor ?

UKHeliBob:
Are you saying that your code never prints the word fuera on the Serial monitor ?

The word "dentro" does not get printed, "fuera" yes...i assume then that this condition

if( measure_environment( &temperature, &humidity ) == true )

returns false... and i don´t get it...

You have more than one variable named temperature in your sketch. Could that be a problem ?

For instance, in this function call

  if ( measure_environment( &temperature, &humidity ) == true )

what is the value of temperature ?

UKHeliBob:
You have more than one variable named temperature in your sketch. Could that be a problem ?

For instance, in this function call

  if ( measure_environment( &temperature, &humidity ) == true )

what is the value of temperature ?

This is how it is done in the example that is given in the arduino SW for the DTH11... and when i run this example by itself, it works just fine...

Please post the example code that you are referring to

UKHeliBob:
Please post the example code that you are referring to

#include <dht_nonblocking.h>

/* Uncomment according to your sensortype. */
#define DHT_SENSOR_TYPE DHT_TYPE_11
//#define DHT_SENSOR_TYPE DHT_TYPE_21
//#define DHT_SENSOR_TYPE DHT_TYPE_22

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );



/*
 * Initialize the serial port.
 */
void setup( )
{
  Serial.begin( 9600);
}



/*
 * Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.
 */
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}



/*
 * Main program loop.
 */
void loop( )
{
  float temperature;
  float humidity;

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
  }
}

In your code

const float temperature;

A global variable named temperature with a constant value of zero

In the example code

void loop( )
{
  float temperature;

A variable named temperature local to the loop() function

Not the same thing at all

Please answer the question posed in reply #10
Try printing the value of temperature before the function call

UKHeliBob:
In your code

const float temperature;

A global variable named temperature with a constant value of zero

In the example code

void loop( )

{
 float temperature;



A variable named temperature local to the loop() function

Not the same thing at all

Please answer the question posed in reply #10
Try printing the value of temperature before the function call

Hi!, i solved the problem changing the library to this one: #include "DHT.h", and using this if instead...

"if(isnan(h)|| isnan(t)){
    Serial.print("Error al leer");
    return;
  }"

elprims:
Hi!, i solved the problem changing the library to this one: #include "DHT.h", and using this if instead...

"if(isnan(h)|| isnan(t)){

Serial.print("Error al leer");
   return;
 }"

The const values i create them just to see if it at least returned me a 0 value, this is debug code.
When i tried to print the values before the if as you suggested, they would not get printed..
Now it seems to work fine with the changed library.
Thanks for your help and time! :slight_smile:

Have you got rid of the duplicate variables ?

UKHeliBob:
Have you got rid of the duplicate variables ?

I don¨t know which ones you´re talking about...
In my code there are only two variables for the DTH11 humidity and temperature, maybe you´re talking about the const values...yes they are gone!

maybe you´re talking about the const values...yes they are gone!

Those were the ones that I meant as they were causing a problem as previously pointed out