Reading different measures and turning different LEDs depending on the reading

Hi,

I've been working in this code for a while and can't figure what is wrong.

I have 2 variables (temperature and humidity) and 4 LEDs.

I want GREEN to turn on when temp and humidity are good.
RED to turn on and YELLOW to blink when the temperature is too low or too high.
RED to turn on and BLUE to blink when the humidity is too low or too high.
RED to blink when both temp and humidity are wrong.

I didn't make it blink yet, first I'm trying to set up the code right.
Not sure if using if and else if is the right choice, or should I try the switch function.

I am new to programing.

#include <Adafruit_Sensor.h>
#include <DHT.h>


// pins definition
#define DHTPIN 7
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

// variables


void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  Serial.println("Testing shit!");
  pinMode (9 , OUTPUT); //BLUE
  pinMode (6 , OUTPUT); //YELLOW
  pinMode (12 , OUTPUT); //RED
    pinMode (10 , OUTPUT); //GREEN
  
  dht.begin();
}


void loop() {
  // put your main code here, to run repeatedly:

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

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



  Serial.print("Humidity: ");
  Serial.print(h,0);
  Serial.print("%\t");
  Serial.print("Temperature: ");
  Serial.print(t,0);
  Serial.println("ºC ");
 
  delay(2000);
 
 if ((55 <= h && h <= 75) & (20 <= t & t <= 28)){
    // system messages
 Serial.println("Perfect!"); 
digitalWrite(10, HIGH); 
    digitalWrite(9, LOW);
    digitalWrite(6, LOW);
    digitalWrite(12, LOW); 
    
     delay(100);

if ((h < 55 ) || (h > 75) & (t >= 20 ) & (t <= 28)) { 

    Serial.println("Check Humidity!"); 
    digitalWrite(10, LOW); 
    digitalWrite(9, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(12, HIGH); 
    delay(100);
 
 if ((h >= 55 ) & (h <= 75) & (t < 20 ) || (t > 28)){

    Serial.println("Check Temperature!"); 
    digitalWrite(10, LOW); 
    digitalWrite(9, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(12, HIGH); 
    delay(100);

if ((t < 21) || (t > 28) & (h < 55) || (h > 75)) {
 
   Serial.println("Help!"); 
   digitalWrite(10, LOW); 
   digitalWrite(9, LOW);
   digitalWrite(6, LOW);
   digitalWrite(12, HIGH);
   delay(100);

} } }

A quick format of your code will reveal if there are any missing braces/brackets/parentheses (hint: braces).

#include <Adafruit_Sensor.h>
#include <DHT.h>

// pins definition
#define DHTPIN 7
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

// variables

void setup() 
{
    // put your setup code here, to run once:
    Serial.begin(9600);
    Serial.println("Testing shit!");
    pinMode (9 , OUTPUT); //BLUE
    pinMode (6 , OUTPUT); //YELLOW
    pinMode (12 , OUTPUT); //RED
    pinMode (10 , OUTPUT); //GREEN

    dht.begin();
}

void loop() 
{
    // put your main code here, to run repeatedly:
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);

    if (isnan(h) || isnan(t) || isnan(f)) 
    {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }
    Serial.print("Humidity: ");
    Serial.print(h,0);
    Serial.print("%\t");
    Serial.print("Temperature: ");
    Serial.print(t,0);
    Serial.println("ºC ");
    delay(2000);

    if ((55 <= h && h <= 75) & (20 <= t & t <= 28))
    {
        // system messages
        Serial.println("Perfect!");
        digitalWrite(10, HIGH);
        digitalWrite(9, LOW);
        digitalWrite(6, LOW);
        digitalWrite(12, LOW);
        delay(100);

        if ((h < 55 ) || (h > 75) & (t >= 20 ) & (t <= 28)) 
        {
            Serial.println("Check Humidity!");
            digitalWrite(10, LOW);
            digitalWrite(9, HIGH);
            digitalWrite(6, LOW);
            digitalWrite(12, HIGH);
            delay(100);
            if ((h >= 55 ) & (h <= 75) & (t < 20 ) || (t > 28))
            {
                Serial.println("Check Temperature!");
                digitalWrite(10, LOW);
                digitalWrite(9, LOW);
                digitalWrite(6, HIGH);
                digitalWrite(12, HIGH);
                delay(100);

                if ((t < 21) || (t > 28) & (h < 55) || (h > 75)) 
                {

                    Serial.println("Help!");
                    digitalWrite(10, LOW);
                    digitalWrite(9, LOW);
                    digitalWrite(6, LOW);
                    digitalWrite(12, HIGH);
                    delay(100);
                } 
            } 
        }

You should also re-visit your if statements and make sure that you really want & and not &&.

You could use variables for the pins, not waste RAM to string constants,
centralize the tests in functions, use the else part of if statements, ...

(untested)

#include <Adafruit_Sensor.h>
#include <DHT.h>

#define DHTTYPE DHT11
const byte DHTPIN = 7;
const byte blueLedPin = 9;
const byte yellowLedPin = 6;
const byte redLedPin = 12;
const byte greenLedPin = 10;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(250000);
  Serial.println(F("Testing shit!"));
  pinMode (blueLedPin, OUTPUT);
  pinMode (yellowLedPin , OUTPUT);
  pinMode (redLedPin, OUTPUT);
  pinMode (greenLedPin, OUTPUT);
  dht.begin();
}

bool goodTemperature(float temp) {
  return temp >= 20 && temp <= 28;
}

bool goodHumidity(float humid) {
  return humid >= 55 && humid <= 75;
}

void loop() {
  static uint32_t lastMeasurement;
  uint32_t topLoop = millis();
  if (topLoop - lastMeasurement >= 1000) {
    lastMeasurement = topLoop;
    float h = dht.readHumidity();
    float t = dht.readTemperature();

    if (isnan(h) || isnan(t)) {
      Serial.println(F("Failed to read from DHT sensor!"));
    } else {
      Serial.print(F("Humidity: "));
      Serial.print(h, 0);
      Serial.print(F("%\t"));
      Serial.print(F("Temperature: "));
      Serial.print(t, 0);
      Serial.println(F("ºC "));

      if (goodHumidity(h)) {
        if (goodTemperature(t)) {
          Serial.println(F("Perfect!"));
          digitalWrite(greenLedPin, HIGH);
          digitalWrite(blueLedPin, LOW);
          digitalWrite(yellowLedPin, LOW);
          digitalWrite(redLedPin, LOW);
        } else {
          Serial.println(F("Check Temperature!"));
          digitalWrite(greenLedPin, LOW);
          digitalWrite(blueLedPin, LOW);
          digitalWrite(yellowLedPin, HIGH);
          digitalWrite(redLedPin, HIGH);
        }
      } else {
        if (goodTemperature(t)) {
          Serial.println(F("Check Humidity!"));
          digitalWrite(greenLedPin, LOW);
          digitalWrite(blueLedPin, HIGH);
          digitalWrite(yellowLedPin, LOW);
          digitalWrite(redLedPin, HIGH);
        } else {
          Serial.println(F("Help!"));
          digitalWrite(greenLedPin, LOW);
          digitalWrite(blueLedPin, LOW);
          digitalWrite(yellowLedPin, LOW);
          digitalWrite(redLedPin, HIGH);
        }
      }
    }
  }
}

It looks like you could use a "state machine" to do what you want.

At any one time the program will be in one of several states depending on the sensor readings. So, read the sensors at the start of loop() and depending on their values set a variable, let's call in currentState, to a value representing the state. An enum of states is helpful in giving the current state a meaningful name.

Now use switch/case based on currentState to execute only the code relevant to that state. How do you blink the LEDs without interfering with the repeated reading of the sensors ? Use millis() for timing.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.