Not declared in the scope

Hello all,

I do not understand why I get this error message:

C:\Users\mateusz.winter\Desktop\Beehive_data\Beehive_data.ino: In function 'void loop()':

Beehive_data:248:6: error: 'h' was not declared in this scope

 if ( h > 60 ) // If the humidity if greater than 60%

      ^

Multiple libraries were found for "DHT.h"
 Used: C:\Program Files (x86)\Arduino\libraries\DHT_sensor_library
 Not used: C:\Program Files (x86)\Arduino\libraries\DHT-sensor-library-master
exit status 1
'h' was not declared in this scope

Probably because you did not define h.

But your code is secret, so I can only guess.

The error is pretty self explaining I would say. H isn't defined, at least not in the scope you try to use it.

Plus for posting the whole error.
But fat minus for not posting the actual code...

/*
 * File:   main.c
 * Company: RIOU Glass
 * Author: Mateusz WINTER
 * Project: This project is to monitor temperature and humidity
 * in a beehive by logging and saving data on SD card
 * 
 */

// Configuration for DHT Sensor

 #include <DHT.h>

 #define DHTPIN 2

 #define DHTTYPE DHT11

 DHT dht(DHTPIN, DHTTYPE);

 // Configuration for RTC Module

// DS1302 RST  -> Arduino Digital 7
// DS1302 DATA -> Arduino Digital 6
// DS1302 CLK  -> Arduino Digital 5

#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6, 7, 8);


// Configuration SD card module

// SD card attached to SPI bus as follows:

// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 4 

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

// Temperature sensors

float Temp_1;
float Temp_2;

// Delay

//unsigned long seconds = 1000L; // 1000ms to 1s
//unsigned long minutes = seconds * 900; // 900s in 15 minutes

const unsigned long interval = 60000 ; // 90 minutes is 900000
unsigned long previous = 0 ;
const unsigned long intervala = 1000 ;
unsigned long previousa = 0 ;


// Fan and heater

const int HEATER = 10; //Red LED turn on heater
const int FAN = 9; //Blue LED turn on fan

void setup () {
  Serial.begin(9600); // Baud rate of 9600
  pinMode(chipSelect, OUTPUT);



// Heater and fan

  pinMode(HEATER,OUTPUT);
  pinMode(FAN,OUTPUT);
  digitalWrite(HEATER,LOW);
  digitalWrite(FAN,LOW);

// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(00, 17, 11, 6, 25, 06, 2019);
  
  Serial.println(F("Beehive data logger"));
  Serial.println();
  
  while (!Serial) {
  }
  Serial.print("Starting SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed to start or not present");
    while (1);
  }
  Serial.println("   Card is now operational!");
  Serial.println();
  dht.begin();
  
  File dataFile = SD.open("testing.txt", FILE_WRITE);
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on serial terminal
  
  delay(100);
  Serial.print("Date      "); // Print header name for column
  Serial.print(" \t");        // Create tab/space
  Serial.print("Time      "); // Print header name for column
  Serial.print(" \t");        // Create tab/space 
  Serial.print("Humidity in %"); // Print header name for column  
  Serial.print(" \t");  // Create tab/space   
  Serial.print("Temp_DHT in °C"); // Print header name for column  
  Serial.print(" \t"); // Create tab/space
  Serial.print("Temp_1 in °C"); // Print header name for column
  Serial.print(" \t"); // Create tab/space
  Serial.print("Temp_2 in °C"); // Print header name for column

  Serial.println();  // Insert a new line  
  
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on SD card
  
  dataFile.print("Date      "); // Print header name for column
  dataFile.print(" \t");        // Create tab/space
  dataFile.print("Time      "); // Print header name for column
  dataFile.print(" \t");        // Create tab/space
  dataFile.print("Humidity in %"); // Print header name for column
  dataFile.print(" \t"); // Create tab/space      
  dataFile.print("Temp_DHT in °C"); // Print header name for column  
  dataFile.print(" \t");  // Create tab/space
  dataFile.print("Temp_1 in °C"); // Print header name for column
  dataFile.print(" \t"); // Create tab/space
  dataFile.print("Temp_2 in °C"); // Print header name for column

  dataFile.println();  // Insert a new line 
  dataFile.close();
  
}

//}

