Error compiling. LED not defined in scope DHT22 sensor

I want to turn a fan on when RH% drops below 70. I am using variable “percentHumidity” to turn on an output (fan_on) pin12. Not sure why i get “not declared in this scope”. I think it has something to do with Global vs Local. can someone educate me on this? Everything i try makes it worse.

Arduino: 1.6.5 (Windows 8.1), Board: “Arduino/Genuino Uno”

DHT.cpp: In member function ‘void DHT::begin()’:
DHT.cpp:29: error: ‘FAN_ON’ was not declared in this scope
pinMode (FAN_ON, OUTPUT); //fan digital output
^
DHT.cpp: In member function ‘float DHT::computeHeatIndex(float, float, bool)’:
DHT.cpp:125: error: ‘FAN_ON’ was not declared in this scope
digitalWrite (FAN_ON, HIGH)
^
DHT.cpp:126: error: expected ‘;’ before ‘}’ token
}else{
^
DHT.cpp:126: error: expected ‘}’ before ‘else’
}else{
^
DHT.cpp:127: error: ‘FAN_ON’ was not declared in this scope
digitalWrite (FAN_ON, LOW)
^
DHT.cpp:128: error: expected ‘;’ before ‘}’ token
}
^
DHT.cpp: At global scope:
DHT.cpp:131: error: expected unqualified-id before ‘return’
return isFahrenheit ? hi : convertFtoC(hi);
^
DHT.cpp:132: error: expected declaration before ‘}’ token
}
^
‘FAN_ON’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

//---------------------------------------------------------------------

/* DHT library
MIT license
written by Adafruit Industries
*/

#include “DHT.h”

#define MIN_INTERVAL 2000

DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
_pin = pin;
_type = type;
#ifdef __AVR
_bit = digitalPinToBitMask(pin);
_port = digitalPinToPort(pin);
#endif
_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for
// reading pulses from DHT sensor.
// Note that count is now ignored as the DHT reading algorithm adjusts itself
// basd on the speed of the processor.

const int FAN_ON = 12; //define digital output to fan

}

void DHT::begin(void) {
// set up the pins!
pinMode(_pin, INPUT_PULLUP);
pinMode (FAN_ON, OUTPUT); //fan digital output

// Using this value makes sure that millis() - lastreadtime will be
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
// but so will the subtraction.
_lastreadtime = -MIN_INTERVAL;
DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
}

//boolean S == Scale. True == Fahrenheit; False == Celcius
float DHT::readTemperature(bool S, bool force) {
float f = NAN;

if (read(force)) {
switch (_type) {
case DHT11:
f = data[2];
if(S) {
f = convertCtoF(f);
}
break;
case DHT22:
case DHT21:
f = data[2] & 0x7F;
f *= 256;
f += data[3];
f *= 0.1;
if (data[2] & 0x80) {
f *= -1;
}
if(S) {
f = convertCtoF(f);
}
break;
}
}
return f;
}

float DHT::convertCtoF(float c) {
return c * 1.8 + 32;
}

float DHT::convertFtoC(float f) {
return (f - 32) * 0.55555;
}

float DHT::readHumidity(bool force) {
float f = NAN;
if (read()) {
switch (_type) {
case DHT11:
f = data[0];
break;
case DHT22:
case DHT21:
f = data[0];
f *= 256;
f += data[1];
f *= 0.1;
break;
}
}
return f;
}

//boolean isFahrenheit: True == Fahrenheit; False == Celcius
float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit) {
// Using both Rothfusz and Steadman’s equations
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
float hi;

if (!isFahrenheit)
temperature = convertCtoF(temperature);

hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (percentHumidity * 0.094));

if (hi > 79) {
hi = -42.379 +
2.04901523 * temperature +
10.14333127 * percentHumidity +
-0.22475541 * temperaturepercentHumidity +
-0.00683783 * pow(temperature, 2) +
-0.05481717 * pow(percentHumidity, 2) +
0.00122874 * pow(temperature, 2) * percentHumidity +
0.00085282 * temperature
pow(percentHumidity, 2) +
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);

