Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: October 02, 2012, 03:47:41 pm » |
Howdy I’m a beginner I am trying to use a DHT11 to monitor temp/humidity ( turn on Led8 when temp drops below threshold1 and use threshold2 for humidity-not entered yet, if I can figure temp first then humid. I have declared threshold1, but keep getting this error when compiling Threshold1 was not declared in this scope Using arduino 1280 and arduino 1.01 Thanks for all replies
#include <dht11.h>
dht11 DHT11;
void setup() { const int LEDPIN8 = 8; // heat const int LEDPIN9 = 9;// mister pinMode (LEDPIN8, OUTPUT); // Heat
pinMode (LEDPIN9, OUTPUT); // Mister DHT11.attach(2); Serial.begin(9600);
const int THRESHOLD1 = 90; // Heat const int THRESHOLD2 = 90; // Mister
}
void loop() { Serial.println("\n");
int chk = DHT11.read();
Serial.print("Read sensor: "); switch (chk) { case 0: Serial.println("OK"); break; case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); }
Serial.print("Temperature (°F): "); Serial.println(DHT11.fahrenheit(), DEC); if (chk < THRESHOLD1) // digitalWrite(LEDPIN8, HIGH);
else digitalWrite(LEDPIN8,LOW);
delay(20000); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19375
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: October 02, 2012, 03:49:02 pm » |
The compiler is giving a huge clue. You've declared it inside setup, so it isn't visible (in scope) in loop.
Ditto your pin numbers.
Please use code tags when posting code.
Also, 0 is always less than 90.
|
|
|
|
« Last Edit: October 02, 2012, 03:52:26 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Leeds, England
Offline
Full Member
Karma: 6
Posts: 210
Quick, chuck it in the bin before the boss finds out...
|
 |
« Reply #2 on: October 02, 2012, 03:59:29 pm » |
Or, to put it another way, chk is the device status, not the temperature or humidity...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #3 on: October 02, 2012, 08:34:16 pm » |
Using a DHT11 on arduino1280, Trying to turn on LED when threshold met When I compile it stops on Temperature = (Temperature (°F): ); and says stray "/" in program, I can't find it Sorry don't understand code tags yet, don't see #
#include <dht11.h>
dht11 DHT11;
void setup() { DHT11.attach(2); Serial.begin(9600); }
void loop() { const int LEDPIN8 = 8; // Temp const int LEDPIN9 = 9; // Humidity pinMode (LEDPIN8, OUTPUT); // Temp pinMode (LEDPIN9, OUTPUT); // Humidity {const int THRESHOLD1 = 80; // Temp const int THRESHOLD2 = 50; // humidity Temperature = (Temperature (°F): );
Serial.println("\n");
int chk = DHT11.read();
Serial.print("Read sensor: "); switch (chk) { case 0: Serial.println("OK"); break; case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; }
Serial.print("Humidity (%): "); Serial.println((float)DHT11.humidity, DEC);
Serial.print("Temperature (°F): "); Serial.println(DHT11.fahrenheit(), DEC); //Temperature Temperature = (DHT11.fahrenheit(), DEC); if (Temperature < THRESHOLD1) // heat digitalWrite(LEDPIN8, HIGH); else digitalWrite(LEDPIN8,LOW); else digitalWrite(LEDPIN8,LOW); //Humidity Humidity = (float)DHT11.humidity, DEC); if (Humidity > THRESHOLD2) // Humidity digitalWrite(LEDPIN9, HIGH);
else digitalWrite(LEDPIN9,LOW);
delay(20000); }
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6611
|
 |
« Reply #4 on: October 02, 2012, 08:42:25 pm » |
When I compile it stops on Temperature = (Temperature (°F): ); and says stray "/" in program, I can't find it.
It's probably just confused because "Temperature = (Temperature (°F): );" is not a valid statement. What did you intend that line to do?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #5 on: October 02, 2012, 08:48:06 pm » |
I want to compare it to the threshold1 to turn on the LED
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 16
Posts: 1036
Arduino rocks
|
 |
« Reply #6 on: October 02, 2012, 08:55:34 pm » |
Stray \ in program usually means you have a unicode character in there. Which is most certainly that degree symbol. Delete it.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 5
Arduino rocks
|
 |
« Reply #7 on: October 02, 2012, 09:11:34 pm » |
It looks like this line Temperature = (Temperature (°F): );
near the top of the file was supposed to be a variable declaration but some text from a later println got in there. You make assignments to Temperature and Humidity without having declared them. There is also a stray "{" before "const int THRESHOLD1". Try replacing this: {const int THRESHOLD1 = 80; // Temp const int THRESHOLD2 = 50; // humidity Temperature = (Temperature (°F): );
with this: const int THRESHOLD1 = 80; // Temp const int THRESHOLD2 = 50; // humidity float Temperature; float Humidity;
BTW, variable names usually start with a lower case to distinguish them from class names.
|
|
|
|
« Last Edit: October 02, 2012, 09:14:00 pm by Blue Eyes »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #8 on: October 03, 2012, 12:33:15 am » |
When I set the (temp) threshold1 to 60 ( lower limit), which is below my room temp and threshold3 to 80(upper limit), Ledpin8 just flashes once and goes low, never comes on again, unless restart. I wonder if my IF statements are right? At least a LED comes on. The prog prints temp/humidity OK #include <dht11.h>
dht11 DHT11;
void setup() { DHT11.attach(2); Serial.begin(9600); }
void loop() { const int LEDPIN8 = 8; // Temp const int LEDPIN9 = 9; // Humidity pinMode (LEDPIN8, OUTPUT); // Temp pinMode (LEDPIN9, OUTPUT); // Humidity const int THRESHOLD1 = 60; // Temp lower limit turn on..60 const int THRESHOLD3 = 100; // Temp upper limit turn off const int THRESHOLD2 = 50; // humidity lower limit turn on const int THRESHOLD4 = 75; // humidity upper limit turn off float Temperature; float Humidity;
Serial.println("\n");
int chk = DHT11.read();
Serial.print("Read sensor: "); switch (chk) { case 0: Serial.println("OK"); break; case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; }
Serial.print("Humidity (%): "); Serial.println((float)DHT11.humidity, DEC);
Serial.print("Temperature (F): "); Serial.println(DHT11.fahrenheit(), DEC); //Temperature Temperature = (DHT11.fahrenheit(), DEC); if (DHT11.fahrenheit(), DEC < THRESHOLD1) // temp turn on...less than 60 digitalWrite(LEDPIN8, HIGH); //delay(4000);//works on temp if (DHT11.fahrenheit(), DEC > THRESHOLD3) // temp turn off.. more than 80 digitalWrite(LEDPIN8,LOW); //Humidity Humidity = (DHT11.humidity, DEC); if (DHT11.humidity, DEC < THRESHOLD2) // Humidity turn on les than 50 digitalWrite(LEDPIN9, HIGH); //delay(2000);//no work on hum
if (DHT11.humidity, DEC > THRESHOLD4) // Humidity turn off.. more than 75 digitalWrite(LEDPIN9,LOW);
delay(200); }
Moderator edit: CODE TAGS
|
|
|
|
« Last Edit: October 03, 2012, 01:24:09 am by AWOL »
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19375
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: October 03, 2012, 01:26:35 am » |
Serial.println((float)DHT11.humidity, DEC); Trying to print a 32 bit float (which is, in reality a pointer to a function anyway) to ten decimal places is a trifle optimistic. Why have you got all your "pinMode"s in "loop"? I'd expect to see them in "setup". What do you expect this if (DHT11.fahrenheit(), DEC < THRESHOLD1) to do? Uncompiled, untested. #include <dht11.h>
const int LEDPIN8 = 8; // Temp const int LEDPIN9 = 9; // Humidity const int THRESHOLD1 = 60; // Temp lower limit turn on..60 const int THRESHOLD3 = 100; // Temp upper limit turn off const int THRESHOLD2 = 50; // humidity lower limit turn on const int THRESHOLD4 = 75; // humidity upper limit turn off dht11 DHT11;
void setup() { DHT11.attach(2); Serial.begin(9600); pinMode (LEDPIN8, OUTPUT); // Temp pinMode (LEDPIN9, OUTPUT); // Humidity }
void loop() { Serial.println("\n");
int chk = DHT11.read();
Serial.print("Read sensor: "); switch (chk) { case 0: Serial.println("OK"); break; case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; }
float temperature = DHT11.fahrenheit(); float humidity = DHT11.humidity (); Serial.print("Humidity (%): "); Serial.println(humidity); Serial.print("Temperature (F): "); Serial.println(temperature); if (temperature < THRESHOLD1) { digitalWrite(LEDPIN8, HIGH); } if (temperature > THRESHOLD3) { digitalWrite(LEDPIN8,LOW); } if (humidity < THRESHOLD2) { digitalWrite(LEDPIN9, HIGH); } if (humidity > THRESHOLD4) { digitalWrite(LEDPIN9,LOW); } delay(200); }
I'd be inclined to give the thresholds meaningful names like "LOWER_TEMPERATURE_THRESHOLD" etc.
|
|
|
|
« Last Edit: October 03, 2012, 01:34:53 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #10 on: October 03, 2012, 01:52:26 am » |
What do you expect this Code:
if (DHT11.fahrenheit(), DEC < THRESHOLD1)
to do? If the temp is lower than 60 turn on Ledpin6 Looks like my pinmodes are in setup The sketch compiles and displays temp/humidity, just won't hold the Led's high between thresholds
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19375
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: October 03, 2012, 01:56:17 am » |
If the temp is lower than 60 turn on Ledpin6 It doesn't do that. It compares the value 10 (aka "DEC") to the value 60 (aka "THRESHOLD1"). just won't hold the Led's high between thresholds Now it is my turn to not understand. (that's a hint to you to post your observations)
|
|
|
|
« Last Edit: October 03, 2012, 01:57:51 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #12 on: October 03, 2012, 02:00:30 am » |
What do you expect this if (DHT11.fahrenheit(), DEC < THRESHOLD1) to do? If the temp is lower than 60 turn on Ledpin6 DEC is just an optional argument of Serial.print(), to ensure the first one is printed as a decimal number, not hex or binary or char. It's not a typecast operator, like you seem to be using it: Temperature = (DHT11.fahrenheit(), DEC);
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #13 on: October 03, 2012, 02:05:52 am » |
A note about coding style always use brackets after if(), like this (I have not corrected your code, just added brackets): if (DHT11.fahrenheit(), DEC > THRESHOLD3) { digitalWrite(LEDPIN8,LOW); } //delay(4000);//works on temp <== where did this go ? Inside the "then" block or not ?
Humidity = (DHT11.humidity, DEC);
if (DHT11.humidity, DEC < THRESHOLD2) { digitalWrite(LEDPIN9, HIGH); }
if (DHT11.humidity, DEC > THRESHOLD4) { digitalWrite(LEDPIN9,LOW); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19375
I don't think you connected the grounds, Dave.
|
 |
« Reply #14 on: October 03, 2012, 02:14:05 am » |
DEC is just an optional argument of Serial.print(), to ensure the first one is printed as a decimal number, not hex or binary or char. Or to tell how many decimal places to print the value with, if the value is a float. (or a function pointer cast to a float, which isn't recommended)
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|