Adding a passive infrared motion sensor

I pick one of these up this morning and its got me quite confused. The following code is what I am trying to add it to. I think i got it right until the void loop and then i have no idea what to do. Can anyone help me?

And what would I edit for the servo and lights to run longer after it picks up a motion?

#include <Servo.h>

Servo myservo;

int ledPin = 11;
int inputPin = 2;
int pir State = LOW;
int val = 0;

void setup()
{
myservo.attach(9);
pinMode(ledPin, OUTPUT);
pinMode(myservo, OUTPUT);
pinMode(inputPin, INPUT);

Serial.beguine(9600)
}

void loop() {
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
analogWrite(ledPin, fadeValue);
delay(10);
}

for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
delay(10);
}
myservo.writeMicroseconds(1500);
}

To get the sketch to compile you need to make the following changes:

Change
int pir State = LOW;
to
int pirState = LOW;

remove the following line (myservo is not a pin and the pin mode is set by the servo library):
pinMode(myservo, OUTPUT);

fix the typo and add a semicolon at the end of this line
Serial.beguine(9600)

But to get the code to do what you want will require a clearer description of how you want the sketch to workl. Can you say exactly what you want the servo and LED to do In response to input on the PIR.

I want the lights to fade and the servo to spin like i have in the void loop

If you want the functions in loop only to be executed when the PIR is in a particular state then you can do this:

void loop() {
pirState = digitalRead(pirPin);
if(pirState == HIGH){ // change to LOW depending on the state you want
// your loop code here
...
}
}

My guess is that this will not do what you expect but you need to be clearer about what you want the servo do

All the servo is doing is spinning at a continuous speed when activated by the sensor

You need to say what the state of the LED and servo should be when its not activated.
You also don't say if you are using a continuous rotation servo but I guess you are from reading a post in another thread.

Perhaps something like this at the top of the sketch will help:

#define SERVO_INACTIVE 90 //the value in degrees that stops the CR servo
#define SERVO_ACTIVE 180 // the value you want for the motor to spin

Then in your code
myservo.write(SERVO_INACTIVE ); // stop the servo
and
myservo.write(SERVO_ACTIVE ); // spin the servo

All the servo is doing is spinning at a continuous speed when activated by the sensor

It's difficult to tell from that statement whether you consider that to be a Good Thing or a Bad Thing.

It's difficult to tell from that statement whether you consider that to be a Good Thing or a Bad Thing.

They said it was unclear what i wanted the servo to do so I said that all I wanted it to do was spin

ok so here is what I have. You have completely lost me. I know I have to state what the servo and LEDs are doing when active and not but I am confused on how to do so. And yes it is a continuous servo that only has to spin at 1500 microseconds.

#include <Servo.h>

Servo myservo;

int ledPin = 11;
int inputPin = 2;
int pirState = LOW;
int val = 0;

void setup()
{
myservo.attach(9);
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);

Serial.begin(9600);
}

void loop() {
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
analogWrite(ledPin, fadeValue);
delay(10);
}

for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
delay(10);
}
myservo.writeMicroseconds(1500);
}

From that sketch, you don't seem to have anything that reads the PIR state.
What is the PIR - is it an open-collector type output or a switch?

(Can you please use the "Code" button (#) when posting source code?)

I want everything in the loop to be under the PIR but I am unsure how to take what it is I have and convert it so that it only runs when a motion is detected.

I suggest you move everything currently in loop into a new function called something like activate. Call that function when the PIR activates. Perhaps have another function called deactivate that resets the servo (and led states if necessary).

how would I take my loop and move it to what you called active? Could you explain a little more or give me an example.

Cut all the code that is currently between the "{ }" braces of "loop ()" and paste it all into a new void function:

void active (void)
{
// all the code that was in "loop ()" copy to here.
}

Then in "loop()" simply call "active ();" when your sensor triggers.

Is this what you were saying to do and something is wrong. I really have no idea what I am doing.

#include <Servo.h>

Servo myservo; 

int ledPin = 11;
int inputPin = 2;
int pirState = LOW;
int val = 0;

void setup() 
{
  myservo.attach(9);
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin, INPUT);
  
  Serial.begin(9600);
} 

void active (void){
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) { 
    analogWrite(ledPin, fadeValue);         
    delay(10);                            
  } 

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);            
    delay(10);                            
  } 
  myservo.writeMicroseconds(1500);
}

void loop()  {
    val = digitalRead(inputPin);
  if (val == HIGH) {           
    digitalWrite(active, HIGH);
    if (pirState == LOW) {
      Serial.println("Motion detected!");
      pirState = HIGH;
    }
  } else {
    digitalWrite(active, LOW);
    if (pirState == HIGH){
      Serial.println("Motion ended!");
      pirState = LOW;
    }
}
digitalWrite(active, LOW);

"active" is a function, not a pin.

void loop()  {

  if (someCondition) {
    active ();
  }
}

Is that all I need in the loop or am I replacing something with that

I think this is it but I cant test it at this moment. Could you tell me if the noob finally right?

#include <Servo.h>

Servo myservo; 

int ledPin = 11;
int inputPin = 2;
int pirState = LOW;
int val = 0;

void setup() 
{
  myservo.attach(9);
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin, INPUT);
  
  Serial.begin(9600);
} 

void active (void){
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) { 
    analogWrite(ledPin, fadeValue);         
    delay(10);                            
  } 

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);            
    delay(10);                            
  } 
  myservo.writeMicroseconds(1500);
}

void loop()  {
  if (val == HIGH) {
    active ();
  }
}

Well, almost :wink:

You test "val" in "loop()", but you haven't given it a value (hint: think digitalRead)

I think you may find things are clearer if you start with a tutorial that explains how to read the state of a pin and control an LED. This one from ladyada is highly regarded: Arduino Tutorial - Lesson 5