#define BLYNK_TEMPLATE_ID "TMPL6WOIdo-Gz"
#define BLYNK_TEMPLATE_NAME "Animal Feed System"
#define BLYNK_AUTH_TOKEN "0jzbPzGLbMZnKSdlAPcTzlyqQjbHDJ86"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const char* SSID = "Wokwi-GUEST"; // For simulation
const char* PASSWORD = ""; // No password needed for Wokwi-GUEST
// LCD (I2C address 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pins
#define BUZZER_PIN 18
#define SERVO_PIN 19
// Virtual Pins
#define VPIN_START V1
#define VPIN_FEED V2
#define VPIN_ENERGY V3
int happiness = 50;
int sadness = 50;
Servo myServo;
void setup() {
pinMode(32, OUTPUT);
Serial.begin(115200);
Serial.println("Connecting to WiFi...");
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi is connected.");
Serial.println("IP address:");
Serial.println(WiFi.localIP());
Blynk.begin(BLYNK_AUTH_TOKEN, SSID, PASSWORD);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Pet System Init");
myServo.attach(SERVO_PIN);
pinMode(BUZZER_PIN, OUTPUT);
delay(2000);
lcd.clear();
}
void loop() {
Blynk.run();
displayLevels();
}
// START Button: Reduce feed -> Decrease happiness, increase sadness
BLYNK_WRITE(VPIN_START) {
if (param.asInt() == 1) {
happiness = max(happiness - 10, 0);
sadness = min(sadness + 10, 100);
}
}
// FEED Button: Increase happiness, decrease sadness
BLYNK_WRITE(VPIN_FEED) {
if (param.asInt() == 1) {
happiness = min(happiness + 10, 100);
sadness = max(sadness - 10, 0);
if (happiness == 100) {
myServo.write(90); // Release pet
Blynk.notify("Pet is FREE! Happiness MAX!");
delay(2000);
myServo.write(0); // Reset position
}
}
}
// ENERGY Button: Decrease happiness, increase sadness
BLYNK_WRITE(VPIN_ENERGY) {
if (param.asInt() == 1) {
happiness = max(happiness - 10, 0);
sadness = min(sadness + 10, 100);
if (sadness == 100) {
digitalWrite(BUZZER_PIN, HIGH);
Blynk.notify("Danger! Pet is too sad!");
lcd.setCursor(0, 1);
lcd.print("Danger Condition!");
delay(2000);
digitalWrite(BUZZER_PIN, LOW);
}
}
}
void displayLevels() {
lcd.setCursor(0, 0);
lcd.print("Happy:");
lcd.print(happiness);
lcd.print(" Sad:");
lcd.print(sadness);
}