#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Buttons
#define FEED 10
#define PLAY 11
#define SLEEP_BTN 12
#define STATUS 13
// Pet statistics
int hunger = 100;
int happiness = 100;
int energy = 100;
// Timers
unsigned long lastUpdate = 0;
unsigned long lastBlink = 0;
unsigned long actionUntil = 0;
const unsigned long UPDATE_INTERVAL = 5000;
const unsigned long ACTION_DURATION = 1500;
const unsigned long BLINK_INTERVAL = 400;
// States
bool showStats = false;
bool eyesClosed = false;
enum Action {
NONE,
EATING,
PLAYING,
SLEEPING
};
Action currentAction = NONE;
// Draw status icons
void drawIcons(unsigned long now) {
bool visible = (now / BLINK_INTERVAL) % 2 == 0;
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
// Food icon
if (hunger < 30 && !visible) {
// Blink off
} else {
oled.setCursor(4, 2);
oled.print("F");
}
// Happiness icon
if (happiness < 30 && !visible) {
// Blink off
} else {
oled.setCursor(20, 2);
oled.print("<3");
}
// Energy icon
if (energy < 30 && !visible) {
// Blink off
} else {
oled.setCursor(48, 2);
oled.print("Z");
}
oled.setCursor(91, 2);
oled.print("MILO");
}
// Draw Milo's character
void drawMilo() {
// Body
oled.fillRoundRect(39, 19, 50, 39, 12, SSD1306_WHITE);
// Ears
oled.fillTriangle(43, 24, 47, 11, 57, 23, SSD1306_WHITE);
oled.fillTriangle(71, 23, 81, 11, 85, 24, SSD1306_WHITE);
// Eyes
if (eyesClosed || currentAction == SLEEPING) {
oled.drawLine(49, 34, 56, 34, SSD1306_BLACK);
oled.drawLine(72, 34, 79, 34, SSD1306_BLACK);
} else if (hunger < 30) {
oled.drawCircle(53, 34, 3, SSD1306_BLACK);
oled.drawCircle(75, 34, 3, SSD1306_BLACK);
} else {
oled.fillCircle(53, 33, 3, SSD1306_BLACK);
oled.fillCircle(75, 33, 3, SSD1306_BLACK);
}
// Mouth
if (currentAction == PLAYING ||
(happiness >= 30 && hunger >= 30 && energy >= 30)) {
oled.drawLine(59, 43, 63, 47, SSD1306_BLACK);
oled.drawLine(63, 47, 67, 43, SSD1306_BLACK);
} else if (hunger < 30 || happiness < 30) {
oled.drawLine(59, 46, 67, 46, SSD1306_BLACK);
} else {
oled.fillCircle(63, 44, 2, SSD1306_BLACK);
}
}
// Main screen
void drawMainScreen(unsigned long now) {
oled.clearDisplay();
oled.drawLine(0, 12, 127, 12, SSD1306_WHITE);
drawIcons(now);
drawMilo();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(2, 56);
if (currentAction == EATING) {
oled.print("NOM NOM!");
} else if (currentAction == PLAYING) {
oled.print("WHEEEEE!");
} else if (currentAction == SLEEPING) {
oled.print("ZZZZZ...");
} else if (hunger < 30) {
oled.print("FEED ME PLEASE!");
} else if (energy < 30) {
oled.print("I'M SO SLEEPY!");
} else if (happiness < 30) {
oled.print("PLAY WITH ME!");
} else {
oled.print("I'M HAPPY!");
}
oled.display();
}
// Statistics screen
void drawStatsScreen() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(2, 0);
oled.print("MILO'S STATS");
oled.drawLine(0, 10, 127, 10, SSD1306_WHITE);
oled.setCursor(2, 16);
oled.print("HUNGER: ");
oled.print(hunger);
oled.print("%");
oled.setCursor(2, 29);
oled.print("HAPPY: ");
oled.print(happiness);
oled.print("%");
oled.setCursor(2, 42);
oled.print("ENERGY: ");
oled.print(energy);
oled.print("%");
oled.setCursor(2, 56);
oled.print("STATUS TO RETURN");
oled.display();
}
// Setup
void setup() {
pinMode(FEED, INPUT_PULLUP);
pinMode(PLAY, INPUT_PULLUP);
pinMode(SLEEP_BTN, INPUT_PULLUP);
pinMode(STATUS, INPUT_PULLUP);
Wire.setSDA(4);
Wire.setSCL(5);
Wire.begin();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
lastUpdate = millis();
lastBlink = millis();
drawMainScreen(millis());
}
// Main loop
void loop() {
unsigned long now = millis();
// Update pet needs
if (now - lastUpdate >= UPDATE_INTERVAL) {
lastUpdate = now;
if (hunger > 0) hunger = max(0, hunger - 2);
if (happiness > 0) happiness = max(0, happiness - 1);
if (energy > 0 && currentAction != SLEEPING) {
energy = max(0, energy - 1);
}
}
// Finish temporary actions
if (currentAction != NONE && now >= actionUntil) {
currentAction = NONE;
}
// Toggle statistics screen
if (!digitalRead(STATUS)) {
showStats = !showStats;
if (showStats) {
drawStatsScreen();
} else {
drawMainScreen(now);
}
delay(250);
while (!digitalRead(STATUS)) {
delay(10);
}
}
// Main screen interactions
if (!showStats) {
if (!digitalRead(FEED) && currentAction == NONE) {
hunger = min(100, hunger + 20);
currentAction = EATING;
actionUntil = now + ACTION_DURATION;
delay(200);
}
else if (!digitalRead(PLAY) && currentAction == NONE) {
happiness = min(100, happiness + 20);
energy = max(0, energy - 10);
currentAction = PLAYING;
actionUntil = now + ACTION_DURATION;
delay(200);
}
else if (!digitalRead(SLEEP_BTN) && currentAction == NONE) {
energy = min(100, energy + 20);
currentAction = SLEEPING;
actionUntil = now + ACTION_DURATION;
delay(200);
}
// Animate blinking
if (now - lastBlink >= BLINK_INTERVAL) {
lastBlink = now;
eyesClosed = !eyesClosed;
}
drawMainScreen(now);
}
else {
drawStatsScreen();
}
delay(30);
}
Loading
ssd1306
ssd1306