Servo motor and relay working on their own without receiving signal from sensor

Hello, everyone. I need some help with the coding of a project I'm making.
I'm trying to make a water turret that shoots out water when the sensor detects fire. I used a five way fire sensor, the picture will be attached in this post.


Here is my code:

#include <Servo.h>
Servo servo1; //the bottom servo
Servo servo2; //the top servo
int pos = 90; //servo position in degrees

int Relay = 13; //relay to pin 13 digital

int flame1 = A0;
int flame2 = A1;
int flame3 = A2;
int flame4 = A3;
int flame5 = A4;

void setup() {
  Serial.begin(9600);
  servo1.attach(6); //attach servo 1 to pin 6 digital
  servo2.attach(9); //attach servo 2 to pin 9 digital
  
  pinMode(flame1, INPUT);
  pinMode(flame2, INPUT);
  pinMode(flame3, INPUT);
  pinMode(flame4, INPUT);
  pinMode(flame5, INPUT);

  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  flame1 = analogRead(A0);
  flame2 = analogRead(A1);
  flame3 = analogRead(A2);
  flame4 = analogRead(A3);
  flame5 = analogRead(A4);

  if(flame1 == LOW && flame2 == LOW && flame3 == LOW && flame4 == LOW && flame5 == LOW) //all sensor inactive
  {
  digitalWrite(13,LOW); //Relay off, water pump won't receive signal
  servo1.write(90);
  servo2.write(90);
  }
  else if(flame1 == HIGH)
  {
    servo1.write(150); // bottom servo at 150 degrees
    delay(20);   
  for (pos = 90; pos <= 110; pos += 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 110; pos >= 80; pos -= 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 80; pos <= 90; pos += 1){
    servo2.write(pos);
    delay(10);
  digitalWrite(13, HIGH);
  }
  delay(500);
  }
  else if(flame2 == HIGH)
  {
    servo1.write(120); //bottom servo at 120 degrees
    delay(20);   
  for (pos = 90; pos <= 110; pos += 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 110; pos >= 80; pos -= 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 80; pos <= 90; pos += 1){
    servo2.write(pos);
    delay(10);
  digitalWrite(13, HIGH);
  }
  delay(500);
  }
  else if(flame3 == HIGH)
  {
    servo1.write(90); //bottom servo at 90 degrees
    delay(20);
  for (pos = 90; pos <= 110; pos += 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 110; pos >= 80; pos -= 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 80; pos <= 90; pos += 1){
    servo2.write(pos);
    delay(10);
  digitalWrite(13, HIGH);
  }
  delay(500);
  }
  else if(flame4 == HIGH)
  {
    servo1.write(60); //bottom servo at 60 degrees
    delay(20);
  for (pos = 90; pos <= 110; pos += 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 110; pos >= 80; pos -= 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 80; pos <= 90; pos += 1){
    servo2.write(pos);
    delay(10);
  digitalWrite(13, HIGH);
  }
  delay(500);  
  }  
  else if(flame5 == HIGH) 
  {
    servo1.write(30); //bottom servo at 30 degrees
    delay(20);
    
  for (pos = 90; pos <= 110; pos += 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 110; pos >= 80; pos -= 1){
    servo2.write(pos);
    delay(10);
    }
  for (pos = 80; pos <= 90; pos += 1){
    servo2.write(pos);
    delay(10);
  digitalWrite(13, HIGH);
  }
  delay(500);
  }    
}

The servo motors and the relay are working even though there's no fire detected.
Is there something wrong with my code, or is the sensor broken?

Thank you for the help.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you developed your code in stages?

Have you got code that JUST reads the IR sensors and outputs a signal when fire is detected?

analogRead returns a value between 0 and 1023, not HIGH or LOW.

Use your serial.print to output to the IDE monitor your various flame variables.
You will find they will be greater than 0 which means they will not be logic LOW.

You need the basic input code I suggested to calibrate your setup, work out the levels for no flame and the levels for a flame situation.

Please post a link to data/specs of the sensor array.

Tom.... :slight_smile:

Hi, thanks for replying.
Here is my circuit diagram. Sorry I had to draw it by hand because I haven't install the program to make the circuit.

I have tested each component/module separately to see if they work.

The first fire sensor code I've tried is this:

// lowest and highest sensor readings:
const int sensorMin = 0;     // sensor minimum
const int sensorMax = 1024;  // sensor maximum

void setup() {
  // initialize serial communication @ 9600 baud:
  Serial.begin(9600);  
}
void loop() {
  // read the sensor on analog A0:
  int sensorReading = analogRead(A0);
  int sensorReading = analogRead(A1);
  int sensorReading = analogRead(A2);
  // map the sensor range (four options):
  // ex: 'long int map(long int, long int, long int, long int, long int)'
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
  
  // range value:
  switch (range) {
  case 0:    // A fire closer than 1.5 feet away.
    Serial.println("** Close Fire **");
    break;
  case 1:    // A fire between 1-3 feet away.
    Serial.println("** Distant Fire **");
    break;
  case 2:    // No fire detected.
    Serial.println("No Fire");
    break;
  }
  delay(1);  // delay between reads
}

However when I added the A1 and A2 port for the other sensors I've got this error messege:

exit status 1
redeclaration of 'int sensorReading'

The second flame sensor code I've tried is this:

int flame1 = A0;
int flame2 = A1;
int flame3 = A2;
int flame4 = A3;
int flame5 = A4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(flame1, INPUT);
  pinMode(flame2, INPUT);
  pinMode(flame3, INPUT);
  pinMode(flame4, INPUT);
  pinMode(flame5, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  flame1 = analogRead(A0);
  flame2 = analogRead(A1);
  flame3 = analogRead(A2);
  flame4 = analogRead(A3);
  flame5 = analogRead(A4);

  delay(1000);  
}

I've used this code for the project.

I can't seem to find the "official" website for the sensor, but I got the spec from the seller:

  1. 3.3V-9V power supply, and is compatible with most of the minimum system
  2. The item is able to detect the flame band respectively in the range of 700-1100 nm, short-wave near-infrared (SW-NIR), and is output via an electric signal (voltage signal).

Thank you for the help.

You are checking the sensor values against LOW. LOW = 0 and the result of an analogRead is hardly ever exactly 0.

Steve

Yep, my bad :smiley:

Now I just have to code the values of each sensor.

Thank you.

Hi,
A little thing that you need to know about the ADC, there is only one ADC, it is multiplexed across the analog inputs.
This takes time as there is an input capacitor involved with the ADC input, and if you switch input too quick, you get erratic readings from each input.
A solution is to read the input twice, using the last reading as a valid reading.
So your code can be edited from;

  flame1 = analogRead(A0);
  flame2 = analogRead(A1);
  flame3 = analogRead(A2);
  flame4 = analogRead(A3);
  flame5 = analogRead(A4);

to;

  flame1 = analogRead(A0);
  flame1 = analogRead(A0);
  flame2 = analogRead(A1);
  flame2 = analogRead(A1);
  flame3 = analogRead(A2);
  flame3 = analogRead(A2);
  flame4 = analogRead(A3);
  flame4 = analogRead(A3);
  flame5 = analogRead(A4);
  flame5 = analogRead(A4);

Tom... :slight_smile: :slight_smile: :slight_smile:

benedictus_christo:
Now I just have to code the values of each sensor.

Not really, you can probably just use a sensorMin value like you had in the test program. Just make it a value > 0, say 15 or so and check against that not LOW.

Steve

Does it matter that case 0 and case 1 overlap in your fire sensor code? Shouldn’t they be mutually exclusive?

slipstick:
Not really, you can probably just use a sensorMin value like you had in the test program. Just make it a value > 0, say 15 or so and check against that not LOW.

Steve

I've tried again the code and there's no error. However, the sensor reading is always 0. Is there anything wrong with my code?

const int sensorMin = 0;
const int sensorMax = 1023;
int flame1 = A0;
int flame2 = A1;
int flame3 = A2;
int flame4 = A3;
int flame5 = A4;
void setup() {
  Serial.begin(9600);
  pinMode(flame1, INPUT);
  pinMode(flame2, INPUT);
  pinMode(flame3, INPUT);
  pinMode(flame4, INPUT);
  pinMode(flame5, INPUT);
}

void loop() {
  flame1 = digitalRead(A0);
  flame1 = digitalRead(A0);
  int range1 = map(flame1, sensorMin, sensorMax, 0, 10);
  Serial.println("1 = ");
  Serial.println(range1);
delay(1000);
 
  flame2 = digitalRead(A1);
  flame2 = digitalRead(A1);
  int range2 = map(flame2, sensorMin, sensorMax, 0, 10);
  Serial.println("2 = ");
  Serial.println(range2);
delay(1000);
  
  flame3 = digitalRead(A2);
  flame3 = digitalRead(A2);
  int range3 = map(flame3, sensorMin, sensorMax, 0, 10);
  Serial.println("3 = ");
  Serial.println(range3);
delay(1000);
  
  flame4 = digitalRead(A3);
  flame4 = digitalRead(A3);
  int range4 = map(flame4, sensorMin, sensorMax, 0, 10);
  Serial.println("4 = ");
  Serial.println(range4);
delay(1000);
  
  flame5 = digitalRead(A4);
  flame5 = digitalRead(A4);
  int range5 = map(flame5, sensorMin, sensorMax, 0, 10);
  Serial.println("5 = ");
  Serial.println(range5);
delay(1000);

}

You need to print the actual sensor readings e.g. flame1, flame2 etc. If they are really constantly 0 then maybe the sensors don't work. Neither of your posted "test programs" actually seem to show that anything works. From what I can see of that sensor it can provide either analog or digital outputs and has a sensitivity adjustment. Do you have any documentation for it. Have you tried running it on 5V instead of 3.3V?

And if you're really powering 2 servos and a relay plus the Arduino and the fire sensor all from a little 9V smoke alarm battery then that will also be part of the problem.

Steve

flame1 = digitalRead(A0);
  flame1 = digitalRead(A0);

Why are you reading the sensor twice every time?

It looks to me like your code needs some factoring

Hi,
Try this code, open serial monitor and select 115200 baud.

int flame1Pin = A0;
int flame2Pin = A1;
int flame3Pin = A2;
int flame4Pin = A3;
int flame5Pin = A4;
int flame1;
int flame2;
int flame3;
int flame4;
int flame5;

void setup()
{
  Serial.begin(115200);
  Serial.println("             Code to Serial Print raw data");
  Serial.println("from all 5 analog inputs connected to 5 IR sensors");
  Serial.println("====================================================");
  pinMode(flame1Pin, INPUT);
  pinMode(flame2Pin, INPUT);
  pinMode(flame3Pin, INPUT);
  pinMode(flame4Pin, INPUT);
  pinMode(flame5Pin, INPUT);

}

void loop()
{
  flame1 = analogRead(flame1Pin);
  flame1 = analogRead(flame1Pin);
  Serial.print("flame1 = ");
  Serial.print(flame1);
  flame2 = analogRead(flame2Pin);
  flame2 = analogRead(flame2Pin);
  Serial.print("\t flame2 = ");
  Serial.print(flame2);
  flame3 = analogRead(flame3Pin);
  flame3 = analogRead(flame3Pin);
  Serial.print("\t flame3 = ");
  Serial.print(flame3);
  flame4 = analogRead(flame4Pin);
  flame4 = analogRead(flame4Pin);
  Serial.print("\t flame4 = ");
  Serial.print(flame4);
  flame5 = analogRead(flame5Pin);
  flame5 = analogRead(flame5Pin);
  Serial.print("\t flame5 = ");
  Serial.println(flame5);
  delay(1000);
}

It works on my UNO.

Tom.... :slight_smile: