#include <Arduino.h>
#include <DHT.h>
#define DHTPIN 15 //initials check them plzz in the diagram
#define DHTTYPE DHT22
#define RED_LED 26
#define GREEN_LED 27
#define BLUE_LED 25
#define BUZZER 16 //I somehow fixed the buzzer issue but a lot is not fixed :((
#define SWITCH_PIN 18
DHT dht(DHTPIN, DHTTYPE);
bool sleepMode = false;
bool lastSwitchState = HIGH;
unsigned long previousMillis = 0;
const long interval = 2000;
void setColor(int red, int green, int blue) {
analogWrite(RED_LED, red);
analogWrite(GREEN_LED, green);
analogWrite(BLUE_LED, blue);
}
void updateLED(float humidity) {
if (sleepMode) return; // I'm skipping rgb updates when in sleep mode
if (humidity < 40) {
setColor(0, 0, 255); // Blue (dry air)
Serial.println(" Blue ON (Dry Air)");
} else if (humidity <= 60) {
setColor(0, 255, 0); // Green (optimal humidity)
Serial.println(" Green ON (Optimal Humidity)");
} else {
setColor(255, 0, 0); // Red (high humidity)
Serial.println(" Red ON (High Humidity)");
}
}
void setup() {
Serial.begin(115200);
dht.begin();
delay(2000); // Allowing sensor to stabilize
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
// Ensuring all LEDs start OFF (for common cathode)
setColor(0, 0, 0);
digitalWrite(BUZZER, LOW); // Ensuring buzzer is off initially (otherwise it keeps beeping)
}
void loop() {
bool switchState = digitalRead(SWITCH_PIN);
if (switchState == LOW && lastSwitchState == HIGH) {
delay(50); // Debouncing the switch
if (digitalRead(SWITCH_PIN) == LOW) {
sleepMode = !sleepMode; // Toggling sleep mode
Serial.println(sleepMode ? "Entering Sleep Mode..." : "Waking Up...");
}
}
lastSwitchState = switchState;
if (sleepMode) {
setColor(0, 0, 0); // Turning off RGB LEDs
digitalWrite(BUZZER, LOW); // Ensuring buzzer is off when in sleep mode
noTone(BUZZER); // Explicitly stopping any tone that might be playing
return; // We exit loop early when in sleep mode (not annoying)
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
updateLED(humidity);
if (humidity > 60) {
tone(BUZZER, 1000); // Starting buzzer (beeping tone)
Serial.println(" Buzzer ON (High Humidity Alert)");
} else {
noTone(BUZZER); // Stopping buzzer if humidity is less than or equal to 60
}
}
}