May i know your thoughts on my program, because for me i think on case botready and case charging is kinda messy
/*
SMART MATERIAL SORTER - AUTOMATED WASTE SEGREGATION SYSTEM
Program Description:
This program automates the process of sorting waste materials (metal and non-metal)
and rewards users with phone charging time in return. The system uses capacitive and inductive sensors
to identify the type of waste, and IR sensors to detect if bins are full.
Servo motors manage the waste sorting mechanism and lid control.
A pushbutton allows users to start or stop the charging process, while an LCD provides instructions,
timer updates, and status messages. The relay module controls power delivery to the charging port.
Key Features:
- Material detection using a combination of capacitive and inductive proximity sensors.
- Automatic sorting into metal or non-metal bins via servo-controlled mechanisms
- Overflow detection for both bins using infrared sensors to prevent overfilling.
- Pushbutton-controlled charging that activates a countdown timer and can be extended with more waste input.
- User feedback via LCD, displaying instructions, sorting status, and countdown during charging.
- Relay and LED control for managing and indicating charger activity.
- Timeout mechanism resets the system if no activity is detected within a specific time.
*/
// -----------------------------------------------------------------------------
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Re-named servo for clarity
LiquidCrystal_I2C lcd(0x27,16,2);
Servo servoLid;
Servo servoSeg;
// Sensor Pins
const int indSensorPin = 2; // Inductive
const int capSensorPin = 3; // Capacitive
const int overflowMetal = 4; // Ir-Sensor
const int overflowNon = 5; // Ir-Sensor
// Servo Pins
const int servoLidPin = 6;
const int servoSegPin = 7;
//Servo Positions
const int srvoClosed = 0; // Lid Closed
const int srvoOpen = 180; // Lid Open
const int binCenter = 90;
const int binMetal = 135;
const int binNonMetal = 45;
// Button Pins
const int chargeBut = 8; // Start/Stop Button
// Charger Controls Pins
const int chargerRelay = 11; // Power Control
const int greenLED = 13; // Status Light
// Charing Time Countdown
int countdown = 0;
const int addTime = 60;
const unsigned long msecTimer = 1000;
unsigned long msec1 = 0;
// Inactivity Timeout
const unsigned long TimeOutInac = 20000; // 20 seconds till timeout
bool wasteaddedprev = false;
// Delay Time
const int dt = 500; // Delay for 0.5 seconds
const int dtinac = 2000; // Delay for 2 seconds
const int dtdone = 3000; // Delay for 3 seconds
// Switch Statement Cases
enum{Prompt,Idle,BotReady,Charging,Wait,Overflow};
int state;
// -----------------------------------------------------------------------------
void setup() {
// LCD Startup
lcd.begin(16,2);
lcd.backlight();
// Servo Startup
servoLid.attach(servoLidPin);
servoSeg.attach(servoSegPin);
servoLid.write(srvoClosed); // Calibration check
servoSeg.write(90); // Calibration check
// Sensor Startup
pinMode(indSensorPin, INPUT); // Inductive Sensor
pinMode(capSensorPin, INPUT); // Capacitive Sensor
pinMode(overflowMetal, INPUT); // Ir-Sensor
pinMode(overflowNon, INPUT); // Ir-Sensor
// Charger Controls Startup
pinMode(chargerRelay, OUTPUT); // Power Control
pinMode(greenLED, OUTPUT); // Status Light
digitalWrite(chargerRelay, HIGH); // Calibration check - Active on low
digitalWrite(greenLED, LOW); // Calibration check
// Button Startup
pinMode(chargeBut, INPUT_PULLUP); // Start Button
//Serial Monitor Startup
Serial.begin(9600);
Serial.println("Charging Station Initializing...");
state = Prompt;
}
// -----------------------------------------------------------------------------
// LCD Function
void lcdDisp(const char *s0,const char *s1){
lcd.clear();
if(s0){
lcd.setCursor(0,0);
lcd.print(s0);
}
if(s1){
lcd.setCursor(0,1);
lcd.print(s1);
}
}
// -----------------------------------------------------------------------------
// Servo Function
void sortBin(int binPos){
servoSeg.write(binPos);
delay(dt);
servoLid.write(srvoOpen);
delay(dt);
servoLid.write(srvoClosed);
delay(dt);
servoSeg.write(binCenter);
}
// -----------------------------------------------------------------------------
void loop() {
unsigned long msec = millis();
//Sensor State
bool indDetected = !digitalRead(indSensorPin); // Active on LOW
bool capDetected = digitalRead(capSensorPin); // Active on HIGH
bool Moverflow = !digitalRead(overflowMetal); //Overflow Detection
bool Noverflow = !digitalRead(overflowNon); //Overflow Detection
// Overflow Warning
if (Moverflow || Noverflow){
servoSeg.write(binCenter);
servoLid.write(srvoClosed);
lcdDisp("Trash Bin Full",nullptr);
Serial.println("state Overflow - Trash Bin Full Please Empty");
state = Overflow;
return;
}
// -----------------------------------------------------------------------------
switch(state){
case Prompt:// Waiting for a waste to be inserted
Serial.println("State Idle");
Serial.println(" Insert waste");
lcdDisp("Please Insert A","Waste Material");
state = Idle;
break;
// -----------------------------------------------------------------------------
case Idle: // Waste inserted ready to charge phone
// Sorting Functions
if (capDetected && !indDetected) { // Non-Metal Detected
lcdDisp("Sorting:"," Non-Metal");
Serial.println("state BotReady - Non-Metal Detected");
sortBin(binNonMetal); // Segway to non-metal bin
state = BotReady;
}
else if (capDetected && indDetected) { // Metal Detected
lcdDisp("Sorting: "," Metal");
Serial.println("state BotReady - Metal Detected");
sortBin(binMetal); // Segway to metal bin
state = BotReady;
}
break;
// -----------------------------------------------------------------------------
case BotReady: // Station buttons
if (digitalRead(chargeBut) == LOW){ // Charging starts
lcdDisp("Charging Activated","Connect Phone");
Serial.println("state Charging");
Serial.println("Button Pressed - Start Timer");
countdown = addTime;
msec1 = msec;
digitalWrite(chargerRelay,LOW);
digitalWrite(greenLED,HIGH);
state = Charging;
}
if (msec - msec1 >=TimeOutInac){ // Timeout if no actions was made
digitalWrite(chargerRelay,HIGH);
digitalWrite(greenLED,LOW);
lcdDisp(" Inactivity","Restart Required");
Serial.println ("state Prompt - Inactivity");
delay(dtinac);
msec1 = msec;
state = Prompt;
}
break;
// -----------------------------------------------------------------------------
case Charging: // Charging phone
bool wasteadded = (capDetected && indDetected) || (capDetected && !indDetected);
if (wasteadded && !wasteaddedprev){ // Timer added when waste insert
countdown = countdown + addTime;
msec1 = msec;
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
Serial.println("Time Added");
}
wasteaddedprev = wasteadded;
if (msec - msec1 >= msecTimer){ // Timer countdown
countdown--;
msec1 = msec;
lcd.clear();
lcd.print("Timer left:");
lcd.setCursor(0,1);
lcd.print(countdown);
}
if (countdown <= 0){
msec1 = msec;
digitalWrite(chargerRelay,HIGH);
digitalWrite(greenLED,LOW);
lcdDisp(" Charging Done",nullptr);
Serial.println("state Wait - Charging Timer Done");
state = Wait;
}
if (digitalRead(chargeBut) == LOW){ // Stop button
msec1 = msec;
countdown = 0;
digitalWrite(chargerRelay,HIGH);
digitalWrite(greenLED,LOW);
lcdDisp("Charging Stopped",nullptr);
Serial.println("state Wait - Time stopped by user");
state = Wait;
}
break;
// -----------------------------------------------------------------------------
case Wait:
lcdDisp("Unplug Charger"," Thank You!");
delay(dtdone);
state = Prompt;
break;
// -----------------------------------------------------------------------------
case Overflow: // Overflow Warning
if (!Moverflow && !Noverflow){
lcdDisp("Trash Bin Empty"," Thank You");
Serial.println("State Prompt - Trash Bin Emptied");
state = Prompt;
}
}
}
// -----------------------------------------------------------------------------