#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SD.h>
#include <toneAC.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// OLED Configuration
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Button Pins
const int feedButton = 5;
const int waterButton = 4;
const int modeButton = 0;
const int breedButton = 14; // D5
const int cleanButton = 12; // D6
const int buzzerPin = 2; // D4
bool storyMode = false;
// Pet variables
int hunger;
int thirst;
int health;
int happiness; // New variable for mood
// Rabbit breeding variables
int rabbitCount = 1; // Initial rabbit count
unsigned long breedingStartTime = 0;
bool breedingMode = false;
// Cleaning variables
unsigned long lastCleaningTime = 0;
const unsigned long cleaningInterval = 86400000; // One day in milliseconds
const int cleaningDuration = 10000; // Cleaning event lasts for 10 seconds
bool cleaningMode = false;
// Garden variables
bool isGardenWatered = false;
// File path for storing and retrieving pet data on the SD card
const char *filePath = "/petdata.txt";
const char *storyFilePath = "/stories.txt";
// Max length of a story
const int maxStoryLength = 500;
char stories[10][maxStoryLength]; // Array to store 10 stories
void loadPetData() {
// Implementation of loadPetData function
}
void savePetData() {
// Implementation of savePetData function
}
void displayPetStatus() {
// Implementation of displayPetStatus function
}
void displayRabbit() {
// Implementation of displayRabbit function
}
void displayHealth() {
// Implementation of displayHealth function
}
void displayFoodWaterLevel() {
// Implementation of displayFoodWaterLevel function
}
void updateDayNightCycle() {
// Implementation of updateDayNightCycle function
}
void updateSeasonalChanges() {
// Implementation of updateSeasonalChanges function
}
void checkCleaningEvent() {
// Implementation of checkCleaningEvent function
}
void displayGarden() {
// Implementation of displayGarden function
}
void playStory() {
// Implementation of playStory function
}
void updatePet() {
// ... (Previous code)
// Add mood indicators
if (rabbitCount > 0) {
if (happiness > 70) {
// Happy mood: animated tail or hopping animation
} else if (happiness > 30) {
// Neutral mood: standard pose
} else {
// Unhappy mood: sad expression
}
}
// Save pet data to SD card
savePetData();
}
void loadStories() {
File storyFile = SD.open(storyFilePath);
if (storyFile) {
for (int i = 0; i < 10 && storyFile.available(); ++i) {
storyFile.readBytesUntil('\n', stories[i], maxStoryLength);
}
storyFile.close();
} else {
Serial.println(F("Failed to open stories file"));
}
}
void setup() {
if (!display.begin()) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Tamagotchi Pet");
pinMode(feedButton, INPUT);
pinMode(waterButton, INPUT);
pinMode(modeButton, INPUT);
pinMode(breedButton, INPUT); // New button for breeding
pinMode(cleanButton, INPUT); // New button for cleaning
pinMode(buzzerPin, OUTPUT);
// Initialize SD card
if (!SD.begin(SS)) {
Serial.println(F("SD card initialization failed!"));
for (;;)
;
}
// Load pet data from SD card
loadPetData();
// Load stories from SD card
loadStories();
}
void loop() {
if (!storyMode) {
updatePet();
displayPetStatus();
displayRabbit();
displayHealth();
displayFoodWaterLevel();
updateDayNightCycle();
updateSeasonalChanges();
checkCleaningEvent();
displayGarden(); // Display the garden with watering status
} else {
playStory();
}
// Display the content on the OLED
display.display();
// Sleep until a button is pressed
esp_deep_sleep_start();
}