0% found this document useful (0 votes)
5 views3 pages

Arduino Fire Detection and Response Code

code testing

Uploaded by

T EE
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Arduino Fire Detection and Response Code

code testing

Uploaded by

T EE
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <Servo.

h>

// L298N pins
#define ENA 2
#define IN1 3
#define IN2 4
#define IN3 5
#define IN4 6
#define ENB 7

#define FLAME_LEFT A0
#define FLAME_CENTER A1
#define FLAME_RIGHT A2
#define SMOKE_SENSOR A3

#define RELAY_PUMP 10
#define BUZZER 13

#define TRIG_PIN 8
#define ECHO_PIN 9
#define SERVO_PIN 11

Servo nozzleServo;

int servoPos = 90;


int servoTarget = 90;
unsigned long lastServoTime = 0;

// ⚡ Ultra-fast + smooth servo movement


const int servoStep = 10; // move 3° each step = smooth
const unsigned long servoInterval = 1; // every 5 ms = very fast

bool fireDetected = false;


int fireLocation = 90;

// Updated sweep angles (better spread for SG90)


const int sweepAngles[] = {0, 45, 90, 135, 180};
const int sweepCount = sizeof(sweepAngles)/sizeof(sweepAngles[0]);
int sweepIndex = 0;

void setup() {
[Link](9600);

pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);


pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENB, OUTPUT);

pinMode(RELAY_PUMP, OUTPUT);
pinMode(BUZZER, OUTPUT);

pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

[Link](SERVO_PIN);
[Link](servoPos);

digitalWrite(RELAY_PUMP, LOW);
digitalWrite(BUZZER, LOW);
}
void loop() {
int leftFlame = analogRead(FLAME_LEFT);
int centerFlame = analogRead(FLAME_CENTER);
int rightFlame = analogRead(FLAME_RIGHT);
int smokeVal = analogRead(SMOKE_SENSOR);

long distance = getDistance();

fireDetected = (leftFlame < 400 || centerFlame < 400 || rightFlame < 400);
if (leftFlame < 400) fireLocation = 0; // Aim left
else if (centerFlame < 400) fireLocation = 90; // Aim center
else if (rightFlame < 400) fireLocation = 180; // Aim right

bool smokeAlert = (smokeVal >= 65);


bool smokePump = (smokeVal >= 150);
bool obstacleClose = (distance > 0 && distance < 25);

// Debug output
[Link]("Left: "); [Link](leftFlame);
[Link](" | Center: "); [Link](centerFlame);
[Link](" | Right: "); [Link](rightFlame);
[Link](" | Smoke: "); [Link](smokeVal);
[Link](" | Distance: "); [Link](distance);
[Link](" | ServoPos: "); [Link](servoPos);
[Link](" | ServoTarget: "); [Link](servoTarget);
[Link](" | FireDetected: "); [Link](fireDetected);
[Link](" | SmokeAlert: "); [Link](smokeAlert);
[Link](" | SmokePump: "); [Link](smokePump);
[Link](" | Obstacle: "); [Link](obstacleClose);

if (fireDetected) {
stopMotors();
digitalWrite(BUZZER, HIGH);
digitalWrite(RELAY_PUMP, HIGH);
moveForwardFast();

// Aim directly at fire


servoTarget = fireLocation;
updateServoSweep();

} else {
digitalWrite(RELAY_PUMP, smokePump);
digitalWrite(BUZZER, smokeAlert);

// Reset servo to center when no fire


servoTarget = 90;
updateServoSweep();

if (obstacleClose) stopMotors();
else forward();
}

delay(20);
}

void updateServoSweep() {
unsigned long now = millis();
if (now - lastServoTime >= servoInterval) {
if (servoPos < servoTarget) servoPos += servoStep;
else if (servoPos > servoTarget) servoPos -= servoStep;

// clamp to target (no overshoot)


if (abs(servoPos - servoTarget) < servoStep) servoPos = servoTarget;

[Link](servoPos);
lastServoTime = now;
}
}

void forward() {
analogWrite(ENA, 200);
analogWrite(ENB, 200);
digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW);
}

void moveForwardFast() {
analogWrite(ENA, 255);
analogWrite(ENB, 255);
digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW);
}

void stopMotors() {
analogWrite(ENA,0);
analogWrite(ENB,0);
digitalWrite(IN1,LOW); digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW); digitalWrite(IN4,LOW);
}

long getDistance() {
digitalWrite(TRIG_PIN,LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN,LOW);

long duration = pulseIn(ECHO_PIN,HIGH);


long distance = duration*0.034/2;
return distance;
}

You might also like