// https://forum.arduino.cc/t/firefighting-car-direction-from-flame-sensor/1301885/
// Setting Motor A
int enable1Pin = 14;
int motor1Pin1 = 27;
int motor1Pin2 = 26;
//Settin Motor B
int enable2Pin = 32;
int motor2Pin3 = 25;
int motor2Pin4 = 33;
// define pins for flame sensor
//#define FlamePinForawrd 17
#define FlamePinForawrdAnlog 4
#define FlamePinRight 15
//#define FlamePinLeft
// define pins for gas sensor
#define GasSensorDi 17
// pin 13 that connects to the relay to control the pump
#define RELAY_PIN 12
//pins for siren led and buzzer
//siren
#define LedBlue 2
#define LedRed 21
#define Buzzer 19
//vars for falme sensor read
int FlameCheckForawrdAnalog;
int FlameCheckRight;
//vars for gas sensor read
int GasSensorD;
int three60forward = 0;//var for 360 turn
//time vars for front sensor trigger and movement forward
const unsigned long eventCarMovementInterval = 200;
unsigned long previouseventCarMovement = 0;
//time vars for front sensor trigger and movment left and right
const unsigned long eventCarSideMovementInterval = 200;
unsigned long previouseventCarSideMovement = 0;
//time vars for siren
const unsigned long eventSirenInterval = 500;
unsigned long previousEventSiren = 0;
//time vars for water pump
const unsigned long eventPumpInterval = 500;
unsigned long previousEventPump = 0;
//var for movement forward on the gas condition
const unsigned long eventCarMovementGasInterval = 2500;
unsigned long previouseventGasCarMovement = 0;
void setup() {
//modding motor A
pinMode(enable1Pin, OUTPUT);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
//modding Motor B
pinMode(enable2Pin, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(motor2Pin4, OUTPUT);
//setting flame sensor as input
//pinMode(FlamePinForawrd, INPUT);
pinMode(FlamePinForawrdAnlog, INPUT);
pinMode(FlamePinRight, INPUT);
//pinMode(FlamePinLeft, INPUT);
//setting gas sensor as input
pinMode(GasSensorDi, INPUT);
//setting siren for right
pinMode(LedBlue, OUTPUT); //defined port for right leds
pinMode(LedRed, OUTPUT);
pinMode(Buzzer, OUTPUT); // port front buzzer
// initaite serial monitor
Serial.begin(9600);
}
void loop() {
unsigned long currentTime = millis();
//vars for storing flame sensor vlaues
FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
FlameCheckRight = digitalRead(FlamePinRight);
Serial.println(FlameCheckForawrdAnalog);
//vars for storing gas sensor vlaues
GasSensorD = digitalRead(GasSensorDi);
///sensorTest();
if ((FlameCheckForawrdAnalog < 4090) && (FlameCheckForawrdAnalog > 3500))
{
if (currentTime - previouseventCarMovement >= eventCarMovementInterval) {
Serial.print("flame ahead ");
//FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
Serial.println(FlameCheckForawrdAnalog);
CAR_moveForwardWifi();
previouseventCarMovement = currentTime;
}
if (currentTime - previousEventSiren >= eventSirenInterval) {
// siren on
Serial.println("sirens on ");
// sirenOn(LedBlue, LedRed, Buzzer);
previousEventSiren = currentTime;
}
}
if (FlameCheckForawrdAnalog < 2500 ) { //turn pump while in front sensor triggred
if (currentTime - previousEventPump >= eventPumpInterval ) {
Serial.print("pump on ");
digitalWrite(RELAY_PIN, HIGH);
previousEventSiren = currentTime;
}
}
else if (FlameCheckRight == LOW && !FlameCheckForawrdAnalog < 4000 )
{
if (currentTime - previouseventCarSideMovement >= eventCarSideMovementInterval) {
Serial.print("flame right ");
Serial.print("turning right ");
CAR_turnRightWifi();
Serial.print("siren on ");
// sirenOn(LedBlue,LedRed, Buzzer);
previouseventCarSideMovement = currentTime;
}
}
//else if (FlameCheckLeft == LOW)
//{
//Serial.print("flame dected on the left");
//while(FlameCheckForawrd != LOW)
//{
//FlameCheckForawrd = digitalRead(FlamePinForawrd);
//Serial.print("turning left");
//CAR_turnLeftWifi();
//delay(200);
//}
//}
else if (GasSensorD == LOW && !FlameCheckForawrdAnalog < 4000) {
if (currentTime - previousEventSiren >= eventSirenInterval) {
// siren on
Serial.print("sirens on ");
// sirenOn(LedBlue, LedRed, Buzzer);
previousEventSiren = currentTime;
}
if (three60forward < 20) {
Serial.print("gas detected turn right ");
Serial.print(three60forward);
CAR_turnRightWifi();
three60forward++;
}
else {
{ Serial.print("(move forawrd) ");
CAR_moveForwardWifi();
delay(1000);
}
three60forward = 0;
}
}
else
{ Serial.print("pump off ");
digitalWrite(RELAY_PIN, LOW);
Serial.print("sirens off ");
sirenoff(LedBlue, LedRed, Buzzer);
StopCar();
}
delay(250);
}
// stop car
void StopCar() {
Serial.print("StopCar ");
analogWrite(enable1Pin, 0);
analogWrite(enable2Pin, 0);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
}
void CAR_moveForwardWifi() {
//Set speed motor A and B
analogWrite(enable1Pin, 186);
analogWrite(enable2Pin, 200);
//move forawrd
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin3, HIGH);
digitalWrite(motor2Pin4, LOW);
}
void CAR_moveBackwardWifi() {
//setting speed motor A and B
analogWrite(enable1Pin, 190);
analogWrite(enable2Pin, 200);
//back movement
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, HIGH);
}
void CAR_turnLeftWifi() {
//Set speed motor A and B
analogWrite(enable1Pin, 255);
analogWrite(enable2Pin, 0);
//turn left
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
}
void CAR_turnRightWifi() {
//Set speed motor A and B
analogWrite(enable1Pin, 255);
analogWrite(enable2Pin, 255);
//turn right
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3, HIGH);
digitalWrite(motor2Pin4, LOW);
}
void sensorTest() {
while (1) {
for (int i = 0; i < 3; i++) {
Serial.print(digitalRead(i + 15)); // read "flame" sensors
}
Serial.println(); // print the sensor state
delay(250);
}
}
//functions for siren
void sirenOn(int Led1Pin, int Led2Pin, int BuzzerPin)
{
int i;
for (int i = 700; i < 800; i++) // duration of the first sound 350 2 millisecond cycles.
{
// tone(BuzzerPin, i);
// delay(15);
digitalWrite(Led1Pin, HIGH); // turn on the leds
digitalWrite(Led2Pin, LOW);
delay(30);
digitalWrite(Led1Pin, LOW); // blinking leds
digitalWrite(Led2Pin, HIGH);
delay(30);
delay(15); // wait 1 millisecond and restart from the for statement (100 repetitions)
}
for (i = 800; i > 700; i--) {
// tone(BuzzerPin, i);
// delay(15);
digitalWrite(Led1Pin, HIGH); // turn on the leds
digitalWrite(Led2Pin, LOW);
delay(30);
digitalWrite(Led1Pin, LOW); // blinking leds
digitalWrite(Led2Pin, HIGH);
delay(30);
delay(15); // wait 1 millisecond and restart from the for statement (100 repetitions)
}
}
void sirenoff(int Led1Pin, int Led2Pin, int BuzzerPin)
{
digitalWrite(Led1Pin, LOW); // turn off blinking leds
digitalWrite(Led2Pin, LOW);
tone(BuzzerPin, 0); // turn off buzzer
}
Flame
Right
Flame
Analog