0

I know my code will not be great and this is very basic code, however this is my first (and last) time ever coding for a project, I have very little knowledge of code. The aim of my code is to make a LED light turn on when the humidity reaches above 70 using a DHT11 sensor. My sensor used to work before I changed the code somehow and now it does not, and also the LED has never turned on as my if statements intend it to. How do I need to adjust/redo my code for it to work and to achieve my aim?

#include <SimpleDHT.h>

// for DHT11,
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;
const int ledPin = 13; // We will use the internal LED

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); // Set the LED Pin as an output
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  float h = SimpleDHT.readHumidity();
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if (h >= 70) {{
    digitalWrite(ledPin,HIGH); //Turn the LED off
  else {
    digitalWrite(ledPin,LOW);
  } else if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("uh oh not working ! :(  "); Serial.println(err);delay(1000);
    return;
  }
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" H");
  // DHT11 sampling rate is 1HZ.
  delay(1500);
}                  
3
  • 1
    format your code with proper indentation ... it may help you see that the curly braces are messed up – jsotola Nov 23 '20 at 23:11
  • go to the begining ... start with the DHT11 example code ... then add the LED code – jsotola Nov 24 '20 at 0:14
  • 1
    Hi mate, I think you don't close one curly braces at your first if statment> You wrote: "if (h >= 70) { { " with 2 curly braces but didn't close one. Your compiler should warn you tho... My advice: the same as @jsotola: start with the sensor example and then add the LED code... Simple :-) – k.Cyborg Nov 24 '20 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.