#include <LiquidCrystal.h>
#include <IRremote.h>
// Initialize the LCD (RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Define pins for the ultrasonic sensor, buzzer, LED, and IR receiver
const int trigPin = 2;
const int echoPin = 3;
const int buzzerPin = 6;
const int ledPin = 5;
const int irPin = 4; // IR receiver pin
// Define variables
long echoDuration;
int distance;
int averageDistance;
const int thresholdDistance = 20; // Distance threshold for alarm
const int MAX_VALID_DISTANCE = 400; // Maximum valid distance in cm
bool alarmEnabled = true; // State of the alarm system
// IR Remote variables
IRrecv irReceiver(irPin);
// Variables for manual tone generation
unsigned long previousBuzzMicros = 0;
const unsigned int buzzIntervalMicros = 500; // Half period in microseconds for 1 kHz tone
bool buzzState = false;
bool buzzerActive = false; // Tracks if the buzzer should be active
// Variables for filtering sensor readings
#define NUM_READINGS 5
int readings[NUM_READINGS]; // Array to hold distance readings
int readIndex = 0; // Index of the current reading
int total = 0; // Total of the readings
void setup() {
// Pin modes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Fire Alarm Ready");
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize IR receiver
IrReceiver.begin(irPin, ENABLE_LED_FEEDBACK); // Start the IR receiver
// Initialize readings array
for (int i = 0; i < NUM_READINGS; i++) {
readings[i] = MAX_VALID_DISTANCE;
total += readings[i];
}
}
void loop() {
// Handle IR remote input
handleIRRemote();
// Trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo pulse
echoDuration = pulseIn(echoPin, HIGH, 30000); // Timeout after 30ms
// Calculate the distance in cm
if (echoDuration == 0 || echoDuration >= 30000) {
// No echo received or timeout
distance = MAX_VALID_DISTANCE;
Serial.println("No echo received or timeout.");
} else {
distance = echoDuration * 0.034 / 2;
Serial.print("Echo Duration: ");
Serial.print(echoDuration);
Serial.print(" us, Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
// Filter the distance readings
total = total - readings[readIndex]; // Subtract the oldest reading
readings[readIndex] = distance; // Store the new reading
total = total + readings[readIndex]; // Add the new reading to total
readIndex = (readIndex + 1) % NUM_READINGS; // Advance index
averageDistance = total / NUM_READINGS;
Serial.print("Average Distance: ");
Serial.println(averageDistance);
unsigned long currentMillis = millis();
unsigned long currentMicros = micros(); // For buzzer timing
// Alarm logic
if (alarmEnabled) {
if (averageDistance > 0 && averageDistance < thresholdDistance) {
digitalWrite(ledPin, HIGH); // Turn ON LED
buzzerActive = true; // Activate the buzzer
// Display alarm message
lcd.setCursor(0, 1);
lcd.print("Smoke Detected! ");
} else {
digitalWrite(ledPin, LOW); // Turn OFF LED
buzzerActive = false; // Deactivate the buzzer
digitalWrite(buzzerPin, LOW); // Ensure buzzer is OFF
buzzState = false; // Reset buzz state
// Clear LCD message if necessary
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the row
}
} else {
// If the alarm system is disabled
digitalWrite(ledPin, LOW); // Ensure LED is OFF
buzzerActive = false; // Deactivate the buzzer
digitalWrite(buzzerPin, LOW); // Ensure buzzer is OFF
buzzState = false; // Reset buzz state
lcd.setCursor(0, 1);
lcd.print("ALARM DISABLED "); // Display disabled message
}
// Manual buzzer control
if (buzzerActive) {
if (currentMicros - previousBuzzMicros >= buzzIntervalMicros) {
previousBuzzMicros = currentMicros;
buzzState = !buzzState;
digitalWrite(buzzerPin, buzzState ? HIGH : LOW);
}
} else {
digitalWrite(buzzerPin, LOW); // Ensure buzzer is OFF when not active
}
// No need for delay here
}
// Function to handle IR remote input
void handleIRRemote() {
// Check if an IR signal is available
if (IrReceiver.decode()) {
Serial.print("IR Code Received: ");
Serial.print(IrReceiver.decodedIRData.command, HEX);
Serial.print(" (HEX), ");
Serial.print(IrReceiver.decodedIRData.command);
Serial.println(" (DEC)");
// Use the actual received code
uint8_t receivedCommand = IrReceiver.decodedIRData.command;
// Handle the command
switch (receivedCommand) {
case 0xA2: // Replace 0xA2 with the actual code from your remote
alarmEnabled = !alarmEnabled; // Toggle alarm state
// When alarm is toggled, reset alarm state
digitalWrite(ledPin, LOW); // Turn OFF LED
buzzerActive = false; // Deactivate the buzzer
digitalWrite(buzzerPin, LOW); // Ensure buzzer is OFF
buzzState = false; // Reset buzz state
if (alarmEnabled) {
lcd.setCursor(0, 0);
lcd.print("Fire Alarm Ready");
lcd.setCursor(0, 1);
lcd.print(" "); // Clear any old messages
Serial.println("Alarm Enabled");
} else {
lcd.setCursor(0, 0);
lcd.print("Alarm is OFF ");
lcd.setCursor(0, 1);
lcd.print("ALARM DISABLED ");
Serial.println("Alarm Disabled");
}
break;
default:
lcd.setCursor(0, 0);
lcd.print("Unknown Command ");
Serial.println("Unknown IR Command Received");
break;
}
IrReceiver.resume(); // Prepare to receive the next signal
}
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