Skip to content

Commit cf3ffad

Browse files
committed
Add SensorMenu example with DHT11, HC-SR04
1 parent ce788fa commit cf3ffad

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

examples/BasicMenu/BasicMenu.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void onMainSelect(int index) {
8989
display.setCursor(0, 0);
9090
display.println("OLED MenuUI v1");
9191
display.setCursor(0, 16);
92-
display.println("@sudoyasir"); // You can change or remove it
92+
display.println("@sudoyasir"); //You can change or remove it
9393
display.display();
9494

9595
while (digitalRead(BUTTON_BACK) == HIGH); // Wait for back press

examples/SensorMenu/SensorMenu.ino

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#include <Adafruit_GFX.h>
2+
#include <Adafruit_SSD1306.h>
3+
#include <DHT.h>
4+
#include <EEPROM.h>
5+
#include "MenuUI.h"
6+
7+
// OLED configuration
8+
#define SCREEN_WIDTH 128
9+
#define SCREEN_HEIGHT 32
10+
#define OLED_RESET -1
11+
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
12+
13+
// Pins
14+
#define BUTTON_UP 2
15+
#define BUTTON_DOWN 3
16+
#define BUTTON_SELECT 4
17+
#define BUTTON_BACK 5
18+
#define BUZZER_PIN 6
19+
#define DHTPIN 7
20+
#define DHTTYPE DHT11
21+
#define TRIG_PIN 8
22+
#define ECHO_PIN 9
23+
24+
// Components
25+
DHT dht(DHTPIN, DHTTYPE);
26+
MenuUI menu(&display, BUTTON_UP, BUTTON_DOWN, BUTTON_SELECT, BUTTON_BACK, BUZZER_PIN);
27+
28+
// EEPROM addresses
29+
#define EEPROM_SOUND_ADDR 0
30+
#define EEPROM_BRIGHT_ADDR 1
31+
32+
// State tracking
33+
bool soundOn = true;
34+
bool brightnessHigh = true;
35+
36+
// Menu state
37+
enum ScreenState {
38+
SCREEN_MENU,
39+
SCREEN_TEMP,
40+
SCREEN_HUMID,
41+
SCREEN_DIST,
42+
SCREEN_ABOUT
43+
};
44+
ScreenState currentScreen = SCREEN_MENU;
45+
unsigned long lastUpdate = 0;
46+
47+
// Menu items
48+
const char* mainMenu[] = {
49+
"Temperature",
50+
"Humidity",
51+
"Distance",
52+
"Settings",
53+
"About"
54+
};
55+
56+
const char* settingsMenu[] = {
57+
"Sound: ON",
58+
"Brightness: HIGH"
59+
};
60+
61+
void updateSettingsMenuLabels() {
62+
settingsMenu[0] = soundOn ? "Sound: ON" : "Sound: OFF";
63+
settingsMenu[1] = brightnessHigh ? "Brightness: HIGH" : "Brightness: LOW";
64+
}
65+
66+
void setup() {
67+
Serial.begin(9600);
68+
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
69+
dht.begin();
70+
pinMode(TRIG_PIN, OUTPUT);
71+
pinMode(ECHO_PIN, INPUT);
72+
73+
// Read EEPROM settings
74+
soundOn = EEPROM.read(EEPROM_SOUND_ADDR) == 1;
75+
brightnessHigh = EEPROM.read(EEPROM_BRIGHT_ADDR) == 1;
76+
77+
// Apply settings
78+
menu.setBeepEnabled(soundOn);
79+
menu.setBrightnessLevel(brightnessHigh ? 255 : 10);
80+
81+
menu.begin();
82+
updateSettingsMenuLabels();
83+
menu.setMenuItems(mainMenu, 5);
84+
menu.setCallback(onMainSelect);
85+
menu.setBackCallback(nullptr);
86+
}
87+
88+
void loop() {
89+
if (currentScreen == SCREEN_MENU) {
90+
menu.update();
91+
} else {
92+
updateScreen();
93+
if (digitalRead(BUTTON_BACK) == LOW) {
94+
delay(200); // simple debounce
95+
backToMain();
96+
currentScreen = SCREEN_MENU;
97+
}
98+
}
99+
}
100+
101+
void updateScreen() {
102+
unsigned long now = millis();
103+
if (now - lastUpdate < 1000) return;
104+
lastUpdate = now;
105+
106+
display.clearDisplay();
107+
display.setTextSize(1);
108+
display.setTextColor(SSD1306_WHITE);
109+
110+
if (currentScreen == SCREEN_TEMP) {
111+
float t = dht.readTemperature();
112+
display.setCursor(0, 0);
113+
display.print("Temp (C)");
114+
display.setCursor(0, 16);
115+
display.setTextSize(2);
116+
display.print(isnan(t) ? 0 : t);
117+
118+
} else if (currentScreen == SCREEN_HUMID) {
119+
float h = dht.readHumidity();
120+
display.setCursor(0, 0);
121+
display.print("Humidity (%)");
122+
display.setCursor(0, 16);
123+
display.setTextSize(2);
124+
display.print(isnan(h) ? 0 : h);
125+
126+
} else if (currentScreen == SCREEN_DIST) {
127+
long duration, distance;
128+
digitalWrite(TRIG_PIN, LOW);
129+
delayMicroseconds(2);
130+
digitalWrite(TRIG_PIN, HIGH);
131+
delayMicroseconds(10);
132+
digitalWrite(TRIG_PIN, LOW);
133+
duration = pulseIn(ECHO_PIN, HIGH);
134+
distance = duration * 0.034 / 2;
135+
136+
display.setCursor(0, 0);
137+
display.print("Distance (cm)");
138+
display.setCursor(0, 16);
139+
display.setTextSize(2);
140+
display.print(distance);
141+
142+
} else if (currentScreen == SCREEN_ABOUT) {
143+
display.setCursor(0, 0);
144+
display.setTextSize(1);
145+
display.println("OLED Menu UI V1");
146+
display.setCursor(0, 16);
147+
display.println("@sudoyasir");
148+
}
149+
150+
display.display();
151+
}
152+
153+
void backToMain() {
154+
updateSettingsMenuLabels();
155+
menu.setMenuItems(mainMenu, 5, false);
156+
menu.setCallback(onMainSelect);
157+
menu.setBackCallback(nullptr);
158+
}
159+
160+
void backToSettings() {
161+
updateSettingsMenuLabels();
162+
menu.setMenuItems(settingsMenu, 2, false);
163+
menu.setCallback(onSettingsSelect);
164+
menu.setBackCallback(backToMain);
165+
}
166+
167+
void onMainSelect(int index) {
168+
switch (index) {
169+
case 0: currentScreen = SCREEN_TEMP; break;
170+
case 1: currentScreen = SCREEN_HUMID; break;
171+
case 2: currentScreen = SCREEN_DIST; break;
172+
case 3:
173+
updateSettingsMenuLabels();
174+
menu.setMenuItems(settingsMenu, 2);
175+
menu.setCallback(onSettingsSelect);
176+
menu.setBackCallback(backToMain);
177+
break;
178+
case 4: currentScreen = SCREEN_ABOUT; break;
179+
}
180+
lastUpdate = 0; // force refresh
181+
}
182+
183+
void onSettingsSelect(int index) {
184+
if (index == 0) {
185+
soundOn = !soundOn;
186+
EEPROM.write(EEPROM_SOUND_ADDR, soundOn ? 1 : 0);
187+
menu.setBeepEnabled(soundOn);
188+
} else if (index == 1) {
189+
brightnessHigh = !brightnessHigh;
190+
EEPROM.write(EEPROM_BRIGHT_ADDR, brightnessHigh ? 1 : 0);
191+
menu.setBrightnessLevel(brightnessHigh ? 255 : 10);
192+
}
193+
updateSettingsMenuLabels();
194+
menu.setMenuItems(settingsMenu, 2, false);
195+
}

0 commit comments

Comments
 (0)