if((percentHumidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
hi -= ((13.0 - percentHumidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);

else if((percentHumidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);

if (percentHumidity < 70);{ //If humidity falls below 70%, turn on fan
digitalWrite (FAN_ON, HIGH)
}else{
digitalWrite (FAN_ON, LOW)
}
}

return isFahrenheit ? hi : convertFtoC(hi);
}

boolean DHT::read(bool force) {
// Check if sensor was read less than two seconds ago and return early
// to use last reading.
uint32_t currenttime = millis();
if (!force && ((currenttime - _lastreadtime) < 2000)) {
return _lastresult; // return last correct measurement
}
_lastreadtime = currenttime;

// Reset 40 bits of received data to zero.
data[0] = data[1] = data[2] = data[3] = data[4] = 0;

// Send start signal. See DHT datasheet for full signal diagram:
// http://www.adafruit.com/datasheets/Digital%20humidity%20and%20temperature%20sensor%20AM2302.pdf

// Go into high impedence state to let pull-up raise data line level and
// start the reading process.
digitalWrite(_pin, HIGH);
delay(250);

// First set data line low for 20 milliseconds.
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delay(20);

uint32_t cycles[80];
{
// Turn off interrupts temporarily because the next sections are timing critical
// and we don’t want any interruptions.
InterruptLock lock;

// End the start signal by setting data line high for 40 microseconds.
digitalWrite(_pin, HIGH);
delayMicroseconds(40);

// Now start reading the data line to get the value from the DHT sensor.
pinMode(_pin, INPUT_PULLUP);
delayMicroseconds(10); // Delay a bit to let sensor pull data line low.

// First expect a low signal for ~80 microseconds followed by a high signal
// for ~80 microseconds again.
if (expectPulse(LOW) == 0) {
DEBUG_PRINTLN(F(“Timeout waiting for start signal low pulse.”));
_lastresult = false;
return _lastresult;
}
if (expectPulse(HIGH) == 0) {
DEBUG_PRINTLN(F(“Timeout waiting for start signal high pulse.”));
_lastresult = false;
return _lastresult;
}

for (int i=0; i<80; i+=2) {
cycles* = expectPulse(LOW);*

  • cycles[i+1] = expectPulse(HIGH);*
  • }*
    } // Timing critical code is now complete.
    // Inspect pulses and determine which ones are 0 (high state cycle count < low
    // state cycle count), or 1 (high state cycle count > low state cycle count).
    for (int i=0; i<40; ++i) {
    uint32_t lowCycles = cycles[2i];
    uint32_t highCycles = cycles[2i+1];
  • if ((lowCycles == 0) || (highCycles == 0)) {*
  • DEBUG_PRINTLN(F(“Timeout waiting for pulse.”));*
  • _lastresult = false;*
  • return _lastresult;*
  • }*
  • data[i/8] <<= 1;*
  • // Now compare the low and high cycle times to see if the bit is a 0 or 1.*
  • if (highCycles > lowCycles) {*
  • // High cycles are greater than 50us low cycle count, must be a 1.*
  • data[i/8] |= 1;*
  • }*
  • // Else high cycles are less than (or equal to, a weird case) the 50us low*
  • // cycle count so this must be a zero. Nothing needs to be changed in the*
  • // stored data.*
    }
    DEBUG_PRINTLN(F(“Received:”));
    DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(F(", "));
    DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(F(", "));
    DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(F(", "));
    DEBUG_PRINT(data[3], HEX); DEBUG_PRINT(F(", "));
    DEBUG_PRINT(data[4], HEX); DEBUG_PRINT(F(" =? "));
    DEBUG_PRINTLN((data[0] + data[1] + data[2] + data[3]) & 0xFF, HEX);
    // Check we read 40 bits and that the checksum matches.
    if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
  • _lastresult = true;*
  • return _lastresult;*
    }
    else {
  • DEBUG_PRINTLN(F(“Checksum failure!”));*
  • _lastresult = false;*
  • return _lastresult;*
    }
    }

You didn't intend for the last half of your code to be italic. Go back and put [ code ] tags around the code so that it's properly formatted.

You should not be editing the library. FAN_ON should not appear in there. You might use this library in many different projects, not all of which will have a fan on pin 12. Adafruit may publish a new version of the library and then you'll lose your modifications when you update to the later version.

FAN_ON is more likely useful as a global constant in your sketch which is utilising the library.

Ahhh, good information. I will restore the .cpp file and add my code to the main sketch. I THINK I know what I need to do. But I may be back. Thanks!

What variable can i use to compare in order to insert an IF statement? I need to break in to the code somewhere. It compiles fine. I thought the floating integer “h” but i guess not.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domains

#include “DHT.h”

#define DHTPIN 3 // what digital pin we’re connected to

// Uncomment whatever type you’re using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 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

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
const int FAN_ON = 12;

void setup() {
Serial.begin(9600);
Serial.println(“DHTxx test!”);
pinMode(FAN_ON, OUTPUT);

dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

// 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 = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(“Sum Ting Wong”);
return;
}

if (h < 70, FAN_ON); {
digitalWrite (FAN_ON, HIGH); //turn on fan
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.print(” *C “);
Serial.print(f);
Serial.print(” *F\t”);
Serial.print(“Heat index: “);
Serial.print(hic);
Serial.print(” *C “);
Serial.print(hif);
Serial.println(” *F”);

}

if (h < 70, FAN_ON);It looks like you are testing the right variable but there are problems.

Firstly, semicolon on the end of the if statement should not be there.
Secondly, what do you expect FAN_ON to do in the code above ? Take it and the semicolon out.

Getting closer, I see what you mean. After you asked the question, I wondered myself. I need to read up on if statements. Latest:

}

if (h < 70) {
digitalWrite (FAN_ON, HIGH); //turn on fan
}

Still not turning fan on yet but I havent given up. I will try a few more things. And read.

Hi,
As was asked previously.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

I was wondering where that </> button was, thanks. that will clean it up next time.

Bob, would it make sense to print “h” to a serial monitor to see if it indeed is the variable for the humidity data? I might have to comment out some lines.

would it make sense to print "h" to a serial monitor to see if it indeed is the variable for the humidity data?

It would be a good idea to print it so that you can see its value but that won't actually prove that it is the humidity although
  float h = dht.readHumidity();would seem to indicate that it is.

While you are making changes can I suggest that you give your variables meaningful names to make the code more understandable.