#include <LiquidCrystal.h>
#include <Servo.h>
// Pin definitions
const int bottleSensorPin = 2; // Digital pin for bottle detection
const int pumpRelayPin = 3; // Digital pin to control water pump
const int buttonPin = 4; // Button for manual activation
const int servoPin = 5; // Servo motor pin
// Initialize the LCD (adjust the pin numbers as necessary)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo sortingServo;
// Servo positions
const int bottlePosition = 0; // Position for bottles
const int canPosition = 90; // Position for cans
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(bottleSensorPin, INPUT);
pinMode(pumpRelayPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Pull-up resistor for button
digitalWrite(pumpRelayPin, LOW); // Ensure pump is off initially
// Initialize Servo
sortingServo.attach(servoPin);
sortingServo.write(bottlePosition); // Start at bottle position
// Initialize LCD
lcd.begin(16, 2);
lcd.print("System Ready");
delay(2000);
lcd.clear();
lcd.print("Insert a bottle");
}
void loop() {
// Check if a bottle is detected
if (digitalRead(bottleSensorPin) == HIGH) {
delay(1000); // Delay to avoid multiple triggers
// Sort item (bottle or can)
sortItem();
// Dispense water
dispenseWater();
// Give user time to remove the item
lcd.clear();
lcd.print("Take your item");
Serial.println("Take your item.");
delay(5000); // Allow user to take their item
lcd.clear();
lcd.print("Insert a bottle");
Serial.println("Insert a bottle.");
}
// Optional: Check if button is pressed for manual activation
if (digitalRead(buttonPin) == LOW) {
dispenseWater();
lcd.clear();
lcd.print("Water Dispensed");
Serial.println("Water dispensed manually.");
delay(2000);
lcd.clear();
lcd.print("Insert a bottle");
}
}
void sortItem() {
// Simulate detection (this is where you'd implement your actual logic)
bool isBottle = true; // Change this based on your detection logic
if (isBottle) {
sortingServo.write(bottlePosition); // Move to bottle position
lcd.clear();
lcd.print("Bottle Sorted");
Serial.println("Bottle sorted.");
} else {
sortingServo.write(canPosition); // Move to can position
lcd.clear();
lcd.print("Can Sorted");
Serial.println("Can sorted.");
}
// Return to default position
sortingServo.write(bottlePosition); // Return to bottle position after sorting
}
void dispenseWater() {
digitalWrite(pumpRelayPin, HIGH); // Turn on the pump
lcd.clear();
lcd.print("Dispensing Water");
Serial.println("Dispensing water...");
delay(5000); // Pump water for 5 seconds (adjust as necessary)
digitalWrite(pumpRelayPin, LOW); // Turn off the pump
}