#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define REED_SWITCH_PIN 2 // Pin where the reed switch is connected
#define DHTPIN 3 // Pin where the DHT22 sensor is connected
#define BUTTON_PIN 4 // Pin where the momentary button is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define LED_PIN 13 // Onboard LED pin
#define LED_PIN2 9 // External LED pin
bool ledState = LOW;
bool onboardLedState = LOW;
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis = 0; // For reed switch interval
unsigned long previousMillisforLED = 0; // For LED flashing interval
const long interval = 500; // Interval to check garage state (500 ms)
const long ledInterval = 2000; // Interval for LED flashing (2 seconds)
void setup() {
Serial.begin(9600);
dht.begin();
// Initialize reed switch and button pins as inputs
pinMode(REED_SWITCH_PIN, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize LED pins as outputs
pinMode(LED_PIN, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
// Initialize SSD1306 display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Pass 0x3C as the I2C address
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Halt if display initialization fails
}
// Initial display setup
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setFont(NULL);
display.display();
}
void loop() {
unsigned long currentMillis = millis();
// Check garage state every 500 milliseconds
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the last time the garage state was checked
// Read the state of the reed switch (garage open/shut)
bool reedState = digitalRead(REED_SWITCH_PIN);
// Garage door is open (reed switch LOW)
if (reedState == LOW) {
display.clearDisplay();
display.setTextSize(2); // Increase text size for "Garage open"
display.setCursor(0, 10);
display.println(" Garage");
display.println(" open");
display.display();
}
// Garage door is shut (reed switch HIGH)
else {
display.clearDisplay(); // Blank the display when the garage is shut
display.display();
// Turn off LEDs when garage is shut
digitalWrite(LED_PIN2, LOW);
digitalWrite(LED_PIN, LOW);
}
}
// Flash LEDs when the garage is open
bool reedState = digitalRead(REED_SWITCH_PIN); // Check reed switch state again
if (reedState == LOW && currentMillis - previousMillisforLED >= ledInterval) {
previousMillisforLED = currentMillis; // Save the last time the LEDs flashed
// Toggle the state of both LEDs
ledState = !ledState;
onboardLedState = !onboardLedState;
digitalWrite(LED_PIN2, ledState ? HIGH : LOW); // External LED
digitalWrite(LED_PIN, onboardLedState ? HIGH : LOW); // Onboard LED
}
// Read the state of the button (active LOW)
bool buttonState = digitalRead(BUTTON_PIN);
// If the garage is shut and the button is pressed, show temperature and humidity
if (reedState == HIGH && buttonState == LOW) {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Loading..."));
display.clearDisplay();
display.setCursor(0, 10);
display.println("Loading...");
display.display();
dht.begin(); // Reinitialize DHT sensor
} else {
display.clearDisplay();
display.setTextSize(2); // Set text size to normal for temperature and humidity
display.setCursor(0, 10);
display.print("T: ");
display.print(t);
display.println(" C");
display.setCursor(0, 30);
display.print("H: ");
display.print(h);
display.println(" %");
display.display();
}
// Display the temperature and humidity for 10 seconds
delay(10000);
display.clearDisplay();
display.display();
}
delay(100); // Small delay to avoid bouncing or rapid updates
}