#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
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis = 0; // Stores last time the garage state was checked
const long interval = 10; // Interval to check garage state (5 seconds)
void setup() {
Serial.begin(9600);
dht.begin();
// Initialize reed switch pin as input
pinMode(REED_SWITCH_PIN, INPUT_PULLUP);
// Initialize button pin as input with internal pull-up
pinMode(BUTTON_PIN, INPUT_PULLUP);
// 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(); // Get the current time
// Check garage state every 5 seconds
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 HIGH)
if (reedState == HIGH) {
display.clearDisplay();
display.setTextSize(2); // Increase text size for "Garage open"
display.setCursor(0, 10);
display.println(" Garage");
display.println(" open");
display.display();
// Serial.println("Garage open");
digitalWrite(LED_PIN, HIGH);
}
// Garage door is shut (reed switch LOW)
else {
display.clearDisplay(); // Blank the display when the garage is shut
display.display();
// Serial.println("Garage shut");
digitalWrite(LED_PIN, LOW);
}
}
// 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
bool reedState = digitalRead(REED_SWITCH_PIN); // Get reed state again
if (reedState == LOW && buttonState == LOW) {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
display.setCursor(0, 10);
display.println("DHT read fail");
} else {
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(" %");
// Serial.print("Temp: ");
// Serial.print(t);
// Serial.print(" C, Humidity: ");
// Serial.println(h);
}
// Display the temperature and humidity for 10 seconds
display.display();
delay(6000);
// After 10 seconds, blank the screen again
display.clearDisplay();
display.display();
}
delay(100); // Small delay to avoid bouncing or rapid updates
}