PIR Sensor/Servo help.

Hey everyone, i'm new to the site and new to the whole arduino language. I'm building a project for my engineering class and i'm having trouble with the programming. The project is an Automated dog feeder controlled by a PIR sensor and a servo motor. The current complication i am having is the fact that the servo is not operating with the sensor in mind.

My current code.

//______________________________________________________________\\

int motion_1 = 2;
int light_1 = 13;



// SERVO Config

#include <Servo.h>
Servo servoMain;      // Define our servo

void setup()
{
  servoMain.attach(10); // Servo on pin 10
}

void loop()
{
  servoMain.write(0);  // Turn Servo left to 45 degrees
  delay(5000);            // Delay one second
  servoMain.write(170);   // Turn servo to center position
  delay(5000);
  
  // PIR Config
  pinMode (motion_1,INPUT);
  pinMode (light_1, OUTPUT);
  
  digitalWrite (light_1,LOW);
  delay(1000); //this delay is to let the sensor settle down before taking a reading
  int sensor_1 = digitalRead(motion_1);\
  if (sensor_1 == HIGH){
    digitalWrite(light_1,HIGH);
    delay(500);
    digitalWrite(light_1,LOW);
    delay(500);
}


  }

the servo is not operating with the sensor in mind.

The servo code is unconditional - it doesn't depend on anything.
The only conditional code is the light.

What do you want to achieve?

Servo servoMain;      // Define our servo

Main? You only have one. The instance name should reflect the function of the servo. If it feeds the dog, call it dogFeeder. of foodDispenser, of something that makes more sense than servoMain.

Why are the pinMode() calls in loop()? Don't you think the mode will stay the some from iteration of loop() to the next? It will, just in case you are uncertain. They belong in setup(). You only have one motion sensor and one led. Why was it necessary to number it?

And, AWOL is right.

AWOL:

the servo is not operating with the sensor in mind.

The servo code is unconditional - it doesn't depend on anything.
The only conditional code is the light.

What do you want to achieve?

I want the servo motor to rotate 180 degrees when the PIR sensor recognizes motion, then move back to it's original pos (0 degrees) after a certain time has passed.

Does the light code work?
If so, put the servo movement in the same conditional.
If not, debug the conditional.

AWOL:
Does the light code work?
If so, put the servo movement in the same conditional.
If not, debug the conditional.

When i make motion in front of the PIR sensor an led comes on then goes off. It remains off if there isn't any motion. If that is what you are asking. My problem is when the sensor recognizes motion, the servo doesn't react to it whatsoever.

the servo doesn't react to it whatsoever

That's because your servo code will be executed irrespective of the state of the PIR.
Put your servo code in the same block as your light code.

My Servo code and my PIR code are both in the void loop() section. Is that not correct?

Is that not correct?

They belong in the same function, so, yes that is correct. But, it is not sufficient.

Suppose you want to turn the light on and feed the dog when it is dark. You could do this:
Turn the light on
If it's dark, feed the dog.

Which would be wrong. Or you could:
if it's dark
{
turn the light on
feed the dog
}

Which would be correct.

Your code is turning the light on and feeding the dog, if it is dark, the first way. Which is why it doesn't seem to work.

So this should work.

// Arduino Dog Feeder


int motion = 2;
int light = 13;



// SERVO Config

#include <Servo.h>
Servo dogFeeder;      // Define our servo

void setup()
{
  dogFeeder.attach(10); // Servo on pin 10
  pinMode (motion,INPUT);
  pinMode (light, OUTPUT);
}

void loop()
{
 
  // PIR Config

  
  digitalWrite (light,LOW);
  delay(1000); //this delay is to let the sensor settle down before taking a reading
  int sensor = digitalRead(motion);\
  if (sensor == HIGH){
    digitalWrite(light,HIGH);
    dogFeeder.write(0);  // Turn Servo to original position
    delay(500);
    digitalWrite(light,LOW);
    dogFeeder.write(170);   // Turn servo to ending position
    delay(500);
  
}


  }

I set a if(sensor == HIGH) do i need to set a if(sensor == LOW)? Or can i leave it as is?

I just realized that the code had "light" settings when my project doesn't depend on a light sensor whatsoever. It only depends on a motion sensor. so i switched digitalWrite(light,HIGH); to digitalWrite(motion,HIGH); and changed digitalWrite(light,LOW); to digitalWrite(motion,LOW);

  delay(1000); //this delay is to let the sensor settle down before taking a reading

A digital sensor does not need time to "settle down".

I just realized that the code had "light" settings when my project doesn't depend on a light sensor whatsoever. It only depends on a motion sensor. so i switched digitalWrite(light,HIGH); to digitalWrite(motion,HIGH); and changed digitalWrite(light,LOW); to digitalWrite(motion,LOW);

Why? You are just turning the internal pullup resistor off and on. The motion pin is defined as INPUT.

I just realized that the code had "light" settings when my project doesn't depend on a light sensor whatsoever. It only depends on a motion sensor. so i switched digitalWrite(light,HIGH); to digitalWrite(motion,HIGH); and changed digitalWrite(light,LOW); to digitalWrite(motion,LOW);

Why? You are just turning the internal pullup resistor off and on. The motion pin is defined as INPUT.
[/quote]

I thought digitalWrite(light,HIGH); is referring to the amount of light present. My project doesn't depend on light just motion. I don't understand why you would have digitalWrite(light,HIGH); unless you were dealing with a light detecting sensor. Is my motion sensor supposed to be an input or output?

Is my motion sensor supposed to be an input or output?

Are you telling it that there is motion? If so, it's an OUTPUT. If you are asking it if there is motion, then it's an INPUT.

PaulS:

Is my motion sensor supposed to be an input or output?

Are you telling it that there is motion? If so, it's an OUTPUT. If you are asking it if there is motion, then it's an INPUT.

Ok so my servo will be an output since i'm telling it to move and my sensor will be input since i'm asking it. Thanks for clearing that up for me. Now I just need to troubleshoot my coding.

Ok i finally solved my programming problem and my PIR sensor and servo is acting correctly. My question is there a way to delay the next time the PIR sensor will detect motion. For example, once the sensor recognizes motion it rotates the servo, now the sensor has to wait one hour before it can recognize motion again. I'm building a dog feeder and i don't want it to go off every time it detects motion. Thanks for your help!

My question is there a way to delay the next time the PIR sensor will detect motion.

Yes, of course.
However, the problem is, we can't see your code, so being more specific is difficult.