/*
Toddler Environment Monitoring System
*/
/* This system monitors the environment to ensure toddler safety by checking:
>> Motion detection to identify inactivity
>> Sound levels to detect loud noises
>> Temperature and humidity to ensure comfort
>> Alerts are raised using an alarm when any parameter goes out of the defined safe range.
*/
#include <DHT.h>
// Constants
#define DHTPIN 6 // DHT pin
#define DHTTYPE DHT22 // Type of DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
// Pins
#define SOUND_PIN A0 // Sound Sensor (Potentiometer)
#define MOTION_PIN 8 // PIR Motion Sensor
#define ALARM_PIN 9 // Buzzer and LED
// Thresholds
const int soundThreshold = 700; // Sound threshold (analog value)
const float tempLowThreshold = 19.0; // Minimum temperature
const float tempHighThreshold = 27.0; // Maximum temperature
const float humLowThreshold = 30.0; // Minimum humidity
const float humHighThreshold = 60.0; // Maximum humidity
const int motionThresholdSeconds = 15; // Threshold for motion detection in seconds
const int n = 3; // Delay in seconds for serial output
// Variables
float hum; // Store Humidity value
float temp; // Store Temperature value
int soundLevel; // Sound Level Value (from potentiometer)
int movement; // Motion Detection Value
unsigned long lastMotionTime = 0; // Track the last time motion was detected
const unsigned long motionThreshold = motionThresholdSeconds * 1000; // Convert to milliseconds
void setup() {
pinMode(SOUND_PIN, INPUT);
pinMode(MOTION_PIN, INPUT);
pinMode(ALARM_PIN, OUTPUT);
Serial.begin(9600);
dht.begin();
}
void loop() {
// Read data from sensors
hum = dht.readHumidity();
temp = dht.readTemperature();
soundLevel = analogRead(SOUND_PIN);
movement = digitalRead(MOTION_PIN);
Serial.println("-----------------------");
bool alarmActive = false; // Keep track of whether the alarm should be active
// Display sound details and check alert
if (soundLevel >= soundThreshold) {
Serial.println("ALERT: High sound level detected!");
alarmActive = true;
}
Serial.print("Sound Level: ");
Serial.println(soundLevel);
// Handle motion detection
if (movement == HIGH) {
lastMotionTime = millis(); // Reset the motion timer
Serial.println("Motion detected! Timer reset.");
} else {
unsigned long timeWithoutMotion = millis() - lastMotionTime;
Serial.print("Time without motion: ");
Serial.print(timeWithoutMotion / 1000); // Convert to seconds
Serial.println(" seconds");
if (timeWithoutMotion >= motionThreshold) {
Serial.print("ALERT: No motion detected for over ");
Serial.print(motionThresholdSeconds);
Serial.println(" seconds!");
alarmActive = true;
}
}
// Check temperature and humidity alerts
if (temp < tempLowThreshold || temp > tempHighThreshold) {
Serial.println("ALERT: Temperature out of range!");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
alarmActive = true;
} else {
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" °C");
}
if (hum < humLowThreshold || hum > humHighThreshold) {
Serial.println("ALERT: Humidity out of range!");
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println(" %");
alarmActive = true;
} else {
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println(" %");
}
// Activate or deactivate the alarm
if (alarmActive) {
digitalWrite(ALARM_PIN, HIGH); // Turn on alarm
} else {
digitalWrite(ALARM_PIN, LOW); // Turn off alarm
}
delay(n * 1000); // Delay for n seconds
}