#include "FireDetection.h"
#include <Wire.h>
#include <PWMServo.h>
//#include <Servo.h>
FireDetection fireDetection1;
FireDetection fireDetection2;
PWMServo servoMotor;
const int ledPin = 3; // PWM-capable pin for LED
const int buzzerPin = 22; // Pin for piezo buzzer
bool communicationEnabled = true;
String lastSeveritySent = ""; // Track last sent severity level
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
delay(5000);
servoMotor.attach(9);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
if (!fireDetection1.begin(&Wire)) Serial.println("Failed to initialize MLX90640 sensor 1 on Wire");
if (!fireDetection2.begin(&Wire1)) Serial.println("Failed to initialize MLX90640 sensor 2 on Wire1");
servoMotor.write(0);
}
void loop() {
delay(2000);
float maxTemp1, maxTemp2;
int fireLevel1 = fireDetection1.detectFire(maxTemp1);
int fireLevel2 = fireDetection2.detectFire(maxTemp2);
String severityLevel;
float highestTemp = max(maxTemp1, maxTemp2);
if (fireLevel1 == 3 || fireLevel2 == 3) {
severityLevel = "High";
Serial.println("High severity detected");
} else if (fireLevel1 == 2 || fireLevel2 == 2) {
severityLevel = "Medium";
Serial.println("Medium severity detected");
} else if (fireLevel1 == 1 || fireLevel2 == 1) {
severityLevel = "Low";
Serial.println("Low severity detected");
} else {
severityLevel = "None";
Serial.println("No severity detected");
}
fireDetection1.printThermalData();
fireDetection2.printThermalData();
if (communicationEnabled && severityLevel != lastSeveritySent) {
if (severityLevel != "None") {
Serial1.println("FIRE_DETECTED");
Serial1.println("Severity Level: " + severityLevel);
Serial1.println("Max Temp: " + String(highestTemp, 2));
} else {
Serial1.println("NO_FIRE_DETECTED");
}
lastSeveritySent = severityLevel;
}
if (severityLevel == "High") {
digitalWrite(ledPin, HIGH);
playBFPWarningTone();
servoMotor.detach();
} else if (severityLevel == "Medium") {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000);
} else if (severityLevel == "Low") {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
servoMotor.attach(9);
static int angle = 0;
static int step = 30;
angle += step;
if (angle >= 180 || angle <= 0) {
step = -step;
}
servoMotor.write(angle);
}
}
void playBFPWarningTone() {
for (int i = 0; i < 50; i++) {
tone(buzzerPin, 1500);
delay(100);
noTone(buzzerPin);
delay(100);
}
}