Advertisement
BlackSunFL

Alarm System 2 remote

Dec 3rd, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. #include <IRremote.h>
  3.  
  4. // Initialize the LCD (RS, EN, D4, D5, D6, D7)
  5. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  6.  
  7. // Define pins for the ultrasonic sensor, buzzer, LED, and IR receiver
  8. const int trigPin = 2;
  9. const int echoPin = 3;
  10. const int buzzerPin = 6;
  11. const int ledPin = 5;
  12. const int irPin = 4; // IR receiver pin
  13.  
  14. // Define variables
  15. long echoDuration;
  16. int distance;
  17. int averageDistance;
  18. const int thresholdDistance = 20; // Distance threshold for alarm
  19. const int MAX_VALID_DISTANCE = 400; // Maximum valid distance in cm
  20. bool alarmEnabled = true;  // State of the alarm system
  21.  
  22. // IR Remote variables
  23. IRrecv irReceiver(irPin);
  24.  
  25. // Variables for manual tone generation
  26. unsigned long previousBuzzMicros = 0;
  27. const unsigned int buzzIntervalMicros = 500; // Half period in microseconds for 1 kHz tone
  28. bool buzzState = false;
  29. bool buzzerActive = false; // Tracks if the buzzer should be active
  30.  
  31. // Variables for filtering sensor readings
  32. #define NUM_READINGS 5
  33. int readings[NUM_READINGS];      // Array to hold distance readings
  34. int readIndex = 0;               // Index of the current reading
  35. int total = 0;                   // Total of the readings
  36.  
  37. void setup() {
  38.   // Pin modes
  39.   pinMode(trigPin, OUTPUT);
  40.   pinMode(echoPin, INPUT);
  41.   pinMode(buzzerPin, OUTPUT);
  42.   pinMode(ledPin, OUTPUT);
  43.  
  44.   // Initialize LCD
  45.   lcd.begin(16, 2);
  46.   lcd.setCursor(0, 0);
  47.   lcd.print("Fire Alarm Ready");
  48.  
  49.   // Initialize Serial Monitor
  50.   Serial.begin(9600);
  51.  
  52.   // Initialize IR receiver
  53.   IrReceiver.begin(irPin, ENABLE_LED_FEEDBACK); // Start the IR receiver
  54.  
  55.   // Initialize readings array
  56.   for (int i = 0; i < NUM_READINGS; i++) {
  57.     readings[i] = MAX_VALID_DISTANCE;
  58.     total += readings[i];
  59.   }
  60. }
  61.  
  62. void loop() {
  63.   // Handle IR remote input
  64.   handleIRRemote();
  65.  
  66.   // Trigger the ultrasonic sensor
  67.   digitalWrite(trigPin, LOW);
  68.   delayMicroseconds(2);
  69.   digitalWrite(trigPin, HIGH);
  70.   delayMicroseconds(10);
  71.   digitalWrite(trigPin, LOW);
  72.  
  73.   // Measure the duration of the echo pulse
  74.   echoDuration = pulseIn(echoPin, HIGH, 30000); // Timeout after 30ms
  75.  
  76.   // Calculate the distance in cm
  77.   if (echoDuration == 0 || echoDuration >= 30000) {
  78.     // No echo received or timeout
  79.     distance = MAX_VALID_DISTANCE;
  80.     Serial.println("No echo received or timeout.");
  81.   } else {
  82.     distance = echoDuration * 0.034 / 2;
  83.     Serial.print("Echo Duration: ");
  84.     Serial.print(echoDuration);
  85.     Serial.print(" us, Distance: ");
  86.     Serial.print(distance);
  87.     Serial.println(" cm");
  88.   }
  89.  
  90.   // Filter the distance readings
  91.   total = total - readings[readIndex];   // Subtract the oldest reading
  92.   readings[readIndex] = distance;        // Store the new reading
  93.   total = total + readings[readIndex];   // Add the new reading to total
  94.   readIndex = (readIndex + 1) % NUM_READINGS; // Advance index
  95.   averageDistance = total / NUM_READINGS;
  96.  
  97.   Serial.print("Average Distance: ");
  98.   Serial.println(averageDistance);
  99.  
  100.   unsigned long currentMillis = millis();
  101.   unsigned long currentMicros = micros(); // For buzzer timing
  102.  
  103.   // Alarm logic
  104.   if (alarmEnabled) {
  105.     if (averageDistance > 0 && averageDistance < thresholdDistance) {
  106.       digitalWrite(ledPin, HIGH); // Turn ON LED
  107.       buzzerActive = true;        // Activate the buzzer
  108.  
  109.       // Display alarm message
  110.       lcd.setCursor(0, 1);
  111.       lcd.print("Smoke Detected! ");
  112.     } else {
  113.       digitalWrite(ledPin, LOW);  // Turn OFF LED
  114.       buzzerActive = false;       // Deactivate the buzzer
  115.       digitalWrite(buzzerPin, LOW); // Ensure buzzer is OFF
  116.       buzzState = false;          // Reset buzz state
  117.  
  118.       // Clear LCD message if necessary
  119.       lcd.setCursor(0, 1);
  120.       lcd.print("                "); // Clear the row
  121.     }
  122.   } else {
  123.     // If the alarm system is disabled
  124.     digitalWrite(ledPin, LOW);     // Ensure LED is OFF
  125.     buzzerActive = false;          // Deactivate the buzzer
  126.     digitalWrite(buzzerPin, LOW);  // Ensure buzzer is OFF
  127.     buzzState = false;             // Reset buzz state
  128.  
  129.     lcd.setCursor(0, 1);
  130.     lcd.print("ALARM DISABLED  "); // Display disabled message
  131.   }
  132.  
  133.   // Manual buzzer control
  134.   if (buzzerActive) {
  135.     if (currentMicros - previousBuzzMicros >= buzzIntervalMicros) {
  136.       previousBuzzMicros = currentMicros;
  137.       buzzState = !buzzState;
  138.       digitalWrite(buzzerPin, buzzState ? HIGH : LOW);
  139.     }
  140.   } else {
  141.     digitalWrite(buzzerPin, LOW); // Ensure buzzer is OFF when not active
  142.   }
  143.  
  144.   // No need for delay here
  145. }
  146.  
  147. // Function to handle IR remote input
  148. void handleIRRemote() {
  149.   // Check if an IR signal is available
  150.   if (IrReceiver.decode()) {
  151.     Serial.print("IR Code Received: ");
  152.     Serial.print(IrReceiver.decodedIRData.command, HEX);
  153.     Serial.print(" (HEX), ");
  154.     Serial.print(IrReceiver.decodedIRData.command);
  155.     Serial.println(" (DEC)");
  156.  
  157.     // Use the actual received code
  158.     uint8_t receivedCommand = IrReceiver.decodedIRData.command;
  159.  
  160.     // Handle the command
  161.     switch (receivedCommand) {
  162.       case 0xA2: // Replace 0xA2 with the actual code from your remote
  163.         alarmEnabled = !alarmEnabled; // Toggle alarm state
  164.  
  165.         // When alarm is toggled, reset alarm state
  166.         digitalWrite(ledPin, LOW);    // Turn OFF LED
  167.         buzzerActive = false;         // Deactivate the buzzer
  168.         digitalWrite(buzzerPin, LOW); // Ensure buzzer is OFF
  169.         buzzState = false;            // Reset buzz state
  170.  
  171.         if (alarmEnabled) {
  172.           lcd.setCursor(0, 0);
  173.           lcd.print("Fire Alarm Ready");
  174.           lcd.setCursor(0, 1);
  175.           lcd.print("                "); // Clear any old messages
  176.           Serial.println("Alarm Enabled");
  177.         } else {
  178.           lcd.setCursor(0, 0);
  179.           lcd.print("Alarm is OFF    ");
  180.           lcd.setCursor(0, 1);
  181.           lcd.print("ALARM DISABLED  ");
  182.           Serial.println("Alarm Disabled");
  183.         }
  184.         break;
  185.  
  186.       default:
  187.         lcd.setCursor(0, 0);
  188.         lcd.print("Unknown Command ");
  189.         Serial.println("Unknown IR Command Received");
  190.         break;
  191.     }
  192.  
  193.     IrReceiver.resume(); // Prepare to receive the next signal
  194.   }
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement