Preview:
#include <DHT.h>

#define DHTPIN 4         // Pin where the DHT11 is connected to the ESP32
#define DHTTYPE DHT11    // Type of DHT sensor used
#define LED_COUNT 4      // Number of LEDs connected
#define LED_START_PIN 13 // Starting pin for LEDs

DHT dht(DHTPIN, DHTTYPE);

const int ledPins[LED_COUNT] = {LED_START_PIN, LED_START_PIN + 1, LED_START_PIN + 2, LED_START_PIN + 3}; // Pins for 4 LEDs
int patternIndex = 0; // Index for the current lighting pattern
unsigned long patternStartTime = 0; // Variable to track the start time of the pattern
unsigned long offTime = 0; // Variable to track the LED off time

void setup() {
  Serial.begin(9600);
  dht.begin();

  for (int i = 0; i < LED_COUNT; ++i) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); // Ensure all LEDs are initially turned off
  }
}

void loop() {
  unsigned long currentMillis = millis();

  if (patternStartTime == 0) {
    patternStartTime = currentMillis;
  }

  unsigned long patternDuration = 5000; // Pattern duration: 5 seconds
  unsigned long offDuration = 3000; // LED off duration: 3 seconds

  // Execute pattern for 5 seconds
  if (currentMillis - patternStartTime < patternDuration) {
    executePattern();
  } else {
    // Turn off LEDs for 3 seconds after pattern execution
    if (offTime == 0) {
      turnOffAll();
      offTime = currentMillis;
    }

    if (currentMillis - offTime >= offDuration) {
      // Reset variables for the next pattern
      patternIndex = (patternIndex + 1) % 4; // Change to the next pattern
      patternStartTime = 0;
      offTime = 0;
      Serial.print("Pattern: ");
      Serial.println(patternIndex);
    }
  }

  readSensorData(); // Read sensor data and print
}

void executePattern() {
  switch (patternIndex) {
    case 0:
      allOn();
      break;
    case 1:
      alternate();
      break;
    case 2:
      blink();
      break;
    case 3:
      sequence();
      break;
  }
}

void readSensorData() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (!isnan(humidity) && !isnan(temperature)) {
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" °C");
  } else {
    Serial.println("Failed to read from DHT sensor!");
  }
  delay(2000);
}

// Functions for LED patterns (allOn, alternate, blink, sequence) remain the same as before.

void allOn() {
  for (int i = 0; i < LED_COUNT; ++i) {
    digitalWrite(ledPins[i], HIGH);
  }
}

void alternate() {
  for (int i = 0; i < LED_COUNT; i += 2) {
    digitalWrite(ledPins[i], HIGH);
    delay(100);
    digitalWrite(ledPins[i], LOW);
    delay(100);
  }
}

void blink() {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < LED_COUNT; ++j) {
      digitalWrite(ledPins[j], HIGH);
    }
    delay(300);
    for (int j = 0; j < LED_COUNT; ++j) {
      digitalWrite(ledPins[j], LOW);
    }
    delay(300);
  }
}

void sequence() {
  for (int i = 0; i < LED_COUNT; ++i) {
    digitalWrite(ledPins[i], HIGH);
    delay(300);
    digitalWrite(ledPins[i], LOW);
  }
}

void turnOffAll() {
  for (int i = 0; i < LED_COUNT; ++i) {
    digitalWrite(ledPins[i], LOW);
  }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter