Does this look better??
/*
THIS PROGRAM WAS MADE BY: AQUA-PLAST COLLECTORS - STUDYING THE EFFICIENCY OF PLASTIC COLLECTOR AS A WATER DISPENSER
-----------------------------------------------------------------------------
Programer:
Head Leader:
Co-Leader:
Prototype Maker:
Members:
-----------------------------------------------------------------------------
*/
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //Renaming the LCD
Servo ServoRecycler; //Renaming the Servo
Servo ServoFiller; //Renaming the Servo
//SesnorPins
const int trigPin = 2; //Ultrasonic
const int echoPin = 3; //Ultrasonic
const int SensorRecycler = 4; //(inductive/capacitative)
//ButtonPins
const int PinBut750ml = 11; //water amount 750ml
const int PinBut500ml = 12; //water amount 500ml
const int PinBut250ml = 13; //water amount 250ml
bool butPressed = false;
//ServoPins
const int servoRecPin = 6;
const int servoFillPin = 7;
const int ServoOpen = 180;
const int ServoClosed = 0;
//Sensor Status
float bottleDist; //Ultrasonic
const float distCm = 5.0; //Ultrasonic
const int sensorActive = LOW;//(Inductive/Capacitative)
//Delay Time
const int dt = 200;
const int dtInac = 1500;
const int dtFilled = 5000;
const int dtRecycler1 = 1500;
const int dtRecycler2 = 3500;
//Inactivity Timeout
const unsigned long TimeOut20secEnabled = 20000;
const unsigned long TimeOut30secFill = 30000;
//Water amount
const unsigned long msecFill750ml = 67500;
const unsigned long msecFill500ml = 45800;
const unsigned long msecFill250ml = 22000;
unsigned long msec1 = 0;
unsigned long msecFill;
const char *fillStr;
//Switch Statement Cases
enum { Prompt, Idle, Enabled, BotReady, Inactivity, Fill, Wait };
int state;
// -----------------------------------------------------------------------------
void setup()
{
//Serial Monitor Startup
Serial.begin (9600);
//Liquid Crystal Display I2C Startup
lcd.begin(16,2);
lcd.backlight();
//Servo Startup
ServoRecycler.attach(servoRecPin);
ServoFiller.attach(servoFillPin);
ServoRecycler.write(ServoClosed);//Calibration check
ServoFiller.write(ServoClosed);//Calibration check
//SensorPin
pinMode(trigPin,OUTPUT); //Ultrasonic
pinMode(echoPin,INPUT); //Ultrasonic
pinMode(SensorRecycler,INPUT); //inductive/capacitative
//ButtonPin
pinMode (PinBut250ml, INPUT_PULLUP);
pinMode (PinBut500ml, INPUT_PULLUP);
pinMode (PinBut750ml, INPUT_PULLUP);
state = Prompt;
}
// -----------------------------------------------------------------------------
void lcdDisp (const char *s0, const char *s1, int col1 = 0, int col2 = 0){ //LCD Display function
lcd.clear();
if (s0) {
lcd.setCursor (col1, 0);
lcd.print (s0);
}
if (s1) {
lcd.setCursor (col2, 1);
lcd.print (s1);
}
}
// -----------------------------------------------------------------------------
void loop()
{
unsigned long msec = millis ();
digitalWrite(trigPin, LOW);
delay(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
bottleDist = pulseIn(echoPin, HIGH);
bottleDist = (bottleDist/58.82);
// -----------------------------------------------------------------------------
switch (state) {
case Prompt: //waiting for the recycled bottle to be inserted
Serial.println (" state Idle");
Serial.println (" insert recycled bottle");
lcdDisp("Please Insert A", "Water Bottle");
state = Idle;
break;
// -----------------------------------------------------------------------------
case Idle: //recycled bottle inserted ready to dispence water
if (digitalRead (SensorRecycler) == sensorActive) {
delay(dtRecycler1);
ServoRecycler.write(ServoOpen);
delay(dtRecycler2);
ServoRecycler.write(ServoClosed);
Serial.println (" state Enabled");
Serial.println (" place bottle");
lcdDisp("Place Bottle At","Dispensing Area");
delay(dt);
msec1 = msec;
state = Enabled;
}
break;
// -----------------------------------------------------------------------------
case Enabled: //bottle was placed ready to select water amount
if (bottleDist<=distCm) {
state = BotReady;
Serial.println (" state BotReady");
Serial.println (" press button");
lcdDisp("Please Select","Water Amount");
delay(dt);
}
if (msec - msec1 >=TimeOut20secEnabled){ //No actions was made
msec1 = msec;
Serial.println (" state Prompt - Inactivity");
state = Prompt;
}
break;
// -----------------------------------------------------------------------------
case BotReady: //water amount options
if (bottleDist>=distCm) {
state = Enabled; // go back
msec1 = msec;
Serial.println (" state Enabled - bottle removed");
lcdDisp("Bottle Removed","Please Replace");
delay(dt);
}
//Water Dispensing 750ML
if (digitalRead (PinBut750ml) == LOW) {
msecFill = msecFill750ml;
fillStr = "750ml";
butPressed = true;
}
//Water Dispensing 500ML
else if (digitalRead (PinBut500ml) == LOW) {
msecFill = msecFill500ml;
fillStr = "500ml";
butPressed = true;
}
//Water Dispensing 250ML
else if (digitalRead (PinBut250ml) == LOW) {
msecFill = msecFill250ml;
fillStr = "250ml";
butPressed = true;
}
if (butPressed){ //Dispensing the water
state = Fill;
msec1 = msec;
lcdDisp("Dispensing...", fillStr, 0, 6);
ServoFiller.write(ServoOpen);
Serial.print("state Fill - button pressed ");
Serial.println(fillStr);
butPressed = false;
}
break;
// -----------------------------------------------------------------------------
case Inactivity: //timeout if no actions was made
if (bottleDist<=distCm){ //bottle detected - continue
Serial.println (" state BotReady");
Serial.println (" press button");
lcdDisp("Please Reselect","Water Amount");
msec1 = msec;
state = BotReady;
}
else if (msec - msec1 >=TimeOut30secFill){ //no bottle detected - restart
msec1 = msec;
Serial.println (" state Prompt - Inactivity");
lcdDisp("Bottle Removed","Restart Required");
delay(dtInac);
state = Prompt;
}
break;
// -----------------------------------------------------------------------------
case Fill: //dispencing the water selected by the person
if (bottleDist>=distCm) { //bottle removed before filled
state = Inactivity;
ServoFiller.write(ServoClosed);
Serial.println (" state Inactivity - bottle removed before filled");
lcdDisp(" Dispensing"," Paused");
// delay(dtInac);
msec1 = msec;
}
else if (msec - msec1 >= msecFill) {
state = Wait;
msec1 = msec;
ServoFiller.write (ServoClosed);
Serial.print (" state Wait - bottle filled ");
Serial.println(fillStr);
Serial.println (" thank you");
lcdDisp(" Water Filled","Remove Bottle");
delay (dtFilled);
}
break;
// -----------------------------------------------------------------------------
case Wait: //waiting to remove the bottle from dispencing area
if (bottleDist>=distCm) {
state = Prompt;
Serial.println (" state Prompt - bottle removed");
}
break;
}
}
// -----------------------------------------------------------------------------