Hi, I have a project about fall detection in hospital toilets using esp32, radar sensors, motion sensors and LDR sensors. for now, my coding doesn't seem to give the correct reading but I don't know if the code I made is correct or not. can anyone help to make sure my code is correct? My project is done by placing the system on the ceiling and radiating on a closed area of ββ1.7m x 1.2m x 1.4m
#include <SPI.h>
#include <LoRa.h>
#include <ld2410.h>
#define BAND 915E6
#define NSS_PIN 15 // Chip Select Pin
#define RST_PIN 4 // Reset Pin
#define DI0_PIN 34 // Interrupt Pin for DIO0
#define MONITOR_SERIAL Serial
#define RADAR_SERIAL Serial1
#define RADAR_RX_PIN 16
#define RADAR_TX_PIN 17
ld2410 radar;
uint32_t lastReading = 0;
uint32_t lastAlertTime = 0; // Timer for 10-second alerts
float currentDistance = 0.0;
// LED pins
const int greenLEDPin = 12; // Green LED for motion detection
const int yellowLEDPin = 14; // Yellow LED for LoRa transmission
const int redLEDPin = 13; // Red LED for fall/emergency
// Constants for fall detection
const float fallDistanceThreshold = 0.8; // Distance in meters indicating a fall
const unsigned long fallAlertCooldown = 5000; // 5 seconds cooldown for fall alerts
const unsigned long alertInterval = 10000; // 10 seconds alert interval
unsigned long lastFallAlertTime = 0;
void setup() {
Serial.begin(9600);
pinMode(greenLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
delay(100);
// Initialize LoRa
SPI.begin();
LoRa.setPins(NSS_PIN, RST_PIN, DI0_PIN);
if (!LoRa.begin(BAND)) {
Serial.println("LoRa initialization failed. Check your connections.");
} else {
Serial.println("LoRa transmitter initialized.");
}
// Initialize Radar
MONITOR_SERIAL.begin(115200);
RADAR_SERIAL.begin(256000, SERIAL_8N1, RADAR_RX_PIN, RADAR_TX_PIN);
if (radar.begin(RADAR_SERIAL)) {
MONITOR_SERIAL.println(F("Radar Initialized"));
} else {
sendMessage("B1 : Faulty");
}
}
void loop() {
radar.read();
// Ensure the radar is connected and we read at intervals
if (radar.isConnected() && millis() - lastReading > 500) {
lastReading = millis();
String alertMessage;
if (radar.presenceDetected()) {
digitalWrite(greenLEDPin, HIGH); // Indicate presence with green LED
if (radar.movingTargetDetected()) {
currentDistance = radar.movingTargetDistance() / 100.0; // Convert to meters
Serial.print("Target moving. Distance: ");
Serial.print(currentDistance);
Serial.println(" m");
alertMessage = "B1 : Target moving";
} else {
currentDistance = radar.stationaryTargetDistance() / 100.0; // Convert to meters
Serial.print("Target detected. Stationary. Distance: ");
Serial.print(currentDistance);
Serial.println(" m");
alertMessage = "B1 : Target stationary";
}
// Check for fall detection
if (currentDistance > fallDistanceThreshold && millis() - lastFallAlertTime > fallAlertCooldown) {
Serial.println("Fall detected!");
alertMessage = "B1 : Fall detected! Emergency!";
digitalWrite(redLEDPin, HIGH); // Indicate fall with red LED
delay(500);
digitalWrite(redLEDPin, LOW);
lastFallAlertTime = millis(); // Update fall alert cooldown
}
} else {
// No target detected
digitalWrite(greenLEDPin, LOW); // Turn off green LED
Serial.println("No target detected.");
alertMessage = "B1 : Vacant";
}
// Send alert every 10 seconds
if (millis() - lastAlertTime > alertInterval) {
sendMessage(alertMessage);
lastAlertTime = millis(); // Update last alert time
}
}
delay(1000); // Main loop delay
}
void sendMessage(String message) {
Serial.println("Sending LoRa message: " + message);
digitalWrite(yellowLEDPin, HIGH); // Turn on yellow LED for transmission
if (LoRa.beginPacket()) {
LoRa.print(message);
if (LoRa.endPacket()) {
Serial.println("LoRa packet sent successfully.");
} else {
Serial.println("LoRa packet sending failed!");
}
} else {
Serial.println("Failed to start LoRa packet!");
}
delay(500); // Delay for yellow LED visibility
digitalWrite(yellowLEDPin, LOW); // Turn off yellow LED after transmission
}