void loop() {
  
  unsigned long current = millis();
  unsigned long currenta = millis();

  if(current - previous >= interval) {
  File dataFile = SD.open("testing.txt", FILE_WRITE);
  // This allows for the update of variables for time or accessing the individual elements.                //
  myRTC.updateTime();   
  // Read the humidity of the DHT11
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();

  Temp_1 = analogRead(A0)*5/1023.0; // Read value from analog port A0 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_1 = Temp_1 - 0.5; 
  Temp_1 = Temp_1 / 0.01;
  Temp_1 = Temp_1 - 2.0;
  Temp_2 = analogRead(A1)*5/1023.0; // Read value from analog port A1 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_2 = Temp_2 - 0.5;
  Temp_2 = Temp_2 / 0.01;
  Temp_2 = Temp_2 - 5.0;

  if (dataFile){
// Save to SD card
  dataFile.print("");                                                          
  dataFile.print(myRTC.dayofmonth);                                                              
  dataFile.print("/");                                                                           
  dataFile.print(myRTC.month);                                                                  
  dataFile.print("/");                                                                          
  dataFile.print(myRTC.year);                                                                
  dataFile.print("       ");                                                                        
  dataFile.print(myRTC.hours);                                                                  
  dataFile.print(":");                                                                                    
  dataFile.print(myRTC.minutes);                                                                        
  dataFile.print(":");                                                                                    
  dataFile.print(myRTC.seconds);
  dataFile.print("    \t");  
  dataFile.print(F(""));
  dataFile.print(h);
  dataFile.print(F("%  "));
  dataFile.print(t);
  dataFile.print("     \t"); // Create tab/space  
  dataFile.print(Temp_1); // Print temperature from temperature sensor 1
  dataFile.print("     \t"); // Create tab/space
  dataFile.print(Temp_2); // Print temperature from temperature sensor 2
  dataFile.print("     \t"); // Create tab/space
  dataFile.println();
  dataFile.close();
// Print to serial

  Serial.print("");                                                          
  Serial.print(myRTC.dayofmonth);                                                              
  Serial.print("/");                                                                           
  Serial.print(myRTC.month);                                                                  
  Serial.print("/");                                                                          
  Serial.print(myRTC.year);                                                                
  Serial.print("       ");                                                                       
  Serial.print(myRTC.hours);                                                                  
  Serial.print(":");                                                                                    
  Serial.print(myRTC.minutes);                                                                        
  Serial.print(":");                                                                                    
  Serial.print(myRTC.seconds);
  Serial.print("    \t");   
  Serial.print(F(""));
  Serial.print(h);
  Serial.print(F("%  \t"));
  Serial.print(t);
  Serial.print("     \t"); // Create tab/space
  Serial.print(Temp_1); // Print temperature from temperature sensor 1
  Serial.print("     \t"); // Create tab/space
  Serial.print(Temp_2); // Print temperature from temperature sensor 2
  Serial.print("     \t"); // Create tab/space
  Serial.println();
  }

  else {
    Serial.println("There has been an error saving data");
  }
  previous = current ;
  }

// Temperature and humidity control
if ( currenta - previousa >= intervala) {

  Temp_1 = analogRead(A0)*5/1023.0; // Read value from analog port A0 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_1 = Temp_1 - 0.5; 
  Temp_1 = Temp_1 / 0.01;
  Temp_1 = Temp_1 - 2.0;

    // Read the humidity of the DHT11
  float h = dht.readHumidity();

Serial.print(h);
Serial.print(F("%  \t"));

Serial.println(Temp_1); // Print temperature from temperature sensor 1
Serial.print("     \t"); // Create tab/space

if(Temp_1 > 40) // Set the temperature
{
   digitalWrite(HEATER,LOW);
   digitalWrite(FAN,HIGH);
}
else
{
  digitalWrite(HEATER,HIGH);
  digitalWrite(FAN,LOW); 
}
      previousa = currenta ;
    }

if ( h > 60 ) // If the humidity if greater than 60%

{
  digitalWrite(HEATER, LOW);  
  digitalWrite(FAN, HIGH);
}
else
{
  digitalWrite(HEATER, LOW);  
  digitalWrite(FAN, LOW);
}
  
}

Objects defined inside { } are only visible inside the same { }.

That concept is called scope, you should get familiar with it.

Looks to me you declared h inside this if statement, and are trying to access it outside the if statement.

if ( currenta - previousa >= intervala) {

I see so I need to declare the float h inside the scope? If so would it be inside the loop function of after the if ( h > 60 )?

In line 146 you declare a float variable h.

float h = dht.readHumidity();

And in line 227 you declare another float variable h.

float h = dht.readHumidity();

Both of those declarations are inside of if blocks. As soon as the if is finished the variable h goes out of scope (ceases to exist). It may be better to declare the variable in global scope. And declare only one h variable. In your original code, the 2 h variables are not the same.