#define LDR_PIN A0
#define LED_PIN 13
#define RELAY_PIN 9
#define BUZZER_PIN 8
const int LARGE_BAG_THRESHOLD = 300; // Lux value below this is considered a large bag
const int SMALL_BAG_THRESHOLD = 600; // Lux value below this and above large threshold is considered a small bag
const unsigned long RELAY_RUN_TIME = 2000; // Time to run relay for removal (2 seconds)
const unsigned long SAFETY_TIMEOUT = 5000; // 5 seconds timeout for safety
unsigned long lastDetectionTime = 0;
int largeBagCount = 0;
int smallBagCount = 0;
unsigned long startTime = 0;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
startTime = millis();
}
void loop() {
int sensorValue = analogRead(LDR_PIN); // Read value from LDR
int lux = map(sensorValue, 0, 1023, 0, 1000); // Convert sensor value to lux
Serial.print("Lux: ");
Serial.println(lux);
if (lux < LARGE_BAG_THRESHOLD) { // If large bag detected
largeBagCount++;
Serial.println("Large bag detected");
} else if (lux < SMALL_BAG_THRESHOLD) { // If small bag detected
smallBagCount++;
Serial.println("Small bag detected");
} else { // Non-defective product detected
Serial.println("Defective product detected");
handleProductDetection();
}
if (millis() - startTime > 10000) { // 10 seconds window for large bags
largeBagCount = 0;
startTime = millis();
}
if (millis() - startTime > 6000) { // 6 seconds window for small bags
smallBagCount = 0;
startTime = millis();
}
if (largeBagCount > 2 || smallBagCount > 2) {
digitalWrite(BUZZER_PIN, HIGH); // Sound buzzer to indicate failure
Serial.println("System failure! Halting assembly line.");
// Additional code to halt the assembly line can be added here
} else {
digitalWrite(BUZZER_PIN, LOW); // Ensure buzzer is off if system is operating normally
}
delay(1000); // Small delay to debounce sensor readings
}
void handleProductDetection() {
digitalWrite(LED_PIN, HIGH); // Turn on LED to indicate detection
digitalWrite(RELAY_PIN, HIGH); // Turn on relay to remove product
delay(RELAY_RUN_TIME); // Wait for product to be removed
digitalWrite(RELAY_PIN, LOW); // Turn off relay
digitalWrite(LED_PIN, LOW); // Turn off LED
lastDetectionTime = millis();
}