// Auto clean concept for Bandicoot mini done by Amaljith A.
// Last updated on 24/04/2024.
#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 autocleanstat = 10; // Blue led
int fault = 11; // Red led
int complete = 12; // Green led
int LOADCELL_DOUT_PIN = 13;
int LOADCELL_SCK_PIN = 3;
HX711 LoadCell;
double calibration_factor = 420; //33090; // calibration factor has to be adjusted in accordance with the known weight as reference.
float ReadWeight; // Variable to store the load cell value
// 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;
bool fault_state = 0;
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(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() {
LoadCellRead(); // function to read load cell value
PB_Button(); // to read push button status
if (buttonPressed == true && ReadWeight >= 30 && digitalRead(L_SWITCH) == LOW && processdone == false && fault_state == 0) { // condition where the autocleanstat is turned ON and downward movement initiated
Initialstart = true;
digitalWrite(HOIST_DOWN, LOW);
digitalWrite(autocleanstat, HIGH);
}
if ((Initialstart == true || Cleanflag == 2) && (digitalRead(HOIST_DOWN) == LOW)) { // condition to find error when the hoist down relay is on for a longer time to detect when it didnt touch the ground.
starttimeofHD++;
Serial.println(starttimeofHD);
if (starttimeofHD >= 52) {
// Check for error if HOIST_DOWN relay is ON for >= to 40 count time
Serial.println("Error: HOIST_DOWN relay has been ON for too long");
faultalarm();
}
}
if (Initialstart && ReadWeight <= 16 && fault_state == 0) { // to start cleaning process when bucket touches ground
starttimeofHD = 0;
cleaning();
Initialstart = false;
Overload = true;
Cleanflag = 1;
}
if (Cleanflag == 1 && (ReadWeight >= 30 && ReadWeight < 45) && fault_state == 0) { // to check sufficient waste is collected
Serial.println("Sufficient waste collected...");
Cleanflag = 0;
digitalWrite(HOIST_UP, LOW);
digitalWrite(complete, HIGH);
processdone = true;
}
if (Cleanflag == 1 && ReadWeight < 30 && fault_state == 0) { // for repeat cleaning condition when less waste is collected
Serial.println("Less wastage collected...");
Cleanflag = 0;
digitalWrite(BUCKET_OPEN, LOW);
delay(2000);
digitalWrite(BUCKET_OPEN, HIGH);
delay(1000);
digitalWrite(BUCKET_CLOSE, LOW);
delay(2000);
digitalWrite(BUCKET_CLOSE, HIGH);
delay(1000);
digitalWrite(HOIST_DOWN, LOW);
Cleanflag = 2;
}
if (Cleanflag == 2 && ReadWeight <= 16 && fault_state == 0) { // to perform cleaning again (repeat) when bucket touches ground
Serial.println("Repeating cleaning process again as less waste collected...");
starttimeofHD = 0;
cleaning();
Overload = true;
Cleanflag = 3;
}
if (Cleanflag == 3 && ReadWeight < 30 && fault_state == 0) { // check waste after repeated clean
Serial.println("Nothing more to collect");
digitalWrite(HOIST_UP, LOW);
digitalWrite(complete, HIGH);
processdone = true;
}
if (Cleanflag == 3 && (ReadWeight >= 30 && ReadWeight < 45) && fault_state == 0) { // to check sufficient waste is collected
Serial.println("Sufficient waste collected...");
digitalWrite(HOIST_UP, LOW);
digitalWrite(complete, HIGH);
processdone = true;
}
if (Overload == true && ReadWeight >= 45) { // to raise fault alarm when overload is detected
Serial.println("Overload condition detected...");
faultalarm();
}
if (processdone == true && ReadWeight < 45 && (digitalRead(L_SWITCH) == HIGH) && fault_state == 0) { // condition to complete cleaning process
Serial.println("Cleaning process completed");
digitalWrite(autocleanstat, LOW);
digitalWrite(complete, HIGH);
} else if (digitalRead(L_SWITCH) == LOW && processdone == true) {
Initial_setup();
}
if (ReadWeight <= -50 || ReadWeight > 50) {
Serial.println(" System failure !!!!");
faultalarm();
}
}
// To set fault alarm for errors
void faultalarm() {
fault_state = 1;
digitalWrite(HOIST_UP, HIGH);
digitalWrite(fault, HIGH);
digitalWrite(complete, 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);
Initialstart = false;
Cleanflag = 0;
processdone = false;
Overload = false;
starttimeofHD = 0;
}
// function for auto cleaning
void cleaning() {
Serial.println("Cleaning Operation Started...");
digitalWrite(HOIST_DOWN, HIGH);
delay(1000);
digitalWrite(HOIST_UP, LOW);
delay(3000);
digitalWrite(HOIST_UP, HIGH);
delay(1000);
digitalWrite(BUCKET_OPEN, LOW);
delay(1500);
digitalWrite(BUCKET_OPEN, HIGH);
delay(1000);
digitalWrite(HOIST_DOWN, LOW);
delay(3700);
digitalWrite(HOIST_DOWN, HIGH);
delay(1000);
digitalWrite(BUCKET_CLOSE, LOW);
delay(2000);
digitalWrite(HOIST_UP, LOW);
delay(600);
digitalWrite(HOIST_UP, HIGH);
delay(500);
digitalWrite(BUCKET_CLOSE, LOW);
delay(2000);
digitalWrite(HOIST_UP, LOW);
delay(2400);
digitalWrite(HOIST_UP, HIGH);
digitalWrite(BUCKET_CLOSE, HIGH);
delay(1000);
Serial.println("Cleaning Operation End...");
LoadCellRead();
}
//function to read the load cell and get the read data
float LoadCellRead() {
if (LoadCell.wait_ready_timeout(1000)) {
ReadWeight = (LoadCell.get_units(5));
//ReadWeight = ReadWeight + 32;
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;
}
}