#include // Definition of pins and maximum distance #define TRIGGER_PIN D9 // GPIO pin for sensor Trigger pin #define ECHO_PIN D10 // GPIO pin for sensor Echo pin #define MAX_DISTANCE 30 // Maximum distance to measure in centimeters #define MIN_DISTANCE 5 // Minimum distance when the container is full (adjust according to your needs) #define LEVEL_RANGE (MAX_DISTANCE - MIN_DISTANCE) // Range of levels NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Sensor initialization void setup() { Serial.begin(115200); // Serial communication initialization } void loop() { delay(1900); // Wait one second between measurements unsigned int distance = sonar.ping_cm(); // Measure distance in centimeters if (distance == 0 || distance > MAX_DISTANCE) { Serial.println("Out of range"); // If the distance is 0 or greater than the maximum distance, indicate it's out of range } else { // Calculate relative fill level based on the measured distance int level = map(distance, MIN_DISTANCE, MAX_DISTANCE, 100, 0); // Map distance to a fill percentage Serial.print("Kibble level: "); Serial.print(level); Serial.println("%"); // Print kibble level } }