Files
Last update 1 week 5 days
by Niranjani_2005
FilesECON | |
---|---|
.. | |
diagram.json | |
libraries.txt | |
sketch.ino | |
wokwi-project.txt |
sketch.ino#include <EEPROM.h> #define TRIG_PIN 4 #define ECHO_PIN 5 #define LED_PIN 18 #define LDR_PIN 34 #define VIB_PIN 35 #define EEPROM_SIZE 128 int eepromAddr = 0; int objectDetectedCount = 0; int noDetectionCount = 0; int cycleCount = 0; int lastVibValue = 0; // ๐ New variables to prevent repeated captures float lastCapturedDistance = 0; unsigned long lastCaptureTime = 0; const int distanceChangeThreshold = 3; // cm const int captureCooldown = 5000; // ms void setup() { Serial.begin(115200); pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); pinMode(LED_PIN, OUTPUT); EEPROM.begin(EEPROM_SIZE); Serial.println("System Initialized โ "); } void loop() { if (Serial.available()) { String cmd = Serial.readStringUntil('\n'); cmd.trim(); if (cmd == "READ") { readEEPROMLogs(); return; } if (cmd == "ERASE") { for (int i = 0; i < EEPROM_SIZE; i++) { EEPROM.write(i, 0xFF); // Clear EEPROM } EEPROM.commit(); Serial.println("๐งน EEPROM Erased Successfully!"); return; } } cycleCount++; // --- Read distance from Ultrasonic --- long duration; float distance; digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); duration = pulseIn(ECHO_PIN, HIGH, 30000); distance = duration * 0.034 / 2; Serial.print("๐ Distance: "); Serial.print(distance); Serial.println(" cm"); // --- Read light level from LDR --- int lightValue = analogRead(LDR_PIN); Serial.print("๐ก Light Level: "); Serial.println(lightValue); // --- Read vibration from Potentiometer --- int vibValue = analogRead(VIB_PIN); Serial.print("๐ฅ Vibration (pot): "); Serial.println(vibValue); // --- Detect vibration based on sudden change --- bool vibrationDetected = abs(vibValue - lastVibValue) > 100; lastVibValue = vibValue; // --- Sensor fusion logic --- bool objectDetected = (distance > 1 && distance < 20.0); bool enoughLight = lightValue > 1000; bool distanceChanged = abs(distance - lastCapturedDistance) >= distanceChangeThreshold; bool cooldownPassed = (millis() - lastCaptureTime) >= captureCooldown; if (objectDetected && enoughLight && !vibrationDetected && distanceChanged && cooldownPassed) { digitalWrite(LED_PIN, HIGH); Serial.println("๐ธ New Valid Object! Capturing..."); logToEEPROM(1, (int)distance, cycleCount); delay(200); digitalWrite(LED_PIN, LOW); objectDetectedCount++; noDetectionCount = 0; lastCapturedDistance = distance; lastCaptureTime = millis(); } else { Serial.println("โ Skipped: Conditions not met or repeated object."); noDetectionCount++; } if (noDetectionCount >= 10) { Serial.println("โ ๏ธ Fault: No detection for 10 cycles"); logToEEPROM(2, 0, cycleCount); noDetectionCount = 0; } Serial.println("---------------"); delay(1000); // This comes LAST } // --- EEPROM Logging Function --- void logToEEPROM(uint8_t eventType, uint8_t distance, uint8_t timestamp) { if (eepromAddr + 3 >= EEPROM_SIZE) { Serial.println("๐ EEPROM Full. Overwriting from 0."); eepromAddr = 0; } EEPROM.write(eepromAddr++, eventType); EEPROM.write(eepromAddr++, distance); EEPROM.write(eepromAddr++, timestamp); EEPROM.commit(); Serial.print("๐ฆ Logged: Event="); Serial.print(eventType); Serial.print(" Distance="); Serial.print(distance); Serial.print(" Timestamp="); Serial.println(timestamp); } void readEEPROMLogs() { Serial.println("๐ Reading EEPROM Logs:"); int addr = 0; while (addr + 3 <= EEPROM_SIZE) { uint8_t eventType = EEPROM.read(addr++); uint8_t distance = EEPROM.read(addr++); uint8_t timestamp = EEPROM.read(addr++); if (eventType == 0xFF && distance == 0xFF && timestamp == 0xFF) { break; // End of meaningful data } Serial.print("๐ Event="); Serial.print(eventType); if (eventType == 1) { Serial.print(" | ๐ Distance="); Serial.print(distance); Serial.print(" | โฑ๏ธ Time="); Serial.println(timestamp); } else if (eventType == 2) { Serial.print(" | โ ๏ธ Fault at Time="); Serial.println(timestamp); } else { Serial.println(" | Unknown Event"); } } }