// Pocket Pet - a beginner electronics workshop demo
// Face + 3 stat bars on the OLED. Joystick up = feed,
// right = play, down (hold) = nap. Button = beep twice.
// The LED lights up when any stat drops below 25%.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDR 0x3c
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Pins - see diagram.json
const int JOY_X = A1; // joystick HORZ, left / right
const int JOY_Y = A2; // joystick VERT, up / down
const int JOY_SW = 3; // joystick click (SEL)
const int BUTTON_PIN = 2;
const int BUZZER_PIN = 8;
const int LED_PIN = 6;
// Pet stats, 0 to 100
int hunger = 80;
int happy = 80;
int energy = 80;
unsigned long lastDrain = 0;
unsigned long lastAction = 0;
unsigned long lastNap = 0;
unsigned long happyUntil = 0;
unsigned long jumpUntil = 0;
bool sleeping = false;
bool lastButton = HIGH;
bool lastJoySw = HIGH;
void drawBar(int y, const char *label, int value) {
display.setCursor(0, y);
display.print(label);
display.drawRect(30, y, 86, 6, SSD1306_WHITE);
int w = value * 82 / 100;
if (w > 0) display.fillRect(32, y + 1, w, 4, SSD1306_WHITE);
}
void drawFace() {
bool isHappy = millis() < happyUntil;
int jump = (millis() < jumpUntil) ? -6 : 0;
int eyeY = 16 + jump;
if (sleeping) {
// Closed eyes: two short lines
display.drawLine(38, eyeY, 52, eyeY, SSD1306_WHITE);
display.drawLine(76, eyeY, 90, eyeY, SSD1306_WHITE);
} else {
// Two round eyes
display.fillCircle(45, eyeY, 7, SSD1306_WHITE);
display.fillCircle(83, eyeY, 7, SSD1306_WHITE);
}
if (isHappy) {
// Smile: draw a circle, then cover its top half
display.drawCircle(64, 28 + jump, 10, SSD1306_WHITE);
display.fillRect(53, 17 + jump, 22, 11, SSD1306_BLACK);
} else {
// Straight mouth
display.drawLine(56, 34 + jump, 72, 34 + jump, SSD1306_WHITE);
}
}
void drawScreen() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
drawFace();
drawBar(40, "HUN", hunger);
drawBar(48, "HAP", happy);
drawBar(56, "NRG", energy);
display.display();
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(JOY_SW, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println("OLED failed - check wiring and address 0x3c");
for (;;) {}
}
drawScreen();
}
void loop() {
unsigned long now = millis();
// Stats drain slowly: 1 point every 3 seconds
if (now - lastDrain >= 3000) {
lastDrain = now;
hunger = max(0, hunger - 1);
happy = max(0, happy - 1);
energy = max(0, energy - 1);
}
int x = analogRead(JOY_X);
int y = analogRead(JOY_Y);
sleeping = false;
// One push = one action (700 ms cooldown)
if (now - lastAction > 700) {
if (y < 300) { // Push up = feed
hunger = min(100, hunger + 25);
happy = min(100, happy + 5);
happyUntil = now + 1500;
lastAction = now;
Serial.println("Feed!");
} else if (x > 700) { // Push right = play
happy = min(100, happy + 25);
jumpUntil = now + 600;
happyUntil = now + 1500;
lastAction = now;
Serial.println("Play!");
}
}
// Push down and hold = nap: energy refills, eyes close
if (y > 700) {
sleeping = true;
if (now - lastNap >= 200) {
lastNap = now;
energy = min(100, energy + 2);
}
}
// Click the joystick = play too
bool joySw = digitalRead(JOY_SW);
if (joySw == LOW && lastJoySw == HIGH && now - lastAction > 700) {
happy = min(100, happy + 10);
jumpUntil = now + 600;
happyUntil = now + 1500;
lastAction = now;
}
lastJoySw = joySw;
// Button press = beep twice
bool btn = digitalRead(BUTTON_PIN);
if (btn == LOW && lastButton == HIGH) {
tone(BUZZER_PIN, 880, 120);
delay(180);
tone(BUZZER_PIN, 880, 120);
}
lastButton = btn;
// LED warns when any stat is low
digitalWrite(LED_PIN, (hunger < 25 || happy < 25 || energy < 25) ? HIGH : LOW);
drawScreen();
delay(50);
}
Loading
ssd1306
ssd1306