#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// KILIZU hallway-light prototype. The LED is a low-voltage test load.
const uint8_t DARK_PIN = 34, MOTION_PIN = 27;
const uint8_t RELAY_PIN = 26, MODE_PIN = 14;
const uint32_t HOLD_MS = 4000;
enum Mode { AUTO, FORCE_ON, FORCE_OFF };
Mode mode = AUTO;
Adafruit_SSD1306 screen(128, 64, &Wire, -1);
bool dark = false, motion = false, lightOn = false;
bool motionSeen = false;
uint32_t lastMotion = 0, buttonChanged = 0, lastDisplay = 0;
bool rawButton = HIGH, stableButton = HIGH;
const char *modeName() {
if (mode == FORCE_ON) return "MANUAL ON";
if (mode == FORCE_OFF) return "MANUAL OFF";
return "AUTO";
}
void setLight(bool on) {
digitalWrite(RELAY_PIN, on ? HIGH : LOW); // Simulated NPN relay: active HIGH.
if (on != lightOn) Serial.printf("LIGHT %s | %s\n", on ? "ON" : "OFF", modeName());
lightOn = on;
}
void updateButton(uint32_t now) {
bool reading = digitalRead(MODE_PIN);
if (reading != rawButton) {
rawButton = reading;
buttonChanged = now;
}
if (now - buttonChanged >= 30 && reading != stableButton) {
stableButton = reading;
if (stableButton == LOW) {
mode = (Mode)((mode + 1) % 3);
motionSeen = false; // Do not reuse an old AUTO timer after a mode change.
Serial.printf("MODE: %s\n", modeName());
}
}
}
void updateLight(uint32_t now) {
if (mode == FORCE_ON) { setLight(true); return; }
if (mode == FORCE_OFF) { setLight(false); return; }
if (!dark) {
motionSeen = false; // Daylight cancels any remaining hold time.
setLight(false);
return;
}
if (motion) {
lastMotion = now; // Refresh while the PIR output stays HIGH.
motionSeen = true;
}
setLight(motionSeen && (uint32_t)(now - lastMotion) < HOLD_MS);
}
void drawScreen(uint32_t now) {
if (now - lastDisplay < 100) return;
lastDisplay = now;
screen.clearDisplay();
screen.setTextColor(SSD1306_WHITE);
screen.setTextSize(1);
screen.setCursor(3, 0);
screen.print("KILIZU HALLWAY");
screen.setCursor(3, 10);
screen.print(modeName());
screen.setTextSize(2);
screen.setCursor(3, 23);
screen.print(lightOn ? "LIGHT ON" : "LIGHT OFF");
screen.setTextSize(1);
screen.setCursor(3, 45);
screen.printf("%s PIR:%s", dark ? "DARK" : "BRIGHT", motion ? "YES" : "NO");
screen.setCursor(3, 56);
if (mode != AUTO) screen.print("BUTTON: NEXT MODE");
else if (!dark) screen.print("DAYLIGHT: OFF");
else if (lightOn) {
uint32_t left = HOLD_MS - (uint32_t)(now - lastMotion);
screen.printf("HOLD: %lus", (unsigned long)((left + 999) / 1000));
} else screen.print("WAITING FOR MOTION");
screen.display();
}
void setup() {
Serial.begin(115200);
digitalWrite(RELAY_PIN, LOW);
pinMode(RELAY_PIN, OUTPUT);
pinMode(DARK_PIN, INPUT);
pinMode(MOTION_PIN, INPUT);
pinMode(MODE_PIN, INPUT_PULLUP);
Wire.begin(21, 22);
Wire.setClock(400000);
if (!screen.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
Serial.println("OLED missing: light remains off");
while (true) delay(100);
}
Serial.println("READY: AUTO / ON / OFF; 4 second hold after PIR goes LOW");
}
void loop() {
uint32_t now = millis();
dark = digitalRead(DARK_PIN) == HIGH;
motion = digitalRead(MOTION_PIN) == HIGH;
updateButton(now);
updateLight(now);
drawScreen(now);
delay(5);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306
5V SIM SUPPLY
COMMON GROUND
DAY / NIGHT
LIGHT SENSOR
MOTION INPUT
LED LIGHT TEST
AUTO / ON / OFF