#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <RTClib.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHTPIN 19 // Pin connected to the DHT22 sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
RTC_DS1307 rtc;
// Define button pins
#define button_up 14
#define button_down 27
#define button_enter 12
// Menu variables
int menuIndex = 0;
const int menuLength = 3; // Number of menu items
const char* menuItems[] = {"Clock", "Temp", "Humidity"};
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize the SSD1306 display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
// Initialize DHT sensor
dht.begin();
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// Comment out the following line once you set the correct time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Set button pins as input with pullup
pinMode(button_up, INPUT_PULLUP);
pinMode(button_down, INPUT_PULLUP);
pinMode(button_enter, INPUT_PULLUP);
// Display the initial menu
displayMenu();
}
void loop() {
// Check button presses
if (digitalRead(button_up) == LOW) {
menuIndex--;
if (menuIndex < 0) menuIndex = menuLength - 1;
displayMenu();
delay(200); // Debounce delay
}
if (digitalRead(button_down) == LOW) {
menuIndex++;
if (menuIndex >= menuLength) menuIndex = 0;
displayMenu();
delay(200); // Debounce delay
}
if (digitalRead(button_enter) == LOW) {
selectMenuItem();
delay(200); // Debounce delay
}
}
void displayMenu() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
for (int i = 0; i < menuLength; i++) {
if (i == menuIndex) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Highlight selected item
} else {
display.setTextColor(SSD1306_WHITE);
}
display.setCursor(0, i * 10);
display.print(menuItems[i]);
}
display.display();
}
void selectMenuItem() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
if (menuIndex == 0) {
// Display clock
DateTime now = rtc.now();
display.setCursor((SCREEN_WIDTH - 6 * 10) / 2, (SCREEN_HEIGHT - 16) / 2); // Center align
display.printf("%02d:%02d:%02d", now.hour(), now.minute(), now.second());
} else if (menuIndex == 1) {
// Display temperature
float temp = dht.readTemperature();
display.setCursor((SCREEN_WIDTH - 7 * 6) / 2, (SCREEN_HEIGHT - 16) / 2); // Center align
if (isnan(temp)) {
display.print("Error");
} else {
display.printf("Temp: %.1fC", temp);
}
} else if (menuIndex == 2) {
// Display humidity
float humidity = dht.readHumidity();
display.setCursor((SCREEN_WIDTH - 8 * 6) / 2, (SCREEN_HEIGHT - 16) / 2); // Center align
if (isnan(humidity)) {
display.print("Error");
} else {
display.printf("Humidity: %.1f%%", humidity);
}
}
display.display();
delay(1000); // Show the selected item for 1 second
// After showing the selected item, return to the menu
displayMenu();
}