Pages: [1] 2   Go Down
Author Topic: Need help with sketch  (Read 1548 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Leeds, England
Offline Offline
God Member
*****
Karma: 14
Posts: 590
Quick, chuck it in the bin before the boss finds out...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or, to put it another way, chk is the device status, not the temperature or humidity...
Logged

Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like this line

Code:
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:
{const int THRESHOLD1 = 80;   // Temp
const int THRESHOLD2 = 50;  // humidity
Temperature = (Temperature (°F): );

with this:

Code:
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 Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
if (DHT11.fahrenheit(), DEC < THRESHOLD1)    
to do?

Uncompiled, untested.
Code:
#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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
« 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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

"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.

Pages: [1] 2   Go Up
Jump to: