working on firefighting car ,
currently i am on the car movement.
big idea is front sensor triggered car moves forward up until safe distance from the fire.
and left and right will move the car accordingly until front sensor will face the flame.
// 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
// 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
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
//pinMode(FlamePinForawrd, INPUT);
pinMode(FlamePinForawrdAnlog, INPUT);
pinMode(FlamePinRight, INPUT);
//pinMode(FlamePinLeft, 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() {
//int FlameCheckForawrd = digitalRead(FlamePinForawrd);
int FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
int FlameCheckRight = digitalRead(FlamePinRight);
//int FlameCheckLeft = digitalRead(FlamePinLeft);
///sensorTest();
Serial.println(FlameCheckForawrdAnalog);
if (FlameCheckForawrdAnalog < 2500)
{
Serial.println("flame dected a head");
//FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
Serial.println(FlameCheckForawrdAnalog);
CAR_moveForwardWifi();
// front fire indicated by both siren on
Serial.println("sirens on");
sirenOn(LedBlue, LedRed, Buzzer);
Serial.println("pump on");
digitalWrite(RELAY_PIN, HIGH);
delay(500);}
else if(FlameCheckRight == LOW)
{
Serial.println("flame dected on the right");
Serial.println("turning right");
CAR_turnRightWifi();
Serial.println("siren on");
sirenOn(LedBlue,LedRed, Buzzer);
//updated front sensor value
//FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
//Serial.println(FlameCheckForawrdAnalog);
//if (FlameCheckForawrdAnalog < 2500)
//{
//FlameCheckForawrdAnalog = analogRead(FlamePinForawrdAnlog);
//Serial.println(FlameCheckForawrdAnalog);
//Serial.println("sirens off");
//sirenoff(LedBlue, LedRed, Buzzer);
//StopCar();
//}
delay(100);
}
//else if (FlameCheckLeft == LOW)
//{
//Serial.println("flame dected on the left");
//while(FlameCheckForawrd != LOW)
//{
//FlameCheckForawrd = digitalRead(FlamePinForawrd);
//Serial.println("turning left");
//CAR_turnLeftWifi();
//delay(200);
//}
//}
else
{Serial.println("pump off");
digitalWrite(RELAY_PIN, LOW);
Serial.println("sirens off");
sirenoff(LedBlue, LedRed, Buzzer);
StopCar();}
//delay(300);//change this va
}
// stop car
void StopCar(){
Serial.println("StopCar");
analogWrite(enable1Pin,0);
analogWrite(enable2Pin,0);
digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin3,LOW);
digitalWrite(motor2Pin4, LOW);
delay(700);
}
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
}
problem is ,even with high low sensitivity ,say on the right sensor,
it takes around 20+ secs for the sensor to stop.
how can i more accurately control the movement?