Problem using servo with if condition

Hi,i'm doing a project for school and getting problems when i try to control a servo with if conditions.
The servo moves to a position as soon as the program loads and completely ignores the if condition.As far as I can tell this only happens when i try to use the servo with an if statement.

The project involves automating a building to respond in case of a fire ; this includes using a vacuum to remove smoke, sprinkler system, warning lights , unlocking a door that uses solenoids and a servo to open and close a door.

I'm also using a capacitive sensor to open the door under normal conditions.
The entire system can be armed or disarmed by using a push button.

Can someone help me resolve this problem please???

completed_program_m.ino (2.32 KB)

The project involves automating a building to respond in case of a fire ; this includes using a vacuum to remove smoke, sprinkler system, warning lights , unlocking a door that uses solenoids and a servo to open and close a door.

Why do you detach the servo when you get done waving it around?

You have serial output that you are hogging. We can't help you unless you are willing to share.

    sensorValue1 = analogRead(analogInPin);

Do you really have an analogIn attached to that pin? What is an analogIn?

Meaningful names will make your code much easier to understand.

I don't see how the condition in the second if statement in this fragment can ever fire 'true' unless Serial.print takes a long time. And I'm sorry but I cannot figure out what you're trying to do here, so I can't suggest an alternative. But I bet your issue is in this neighborhood.

    if (sensorValue < Setpoint) {
      long start = millis();
      long total1 =  cs_4_2.capacitiveSensor(30);
      Serial.print(millis() - start);
      Serial.print("\t"); 
      if((millis() - start) >100)

-br

The analogInPin1 and analogInPin2 are smoke detectors.

This piece of code reads the capacitive sensor to detect that someone is by the door, it will only execute once there is no smoke in the building.

if (sensorValue < Setpoint) {
long start = millis();
long total1 = cs_4_2.capacitiveSensor(30);
Serial.print(millis() - start);
Serial.print("\t");
if((millis() - start) >100)

Hi Xeon,

Do you know that the millisecond counter will continue to count while you are printing? Maybe you need to do something like the following:

 if (sensorValue < Setpoint) {
      long start = millis();
      long total1 =  cs_4_2.capacitiveSensor(30);
      long finish = millis()-start;
      Serial.print(finish);
      Serial.print("\t"); 
      if(finish >100) ...

Thanks patduino.........I've simplified the code so the door will only respond to one sensor.
The problem still persist;this only happens when I try to use if statements.

#include <Servo.h>

Servo myservo;
int smoke = A5;
int pos = 0;

void setup()
{
myservo.attach(11);
}

void loop()
{
if(smoke>100){
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos); // Make the door open
delay(15);

}
myservo.detach(); // I detach the servo here so it wont close the door
}
}

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

xeon:
Thanks patduino.........I've simplified the code so the door will only respond to one sensor.
The problem still persist;this only happens when I try to use if statements.

#include <Servo.h> 

Servo myservo;
int smoke = A5;
int pos = 0;




void setup() 
{ 
myservo.attach(11); 
} 


void loop() 
{ 
if(smoke>100){
for(pos = 0; pos < 180; pos += 1) 
{ 
myservo.write(pos); // Make the door open 
delay(15); 


} 
myservo.detach(); // I detach the servo here so it wont close the door
}
}

I don't think I made my point clear enough. Let me try once more.

In that code fragment, you snapshot the value of millis into the variable named start. Then, four lines later, you ask whether the current time is 100 ms later than that: than it was four lines ago. This will never be true. The code in the if clause will never execute. It may as well not be there.

So instead you always end up in the else clause, which sets the servo to a fixed position. Which was, I believe, your complaint.

Does that help?

-br

Thanks billroy.......I removed everything fro the circuit and left the smoke sensor and servo.
Then i modified the code as shown my last post.
The servo keeps shifting as soon as power is plugged into it.

The analogInPin1 and analogInPin2 are smoke detectors.

So, why are the variables not called smokeDetectorPin1 and smokeDetectorPin2? That way, anyone that looks at the code can tell what those pins are for.

sensorValue1 is another vague term. smokeDetectorValue1 is a much better choice.

Lest you think I am picking on you, I think that you'll find that the code is much easier to write if you use meaningful names.

   myservo.detach();  // I detach the servo here so it wont close the door

The servo won't close the door unless the code tells it to close the door, detached or not. Typically, detaching a servo is like using Serial.flush(). Misguided, at best.

Which, of course, brings up the question why isn't the servo called doorServo? Obviously, it's your servo. It isn't mine. It isn't Joe's. But, it has a purpose. Use that purpose in the name.

PaulS if you didn't realize i'm really new at this. I know my code isn't very well done but i'm learning.
I used the detach so the servo wont go back to zero once it opens the door...........when i run this code with out the if it will open and close the door continuously.

I think I see the problem in your code...

You are assigning smoke to the pin's number, but never actually read the value of the pin. "smoke" will never be more than 100, although the value of pin A5 may!

Check for examples of how to setup an analog input pin, and how to read it.

You're close now! Let the fun begin!

Pat

int smoke = A5;

...

  if(smoke>100){
 ...
  }

On the Uno:

static const uint8_t A5 = 23;

So you are testing: is 23 greater than 100?

No, it isn't.

So you are testing: is 23 greater than 100?

No, it isn't.

Which is why I keep stressing meaningful names. smokeDetectorPinN and smokeDetectorValueN are harder to mix up than names like smoke. Does smoke refer to a pin or to a value read from the pin?

smokeDetectorPinN and smokeDetectorValueN leave no doubt which is which.

Please do not feel that I am picking on you. I am only trying to help you develop good habits from the start.

Before you do the myservo.attach, you can execute myservo.write to set the position the servo will move to when the attach occurs. From the look of the code in loop, this should be 0. On an unrelated note, take a look in the reference section of the main site at the documentation for analogRead.