#include <Servo.h>
#include <LiquidCrystal_I2C.h>
// Initialize the tools
Servo gateServo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set LCD address to 0x27
// Define our pins
const int sensorPin = A0;
int premiumCount = 0; // Counter for our top-tier Lampung crops
void setup() {
Serial.begin(9600);
// Attach the servo and set its starting neutral position
gateServo.attach(9);
gateServo.write(90); // 90 degrees = straight ahead (closed/neutral)
// Initialize the Dashboard screen
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Lampung Sorter");
lcd.setCursor(0, 1);
lcd.print("Ready to Process");
delay(2000);
lcd.clear();
}
void loop() {
// Read how dark/light the crop passing the sensor is (0 to 1023)
int cropQuality = analogRead(sensorPin);
// Update the Dashboard live data
lcd.setCursor(0, 0);
lcd.print("Reading: ");
lcd.print(cropQuality);
// LOGIC: Let's pretend a premium crop reflects light beautifully (Value under 400)
// And a defective dark crop blocks/absorbs light (Value 400 or above)
if (cropQuality < 400) {
// PREMIUM CROP DETECTED!
lcd.setCursor(0, 1);
lcd.print("Status: PREMIUM ");
gateServo.write(45); // Move gate to direct crop into Bin A
delay(1000); // Wait for crop to slide down
premiumCount++; // Add 1 to our industrial output log
gateServo.write(90); // Reset gate to center
}
else {
// DEFECTIVE / RAW UNPROCESSED CROP DETECTED
lcd.setCursor(0, 1);
lcd.print("Status: REJECTED");
gateServo.write(135); // Move gate the other way into Bin B
delay(1000); // Wait for crop to slide down
gateServo.write(90); // Reset gate to center
}
// Print our final metric to the screen
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Total Premium:");
lcd.setCursor(0, 1);
lcd.print(premiumCount);
delay(2000);
lcd.clear();
}