/*
Wokwi | questions
I need help asap!!
jasper - Monday, June 1, 2026 9:18 AM
https://wokwi.com/projects/465631428885803009
im doing a project on automatic food dispenser for stray animals,
im only focusing on using ultrasonic sensor to give detect the
remaning food level in the container and using a servo motor to
dispense food out...im controlling all these components using
Blynk app....my hardware works fine but the wokwi coding shows
error, pls help to create a proper coding for my simulation to
work, thank you!
*/
#define BLYNK_PRINT Serial
/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6i-wnkZx1"
#define BLYNK_TEMPLATE_NAME "food dispenser"
#define BLYNK_AUTH_TOKEN "fMLmk4Jy6WlQZgL_IRj-GPU3b5MWFECR"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
// pin constants
const int TRIG_PIN = 23;
const int ECHO_PIN = 22;
const int SERVO_PIN = 18;
// notification flag
bool notified = false;
Servo servo1;
// function to measure distance
int getDistance() {
// send trigger pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// read pulse duration
long duration = pulseIn(ECHO_PIN, HIGH);
// calculate distance
long distance = duration * 0.0343 / 2;
return (int)distance + 0.5;
}
// Slider widget for servo
BLYNK_WRITE(V2) {
int pos1 = param.asInt(); // Get value from slider
servo1.write(pos1); // Set servo 1 position
Blynk.virtualWrite(V3, pos1);
}
void setup() {
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, "Wokwi-GUEST", "");
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
servo1.attach(SERVO_PIN);
}
void loop() {
Blynk.run();
int distance = getDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Send to Blynk display
Blynk.virtualWrite(V1, distance);
// FOOD LOW CONDITION
if (distance > 10 && !notified) { // adjust value
Serial.println("Food is LOW!");
Blynk.logEvent("food_low", "Food container is empty!");
notified = true;
}
// Reset notification when refilled
if (distance < 5) {
notified = false;
}
delay(1000);
}