I am a new member of the forum and recently in the arduino world as well and it appears that I have a problem with my flame sensors interacting with my servo. First off, there are two flame sensors, flame sensor A will be moving the servo 90 degrees, and flame sensor 180 degrees. The problem is, only sensor A works when fire is present, but with sensor B, the servo doesn't move. I would grealtly appreciate it if someone helps me, as this is my entry in my robotics contest in my school.
#include<SoftwareSerial.h> #include <Servo.h>
Servo myservo;
int flame1Pin = A5;
int flame2Pin = A1;
int flame1Value = 0;
int flame2Value = 0;
int pos = 0;
And you've failed to understand how servos work. write(x) sends the servo TO position x. It doesn't move it by x degrees from it's current position. The positions x can be from 0 to 180. There is no such things as position -90.
{
if (flame1Value < 1000) //flame A
{
myservo.write(pos = -90);
}
else {
myservo.write(pos = 0);
}
}
{
flame2Value = analogRead(flame2Pin);
{
if (flame2Value < 1000) //flame B
{
myservo.write(pos = 90);
}
else {
myservo.write(pos = 0);
}
}
}
1. FORMAT YOUR CODE. You can press control-T in the IDE and it will do it for you. There is absolutely no reason to have braces all over the place and blocks not lines up and make your code harder to read and understand.
2. Follow the logic here. If sensor 1 sees flame it moves the servo to -90 and if not to 0. THEN if sensor 2 detects flame it moves to 90 and if not then to 0. It doesn't matter that sensor 1 detected flame, if sensor 2 doesn't then the code says to move the servo to 0. It doesn't matter if sensor 2 sees flame, the code goes back through loop and if sensor 1 doesn't see flame it says to go back to 0. So your two sections are basically fighting with one another over where to move this servo.
You need to think carefully about your logic. You have 4 possibilities to deal with.
1. neither sensor sees flame,
2. only sensor 1,
3. only sensor 2, and
4. both sensors.
Just one question, how can I make the servo move without going through the conditions of flame1?
But I'm not convinced that is actually what you want to do.
It would be better if you laid out what you want to have happen in each of the four cases. If you can't spell out what you want to happen in plain English, then you'll never be able to write code for it. If you'll take the time to write it out without the code then the code practically writes itself.
Thanks for the quick reply, highly appreciate it The scenario I actually want to make is when flame1 detects fire, it should send the servo to 90, and if flame2 detects fire, it should the servo to 180.
Delta_G:
Did you miss the part about describing the FOUR possible states? You're only describing two. You aren't ready to write code until you can clearly explain what you want the code to do.
What is this that you are building?
Please don't ignore any more questions if you want help. There are several posted above that you skipped. People are putting in THEIR time and effort to fix YOUR project. The least you can do is not ignore them.
It is an add-on accessory for automating a portable fire extinguisher. The way it works is that if the flame sensors detect fire, it will trigger the servo, activating the extinguisher and putting out the fire. I decided that it will be better if
3 flame sensors are added and placed on the front, left, and right side of the fire extinguisher (the extinguisher is cylindrical). For example, if fire is detected on the right of side of the extinguisher, a second servo placed under the extinguisher will make the whole extinguisher rotate to the right putting the fire out.
Sorry for losing track of some questions mentioned above as I was busy.