Trying to turn off an led when temp is reached. (using dht22)


#include "DHT.h"
String password = "80";
int arrivingdatabyte;
#define DHTPIN 4


#define DHTTYPE DHT22


DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  dht.begin();
}

void loop() {

  delay(2000);


  float h = dht.readHumidity();

  float t = dht.readTemperature();

  float f = dht.readTemperature(true);


  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }


  float hif = dht.computeHeatIndex(f, h);

  float hic = dht.computeHeatIndex(t, h, false);








  Serial.print(hif);
  Serial.println(F("°F"));



  while (Serial.available() == 1) {
  }

  String input = Serial.readString();

  if (input == "80") {

    digitalWrite(13, LOW);
  }
  if (input == "78") {

    digitalWrite(13, HIGH);
  }
}

Your code will not compile. Please post the complete code.

Please read the forum guidelines](How to get the best out of this forum) to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

1 Like

Looks like you are ignoring what you get from the sensor.

The only thing that turns on/off the LED is information that you receive from the serial monitor.

Should probably be readStringUntil('\r');

What is this for?

i really have no idea im completely new and dont have enough time if i need new code thats fine as long as i can set the temp parameters

image

see when this reads 80 or higher i need the led to turn off

Dude... you really should have paid more attention in class.

You need an "if" statement.

if (hif > 80)
// Do something
else
// Do the other thing

i didnt really do classes this is for a project

did i do it right because im getting an error

#include "DHT.h"
String password = "80";
int arrivingdatabyte;
#define DHTPIN 4


#define DHTTYPE DHT22


DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  dht.begin();
}

void loop() {

  delay(2000);


  float h = dht.readHumidity();

  float t = dht.readTemperature();

  float f = dht.readTemperature(true);


  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }


  float hif = dht.computeHeatIndex(f, h);

  float hic = dht.computeHeatIndex(t, h, false);








  Serial.print(hif);
  Serial.println(F("°F"));



  while (Serial.available()) {
  }

  String input = Serial.readString();

  if (hif >= "80") {

    digitalWrite(13, LOW);
  }
  else (hif <= "78") {

    digitalWrite(13, HIGH);
  }
}

Hi, @ace7776

What is the error report?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

hif is a float... lose the quotes. (")

C:\Users\Alex\Documents\Arduino\sketch_jan15a\sketch_jan15a.ino: In function 'void loop()':
C:\Users\Alex\Documents\Arduino\sketch_jan15a\sketch_jan15a.ino:60:11: error: invalid operands of types 'float' and 'const char [3]' to binary 'operator>='
if (hif >= "80") {
^~~
C:\Users\Alex\Documents\Arduino\sketch_jan15a\sketch_jan15a.ino:64:13: error: invalid operands of types 'float' and 'const char [3]' to binary 'operator<='
else (hif <= "78") {
^~~

exit status 1

Compilation error: invalid operands of types 'float' and 'const char [3]' to binary 'operator>='

See post #10

alright one sec

alright well the error is gone but once it hits past 80 it doesnt turn off

#include "DHT.h"
String password = "80";
int arrivingdatabyte;
#define DHTPIN 4


#define DHTTYPE DHT22


DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  dht.begin();
}

void loop() {

  delay(2000);


  float h = dht.readHumidity();

  float t = dht.readTemperature();

  float f = dht.readTemperature(true);


  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }


  float hif = dht.computeHeatIndex(f, h);

  float hic = dht.computeHeatIndex(t, h, false);








  Serial.print(hif);
  Serial.println(F("°F"));



  while (Serial.available()) {
  }

  String input = Serial.readString();

  if (hif < 80) {

    digitalWrite(13, LOW);
  }
  else (hif > 78); {

    digitalWrite(13, HIGH);
  }
}

Should this be > 80 ?

Should this be < 78 ?

Get rid of the ";"

still doesnt turn off and if i get rid of the ; i get

C:\Users\Alex\Documents\Arduino\sketch_jan15a\sketch_jan15a.ino: In function 'void loop()':
C:\Users\Alex\Documents\Arduino\sketch_jan15a\sketch_jan15a.ino:64:19: error: expected ';' before '{' token
   else (hif < 78) {
                   ^

exit status 1

Compilation error: expected ';' before '{' token

should be
else if (hif < 78) {

Post your latest code in full

1 Like

thanks works now

#include "DHT.h"
String password = "80";
int arrivingdatabyte;
#define DHTPIN 4


#define DHTTYPE DHT22


DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  dht.begin();
}

void loop() {

  delay(2000);


  float h = dht.readHumidity();

  float t = dht.readTemperature();

  float f = dht.readTemperature(true);


  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }


  float hif = dht.computeHeatIndex(f, h);

  float hic = dht.computeHeatIndex(t, h, false);








  Serial.print(hif);
  Serial.println(F("°F"));



  while (Serial.available()) {
  }

  String input = Serial.readString();

  if (hif > 80) {

    digitalWrite(13, LOW);
  }
  else if (hif < 78) {

    digitalWrite(13, HIGH);
  }
}

What is this code for? It does nothing.

really? huh yea

You do nothing with "input" ... if you don't need it get rid of it.

1 Like