1#include <LiquidCrystal.h>
2#include <Servo.h>
3
4// --- Pin Definitions ---
5LiquidCrystal lcd(3, 4, 5, 6, 7, 8);
6Servo scannerServo;
7
8const int servoPin = 9;
9const int greenLedPin = 22;
10const int redLedPin = 50;
11const int buzzerPin = 23;
12const int flameSensorPin = 52;
13
14int servoPos = 0; // Current servo position
15int servoDirection = 1; // 1 means moving forward, -1 means moving backward
16
17void setup() {
18 lcd.begin(16, 2);
19 lcd.print("System Booting..");
20
21 pinMode(greenLedPin, OUTPUT);
22 pinMode(redLedPin, OUTPUT);
23 pinMode(buzzerPin, OUTPUT);
24 pinMode(flameSensorPin, INPUT);
25
26 scannerServo.attach(servoPin);
27 scannerServo.write(servoPos); // Start at 0 degrees
28
29 delay(1000);
30 lcd.clear();
31}
32
33void loop() {
34 // Read the flame sensor
35 int flameDetected = digitalRead(flameSensorPin);
36
37 // ---> THE FIX IS HERE <---
38 // We changed this to LOW. Now, if the sensor outputs LOW (0V), the system sweeps.
39 // If it detects fire and outputs HIGH (5V), it jumps to the alarm mode.
40 if (flameDetected == LOW) {
41
42 // --- NO FLAME: SWEEP NORMALLY ---
43 digitalWrite(redLedPin, LOW); // Red OFF
44 digitalWrite(greenLedPin, HIGH); // Green ON
45 digitalWrite(buzzerPin, LOW); // Buzzer OFF
46
47 lcd.setCursor(0, 0);
48 lcd.print("Fire Scanner ON ");
49 lcd.setCursor(0, 1);
50 lcd.print("Area Secure... ");
51
52 // Move servo to the current position
53 scannerServo.write(servoPos);
54
55 // Step the position forward or backward
56 servoPos += servoDirection;
57
58 // If we hit the boundaries (0 or 80), reverse the direction
59 if (servoPos >= 80) {
60 servoDirection = -1; // Start moving backward
61 } else if (servoPos <= 0) {
62 servoDirection = 1; // Start moving forward
63 }
64
65 delay(20); // Speed of the sweep
66
67 }
68 else {
69 // --- FLAME DETECTED: LOCK TARGET & ALARM ---
70 // Because we DO NOT change "servoPos" here, the motor freezes in its exact spot.
71 // When the fire is removed, the code goes back to the "if" block above
72 // and resumes moving from this exact spot!
73
74 digitalWrite(redLedPin, HIGH); // Red ON
75 digitalWrite(greenLedPin, LOW); // Green OFF
76 digitalWrite(buzzerPin, HIGH); // Buzzer ON
77
78 lcd.setCursor(0, 0);
79 lcd.print("FIRE AT ");
80 lcd.print(servoPos);
81 lcd.print(" DEG! ");
82
83 lcd.setCursor(0, 1);
84 lcd.print("FIRE DETECTED!! ");
85
86 delay(100);
87 }
88}