#include "HX711.h"
// Assign pins for relays, LEDs, buzzer, limit switch, push button, and potentiometer
int START_BUTTON = 8;
int L_SWITCH = 2;
int HOIST_DOWN = 4;
int HOIST_UP = 5;
int BUCKET_OPEN = 6;
int BUCKET_CLOSE = 7;
int BUZZER = 9;
int autocleanstat = 10; //Blue led
int fault = 11; // Red led
int complete = 12; // Green led
HX711 LoadCell;
int LOADCELL_DOUT_PIN = 13;
int LOADCELL_SCK_PIN = 3;
double calibration_factor = 210;
float ReadWeight;
float CurrentWeight;
float Bucketweight = 32.0; // initial bucket weight
float Wasteweight = 5; // sample weight taken as an assumption for comparisons
float GroundWeight = 0.0; // initialisation
float FaultWeight = 70.0;
// assigning different flags for tracking different conditions of relay's and push button status
bool buttonPressed = false;
bool Initialstart = false;
int Cleanflag = 0;
bool processdone = false;
bool Overload = false;
int starttimeofHD = 0; // Variable to store the start time of HOIST_DOWN relay
void setup() {
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(L_SWITCH, INPUT_PULLUP);
pinMode(HOIST_DOWN, OUTPUT);
pinMode(HOIST_UP, OUTPUT);
pinMode(BUCKET_OPEN, OUTPUT);
pinMode(BUCKET_CLOSE, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(autocleanstat, OUTPUT);
pinMode(fault, OUTPUT);
pinMode(complete, OUTPUT);
Initial_setup(); // Set initial conditions
LoadCell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
LoadCell.set_scale(calibration_factor);
LoadCell.tare();
Serial.begin(115200); // begin serial communication
}
void loop() {
PB_Button(); // to read push button status
int Limit_switch = digitalRead(L_SWITCH);
CurrentWeight = LoadCellRead();
CurrentWeight = CurrentWeight + Bucketweight;
GroundWeight = (Bucketweight * 75) / 100;
if (Limit_switch == LOW) {
Initial_setup();
}
if ((buttonPressed && Limit_switch == HIGH) && (CurrentWeight >= Bucketweight)) { //condition 2: where the autocleanstat is turned ON and downward movement initiated
Initialstart = true;
digitalWrite(HOIST_DOWN, LOW);
digitalWrite(autocleanstat, HIGH);
}
if (Initialstart == true && (digitalRead(HOIST_DOWN) == LOW)) {
starttimeofHD++;
Serial.println(starttimeofHD);
if (starttimeofHD >= 40) {
// Check for error if HOIST_DOWN relay is ON for >= to 40 seconds
Serial.println("Error: HOIST_DOWN relay has been ON for too long");
faultalarm();
Initial_setup();
}
}
if (Initialstart && CurrentWeight <= GroundWeight) { // to start cleaning process
cleaning();
LoadCellRead();
CurrentWeight = CurrentWeight + Bucketweight;
Initialstart = false;
Overload = true;
Cleanflag = 1;
}
if (Cleanflag == 1 && (CurrentWeight >= Bucketweight + Wasteweight)) { // to check sufficient waste is collected
Serial.println("Sufficient waste collected...");
digitalWrite(HOIST_UP, LOW);
digitalWrite(complete, HIGH);
processdone = true;
}
if (Cleanflag == 1 && (CurrentWeight < Bucketweight + Wasteweight )) { // for repeat cleaning condition when less waste is collected
Serial.println("Less wastage collected...");
Cleanflag = 2;
digitalWrite(BUCKET_OPEN, LOW);
delay(2000);
digitalWrite(BUCKET_OPEN, HIGH);
digitalWrite(BUCKET_CLOSE, LOW);
delay(2000);
digitalWrite(BUCKET_CLOSE, HIGH);
digitalWrite(HOIST_DOWN, LOW);
LoadCellRead();
}
if (Cleanflag == 2 && CurrentWeight <= GroundWeight) { // to perform cleaning again (repeat)
Serial.println("Repeating cleaning process again as less waste collected...");
cleaning();
Overload = true;
Cleanflag = 3;
LoadCellRead();
CurrentWeight = CurrentWeight + Bucketweight;
}
if (Cleanflag == 3 && (CurrentWeight < Bucketweight + Wasteweight )) { // check waste after repeated clean
Serial.println("Nothing more to collect");
digitalWrite(HOIST_UP, LOW);
digitalWrite(complete, HIGH);
processdone = true;
}
else if (Cleanflag == 3 && (CurrentWeight >= Bucketweight + Wasteweight )) {
Serial.println("Sufficient waste collected...");
digitalWrite(HOIST_UP, LOW);
digitalWrite(complete, HIGH);
processdone = true;
}
if (Overload && CurrentWeight >= FaultWeight) { // to raise fault alarm when overload is detected
Overload_fn();
}
if (processdone && CurrentWeight < FaultWeight && Limit_switch == HIGH) { // condition to complete cleaning process
Serial.println("Cleaning process completed");
digitalWrite(autocleanstat, LOW);
digitalWrite(complete, HIGH);
}
}
// To set fault alarm for errors
void faultalarm() {
digitalWrite(HOIST_UP, HIGH);
digitalWrite(fault, HIGH);
digitalWrite(BUZZER, HIGH);
delay(5000);
digitalWrite(BUZZER, LOW);
}
// initial condition
void Initial_setup() {
digitalWrite(HOIST_DOWN, HIGH);
digitalWrite(HOIST_UP, HIGH);
digitalWrite(BUCKET_OPEN, HIGH);
digitalWrite(BUCKET_CLOSE, HIGH);
digitalWrite(autocleanstat, LOW);
digitalWrite(fault, LOW);
digitalWrite(complete, LOW);
digitalWrite(BUZZER, LOW);
Initialstart = false;
Cleanflag = 0;
processdone = false;
Overload = false;
starttimeofHD = 0;
}
// function for cleaning
void cleaning() {
Serial.println("Cleaning Operation Started...");
digitalWrite(HOIST_DOWN, HIGH);
delay(2000);
digitalWrite(HOIST_UP, LOW);
delay(3000);
digitalWrite(HOIST_UP, HIGH);
delay(2000);
digitalWrite(BUCKET_OPEN, LOW);
delay(2000);
digitalWrite(BUCKET_OPEN, HIGH);
delay(2000);
digitalWrite(HOIST_DOWN, LOW);
delay(4200);
digitalWrite(HOIST_DOWN, HIGH);
delay(2000);
digitalWrite(BUCKET_CLOSE, LOW);
delay(2000);
digitalWrite(BUCKET_CLOSE, HIGH);
delay(2000);
digitalWrite(HOIST_UP, LOW);
delay(3000);
digitalWrite(HOIST_UP, HIGH);
delay(2000);
Serial.println("Cleaning Operation End...");
}
void Overload_fn() { //function for overload condition
if((digitalRead(L_SWITCH) == HIGH) && Overload == true) {
Serial.println("Overload waste detected");
digitalWrite(HOIST_UP, HIGH);
faultalarm();
delay(8000);
digitalWrite(BUCKET_OPEN, LOW);
delay(2000);
digitalWrite(BUCKET_OPEN, HIGH);
delay(500);
digitalWrite(BUCKET_CLOSE, LOW);
delay(2000);
digitalWrite(BUCKET_CLOSE, HIGH);
delay(500);
digitalWrite(HOIST_UP, LOW);
Overload = false;
}
else if (digitalRead(L_SWITCH) == LOW) {
Initial_setup();
}
}
float LoadCellRead() {
if (LoadCell.wait_ready_timeout(1000)) {
ReadWeight = (LoadCell.get_units(5));
Serial.print("HX711 reading: ");
Serial.println(ReadWeight);
} else {
Serial.println("HX711 not found.");
ReadWeight = 0.0;
}
return ReadWeight;
}
// Function for the push button
void PB_Button() {
// Check if the button is pressed and the flip-flop state is off
if (digitalRead(START_BUTTON) == LOW && !buttonPressed) {
// Set buttonPressed flag to true
buttonPressed = true;
} else if (digitalRead(START_BUTTON) == HIGH && buttonPressed) {
// Reset buttonPressed flag when button is released
buttonPressed = false;
}
}