#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Pin button
#define BTN_LEFT 12
#define BTN_RIGHT 13
#define BTN_DOOR 14
#define BUZZER 2
// Game State
int cameraIndex = 0;
bool doorClosed = false;
int power = 100;
unsigned long lastAnimatronicMove = 0;
unsigned long lastSecond = 0;
int hour = 0; // 0 = 12AM, 6 = 6AM
int animatronicPos = 0; // 0 = far, 1 = hallway, 2 = office
bool gameOver = false;
// Room names
const char* rooms[] = {"Show Stage", "Dining", "Hallway", "Office"};
void setup() {
pinMode(BTN_LEFT, INPUT_PULLUP);
pinMode(BTN_RIGHT, INPUT_PULLUP);
pinMode(BTN_DOOR, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 init failed");
while (true);
}
display.clearDisplay();
display.display();
}
void drawUI() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Cam: ");
display.print(rooms[cameraIndex]);
display.setCursor(0, 10);
display.print("Animatronic: ");
display.print(rooms[animatronicPos]);
display.setCursor(0, 20);
display.print("Door: ");
display.print(doorClosed ? "Closed" : "Open");
display.setCursor(0, 30);
display.print("Power: ");
display.print(power);
display.print("%");
display.setCursor(0, 40);
display.print("Time: ");
display.print((hour == 0 ? 12 : hour));
display.print("AM");
if (gameOver) {
display.setCursor(30, 55);
display.print("GAME OVER!");
}
display.display();
}
void moveAnimatronic() {
if (millis() - lastAnimatronicMove > 4000 && !gameOver) {
lastAnimatronicMove = millis();
int move = random(0, 100);
if (move < 60 && animatronicPos < 3) {
animatronicPos++;
} else if (move > 85 && animatronicPos > 0) {
animatronicPos--;
}
// Check for game over
if (animatronicPos == 3 && !doorClosed) {
gameOver = true;
tone(BUZZER, 1000, 1000);
}
}
}
void updateTime() {
if (millis() - lastSecond > 1000 && !gameOver) {
lastSecond = millis();
static int seconds = 0;
seconds++;
if (seconds % 5 == 0 && hour < 6) {
hour++;
}
if (power > 0) {
power--;
} else {
gameOver = true;
tone(BUZZER, 200, 2000);
}
}
}
void checkButtons() {
static bool lastLeft = HIGH, lastRight = HIGH, lastDoor = HIGH;
bool left = digitalRead(BTN_LEFT);
bool right = digitalRead(BTN_RIGHT);
bool door = digitalRead(BTN_DOOR);
if (left == LOW && lastLeft == HIGH) {
cameraIndex = (cameraIndex - 1 + 3) % 3;
power -= 1;
}
if (right == LOW && lastRight == HIGH) {
cameraIndex = (cameraIndex + 1) % 3;
power -= 1;
}
if (door == LOW && lastDoor == HIGH) {
doorClosed = !doorClosed;
power -= 2;
}
lastLeft = left;
lastRight = right;
lastDoor = door;
}
void loop() {
checkButtons();
updateTime();
moveAnimatronic();
drawUI();
if (hour >= 6 && !gameOver) {
gameOver = true;
tone(BUZZER, 1200, 1500);
display.setCursor(30, 55);
display.print("YOU SURVIVED!");
display.display();
}
delay(100);
}