Go Down

Topic: Need help with sketch (Read 1 time) previous topic - next topic

truck56

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);
}



AWOL

#1
Oct 02, 2012, 10:49 pm Last Edit: Oct 02, 2012, 10:52 pm by AWOL Reason: 1
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dannable

Or, to put it another way, chk is the device status, not the temperature or humidity...
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

truck56

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);
}



johnwasser


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?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

truck56

I want to compare it to the threshold1 to turn on the LED

WizenedEE

Stray \ in program usually means you have a unicode character in there. Which is most certainly that degree symbol. Delete it.

Blue Eyes

#7
Oct 03, 2012, 04:11 am Last Edit: Oct 03, 2012, 04:14 am by Blue Eyes Reason: 1
It looks like this line

Code: [Select]

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:

Code: [Select]
{const int THRESHOLD1 = 80;   // Temp
const int THRESHOLD2 = 50;  // humidity
Temperature = (Temperature (°F): );


with this:

Code: [Select]
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.

truck56

#8
Oct 03, 2012, 07:33 am Last Edit: Oct 03, 2012, 08:24 am by AWOL Reason: 1
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





Code: [Select]

#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

AWOL

#9
Oct 03, 2012, 08:26 am Last Edit: Oct 03, 2012, 08:34 am by AWOL Reason: 1
Code: [Select]
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
Code: [Select]
if (DHT11.fahrenheit(), DEC < THRESHOLD1)     to do?

Uncompiled, untested.
Code: [Select]
#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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

truck56

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

AWOL

#11
Oct 03, 2012, 08:56 am Last Edit: Oct 03, 2012, 08:57 am by AWOL Reason: 1
Quote
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").

Quote
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)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

tuxduino

Quote

Quote

What do you expect this
Code: [Select]
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:
Code: [Select]
Temperature = (DHT11.fahrenheit(), DEC);

tuxduino

A note about coding style always use brackets after if(), like this (I have not corrected your code, just added brackets):

Code: [Select]

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);
}

AWOL

Quote
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)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up