#include <Arduino.h>
#include <Wire.h>
#include <Keypad.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// KILIZU: light loss stands in for an interrupted beam in Wokwi.
const int LIGHT_PIN = 34, BUZZER_PIN = 25;
const int GREEN_PIN = 16, RED_PIN = 17;
const int LIGHT_LIMIT = 2200;
const uint32_t BREAK_MS = 250, EXIT_MS = 4000;
const char SECRET_PIN[] = "2580";
const byte ROWS = 4, COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'}, {'4', '5', '6'},
{'7', '8', '9'}, {'*', '0', '#'}
};
byte rowPins[ROWS] = {32, 33, 26, 27};
byte colPins[COLS] = {14, 18, 19};
Keypad keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
enum Mode { DISARMED, EXIT_DELAY, ARMED, ALARM };
Mode mode = DISARMED;
uint32_t modeAt = 0, darkAt = 0, hintAt = 0;
bool darkTiming = false;
int lightRaw = 0;
char entered[5] = {};
byte digits = 0;
const char *hint = nullptr;
void clearPin() {
digits = 0;
entered[0] = '\0';
}
void showHint(const char *text, uint32_t now) {
hint = text;
hintAt = now;
}
void changeMode(Mode next, uint32_t now) {
mode = next;
modeAt = now;
darkTiming = false;
clearPin();
Serial.printf("MODE=%d ADC=%d\n", int(mode), lightRaw);
}
void submitPin(uint32_t now) {
if (digits == 4 && strcmp(entered, SECRET_PIN) == 0) {
changeMode(DISARMED, now);
showHint("ACCESS GRANTED", now);
} else {
clearPin();
showHint("WRONG PIN", now); // The alarm state is unchanged.
}
}
void readKeypad(uint32_t now) {
char key = keypad.getKey();
if (!key) return;
if (key >= '0' && key <= '9' && digits < 4) {
entered[digits++] = key;
entered[digits] = '\0';
} else if (key == '#') {
submitPin(now);
} else if (key == '*') {
clearPin();
if (mode == DISARMED && lightRaw <= LIGHT_LIMIT) {
changeMode(EXIT_DELAY, now);
showHint("STEP AWAY", now);
} else if (mode == DISARMED) {
showHint("RESTORE LIGHT", now);
}
}
}
void checkLight(uint32_t now) {
if (mode == EXIT_DELAY && now - modeAt >= EXIT_MS)
changeMode(ARMED, now);
if (mode != ARMED) return;
if (lightRaw <= LIGHT_LIMIT) {
darkTiming = false;
return;
}
if (!darkTiming) { darkAt = now; darkTiming = true; }
if (now - darkAt >= BREAK_MS) changeMode(ALARM, now);
}
void updateOutputs(uint32_t now) {
bool green = mode == ARMED ||
(mode == EXIT_DELAY && (now / 250) % 2);
digitalWrite(GREEN_PIN, green);
digitalWrite(RED_PIN, mode == ALARM && (now / 150) % 2);
static int previousHz = 0;
int hz = mode == ALARM ? ((now / 240) % 2 ? 1100 : 1700) : 0;
if (hz == previousHz) return;
previousHz = hz;
if (hz) tone(BUZZER_PIN, hz);
else noTone(BUZZER_PIN);
}
void drawScreen(uint32_t now) {
static uint32_t drawnAt = 0;
if (now - drawnAt < 120) return;
drawnAt = now;
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("KILIZU / TRIPWIRE");
oled.drawFastHLine(0, 10, 128, SSD1306_WHITE);
oled.setTextSize(2);
oled.setCursor(0, 15);
if (mode == DISARMED) oled.print("DISARMED");
else if (mode == ARMED) oled.print("ARMED");
else if (mode == ALARM) oled.print("ALARM!");
else {
oled.print("ARMING ");
oled.print((EXIT_MS - min(EXIT_MS, now - modeAt) + 999) / 1000);
}
oled.setTextSize(1);
oled.setCursor(0, 35);
oled.print(lightRaw > LIGHT_LIMIT ? "LIGHT LOST " : "LIGHT OK ");
oled.print(lightRaw);
oled.setCursor(0, 46);
oled.print("PIN: ");
for (byte i = 0; i < digits; ++i) oled.print('*');
oled.setCursor(0, 56);
if (hint && now - hintAt < 1600) oled.print(hint);
else oled.print(mode == DISARMED ? "* ARM # ENTER" : "* CLEAR # ENTER");
oled.display();
}
void setup() {
Serial.begin(115200);
pinMode(GREEN_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
analogReadResolution(12);
analogSetPinAttenuation(LIGHT_PIN, ADC_11db);
Wire.begin(21, 22);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED not found");
while (true) delay(1000);
}
}
void loop() {
uint32_t now = millis();
lightRaw = analogRead(LIGHT_PIN);
readKeypad(now);
checkLight(now);
updateOutputs(now);
drawScreen(now);
delay(1);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306