// sketch.ino
// Isolated test loop for the Bin Level Sensors & Alert Logic
#include "user_interface.h"
#include "bin_sensors.h"
// Variables to control blinking and printing using millis()
unsigned long previousBlinkTime = 0;
unsigned long previousPrintTime = 0; // Timer for the Serial Monitor
// const long blinkInterval = 500; // LED blinks every half second (500ms) <-- COMMENTED OUT TO AVOID REDEFINITION
bool ledState = LOW;
void setup() {
Serial.begin(115200);
setupUI(); // Initialize buttons and LEDs from user_interface.h
setupSensors(); // Initialize ultrasonic pins from bin_sensors.h
Serial.println("--- Bin Sensors Test Ready ---");
Serial.println("Move the sliders on the Ultrasonic Sensors to test distances.");
}
void loop() {
// 1. Read sensors and update logic (evaluates the 3 continuous seconds)
updateBinStatus();
unsigned long currentMillis = millis();
// Print distances every 1 second (1000 ms)
if (currentMillis - previousPrintTime >= 1000) {
previousPrintTime = currentMillis;
// Read distances using the function from bin_sensors.h
long dF = getDistance(trigFerro, echoFerro);
long dNF = getDistance(trigNonFerro, echoNonFerro);
Serial.print("Bin 1 (Magnetic) | Dist: ");
if (dF == 999) Serial.print("ERROR/NO ECHO"); else { Serial.print(dF); Serial.print(" cm"); }
Serial.print(" | Status: ");
Serial.println(isFerroFull ? "FULL (LED BLINKING)" : "OK");
Serial.print("Bin 2 (Non-Magnetic) | Dist: ");
if (dNF == 999) Serial.print("ERROR/NO ECHO"); else { Serial.print(dNF); Serial.print(" cm"); }
Serial.print(" | Status: ");
Serial.println(isNonFerroFull ? "FULL (LED BLINKING)" : "OK");
Serial.println("--------------------------------------------------");
}
// 2. Handle LED blinking if bins are full
if (isFerroFull || isNonFerroFull) {
// Toggle LED state every half second
if (currentMillis - previousBlinkTime >= blinkInterval) {
previousBlinkTime = currentMillis;
ledState = !ledState;
// Blink Bin 1 LED only if full
if (isFerroFull) digitalWrite(ferroLedPin, ledState);
else turnOffFerroLed();
// Blink Bin 2 LED only if full
if (isNonFerroFull) digitalWrite(nonFerroLedPin, ledState);
else turnOffNonFerroLed();
}
} else {
// Ensure both LEDs are off if neither bin is full
turnOffFerroLed();
turnOffNonFerroLed();
}
// Small delay to keep Wokwi simulation running smoothly
delay(50);
}Start Sorting
Calibrate
Limit Switch
Electromagnet
Bin reset
Bin_1
Bin_2
Tray_Servo
Linear_Actuator
Rotation_Servo