Programing and Electronics System Testing
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
#include <VL53L1X.h>
SoftwareSerial mySerial(D9, D10); // DFPlayer RX/TX
DFRobotDFPlayerMini myDFPlayer;
VL53L1X sensor;
const int trigPin1 = D1; // Ultrasonic 1 (front)
const int echoPin1 = D2;
const int servoPin = D8;
Servo dispenserServo;
const int closedAngle = 0;
const int openAngle = 180;
bool audio1Played = false;
bool audio2Played = false;
int prevDistance = 300;
int distance, currentLevel;
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT);
dispenserServo.attach(servoPin, 500, 2400);
dispenserServo.write(closedAngle);
Wire.begin();
Wire.setClock(400000); // Set I2C clock speed to 400kHz
sensor.setTimeout(500);
if (!sensor.init()) {
Serial.println("Failed to detect and initialize sensor!");
while (1);
}
sensor.setDistanceMode(VL53L1X::Long); // Set sensor to long distance mode
sensor.setMeasurementTimingBudget(50000); // Set timing budget (50ms)
sensor.startContinuous(50); // Start continuous measurements every 50ms
Serial.println(F("Initializing DFPlayer..."));
if (!myDFPlayer.begin(mySerial)) {
Serial.println(F("DFPlayer Mini not responding! Call Roshan, Something is Off ..."));
while (true);
}
myDFPlayer.volume(35);
Serial.println("✅ System ready.");
readCurrentLevel();
}
void readCurrentLevel(){
sensor.read(); // Get new sensor reading
currentLevel = sensor.ranging_data.range_mm; // Store measured distance
Serial.print("Current Waste Level: ");
Serial.println(currentLevel);
}
void loop() {
float PeopleDist = getPeopleDistance();
Serial.print("People: ");
Serial.println(PeopleDist);
// sensor.read(); // Get new sensor reading
// distance = sensor.ranging_data.range_mm; // Store measured distance
// Serial.print("Distance: ");
// Serial.println(distance);
// delay(1000);
// 🚫 If user walks away, reset all
if (PeopleDist > 10.0) {
Serial.println("No One");
audio1Played = false;
return;
}
else{
sensor.read(); // Get new sensor reading
distance = sensor.ranging_data.range_mm; // Store measured distance
Serial.print("Distance: ");
Serial.println(distance);
}
// 1️⃣ Step 1: Play "Tickle me" once
if (PeopleDist <= 10.0 && !audio1Played) {
myDFPlayer.play(5); // play first audio
Serial.println("👋 Audio 1: Welcome with Barking Note !");
audio1Played = true;
delay(5000); //adjust based on audio length
myDFPlayer.stop(); // This stops the current audio
}
Serial.println("END 3");
// 2️⃣ Step 2:
if (audio1Played && distance < currentLevel - 5) {
currentLevel = distance;
Serial.println("🕺 Audio 2: Thank you for Dumping waste - Reward at the End !");
myDFPlayer.play(4);
delay(8000); // Give time for audio
// myDFPlayer.stop(); // This stops the current audio
Serial.println("🕺 Audio 3: Information on Waste !");
int x = random(3, 6);
Serial.println(x);
myDFPlayer.play(3); // "I see you!"
delay(15000); // Give time for audio
myDFPlayer.stop(); // This stops the current audio
dispenseChocolate();
// delay(2000);
Serial.println("🕺 Audio 4: Ending with Applause !");
myDFPlayer.play(6); //Clapping
delay(6000); // Give time for audio
myDFPlayer.stop(); // This stops the current audio
audio1Played = false;
}
// delay(10);
}
float getPeopleDistance() {
return measureDistance(trigPin1, echoPin1);
}
float measureDistance(int trig, int echo) {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long duration = pulseIn(echo, HIGH, 30000);
if (duration == 0) return -1;
return duration * 0.034 / 2.0;
}
void dispenseChocolate() {
dispenserServo.write(openAngle);
delay(800);
dispenserServo.write(closedAngle);
Serial.println("✅ Chocolate dispensed.");
}
This is my short simulation video of final project in which it shows how my components all together integrates. As right now since I had just kept the distance shorter in my code so when I am showing my hand next to ultrasonic sensor, it detects the motion and speaker plays audio and then waits for some few seconds whether the user will throw thrash or not. If the user throws thrash than again the speaker plays another round of audio and then in the end he or she will be rewarded with chocolate. But in the other scenerio if the user doesnot show interest in throwing waste, then the cycle will just end there only.