#define BLYNK_TEMPLATE_NAME "Cat Feeder"
#define BLYNK_AUTH_TOKEN "3ZN1hubxV62y7SPnYxZKFFqxuplX9C5Q"
#include <WiFi.h>
#include <ESP32Servo.h>
#include <BlynkSimpleEsp32.h>
// Ultrasonic sensor pins
const int ECHO_PIN = 19;
const int TRIG_PIN = 18;
long duration;
int distance;
int containerHeight = 30; // in centimeters
int lowLevelThreshold = 5; // in centimeters
Servo myservo; // Servo for dispensing food
Servo bowlServoIn; // Servo for moving bowl in
Servo bowlServoOut; // Servo for moving bowl out
BlynkTimer timer;
int statusFeeding = 0; // Variable to store the feeding status
char ssid[] = "iPhone";
char pass[] = "yan12345678";
// Function to get the distance from the ultrasonic sensor
float getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(4);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2; // Calculate distance in cm
return distance;
}
// Function to check the food level and send notifications
void checkFoodLevel() {
float distance = getDistance();
Serial.print("Food Level Distance: ");
Serial.println(distance);
Blynk.virtualWrite(V3, distance); // Send distance to Blynk
static bool notificationSent = false; // Flag to track notification status
if (distance < (containerHeight - lowLevelThreshold) && !notificationSent) {
Blynk.logEvent("food_level_", "Low food level detected");
notificationSent = true; // Set flag to true after sending notification
} else if (distance >= (containerHeight - lowLevelThreshold) && notificationSent) {
notificationSent = false; // Reset flag if distance goes above threshold
}
}
void setup() {
myservo.attach(12);
bowlServoIn.attach(22);
bowlServoOut.attach(23);
myservo.write(0);
bowlServoIn.write(0);
bowlServoOut.write(0); // Ensure bowl is inside initially
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nWiFi Connected");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("Blynk Connected");
Serial.println("Setup completed");
// Setup timers
timer.setInterval(5000L, checkFoodLevel); // Check food level every 5 seconds
}
// Function to move the bowl
void moveBowl(bool out) {
int angle = out ? 90 : 0;
bowlServoOut.write(angle);
bowlServoIn.write(angle);
delay(1000); // Adjust the delay to ensure complete movement
}
// Function to start the feeding process
void startFeedingProcess() {
Serial.println("Starting feeding process...");
myservo.write(180); // Move servo to 180 degrees to dispense food
delay(5000); // Dispense food for 5 seconds (adjust as needed)
myservo.write(0); // Move servo back to 0 degrees
delay(10000);
moveBowl(true); // Move the bowl out
delay(10000); // Wait for 10 minutes (600,000 milliseconds)
moveBowl(false); // Move the bowl back in
statusFeeding = 0; // Reset the statusFeeding variable to 0
Blynk.virtualWrite(V0, 0);
Serial.println("Feeding process completed.");
}
void loop() {
Blynk.run();
timer.run(); // Run the BlynkTimer
if (statusFeeding == 1) {
startFeedingProcess();
}
}
BLYNK_WRITE(V0) {
statusFeeding = param.asInt();
}
actually this is my code for smart pet feeder but i just want to ask you how to make it this ultrasonic sensor can detect smoothly the food level and we can see the level through blynk apps? can you help me?