Timing servo

This sketch runs a servo randomly after the PIR detects motion. Problem is, the motor needs to run five seconds, then stop and wait until new motion is detected. How can I do this?

#include <Servo.h>

#define PIN_SERVO 9
#define MIN_DELAY 130
#define MAX_DELAY 350

Servo servo;


int ledPin = 13; // led connected to control pin 13
int PIRSensor = 3; // the PIR sensor will be plugged at digital pin 2
int state = 0; // variable to store the value read from the sensor pin
int statePin = LOW; // variable used to store the last LED status, to toggle the light

void setup() {
  servo.attach(PIN_SERVO);
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(PIRSensor, INPUT); // declare the PIRSensor as as OUTPUT
Serial.begin(9600); // use the serial port
}

void loop() {
state = digitalRead(PIRSensor); // read the sensor and store it in "state"

if (state != 0)
{
    long msec = random(MIN_DELAY,MAX_DELAY);
    int pos = random(0,181);
    servo.write(pos);
    delay(msec);
digitalWrite(ledPin, HIGH);
Serial.println("Motion Detected!"); // send the string "Motion Detected!" back // to the computer
}
else
digitalWrite(ledPin, LOW); // turns light off if motion is not detected
//digitalWrite(
delay(100); // we have to make a delay to avoid overloading the serial port
}

Why are the comments wrong ... (Usual guess: You tried a number of things already). Means I can't use the rest of the comments, as they may be wrong, too.

int PIRSensor = 3; // the PIR sensor will be plugged at digital pin 2
:
pinMode(PIRSensor, INPUT); // declare the PIRSensor as as OUTPUT

OK, to the real question. "The motor needs to run 5 seconds" - the servo will as quickly as it can go to the position you have sent it. Pease explain what you mean by "running for 5 secs" - use 5 seconds to get to that position or wiggle widly for 5 seconds - or some 3rd thing

I am designing a sculpture that will respond (activate motors) to the viewer's presence for five seconds, then stop. Yes, I am combining several sketches. It does run the motor randomly for a moment but not long enough. Thanks.

millis can help you here (compiled, not tested):

void loop() 
{
static unsigned long StartTime=0;
state = digitalRead(PIRSensor); // read the sensor and store it in "state"

if (state != 0)
  {
  digitalWrite(ledPin, HIGH);
  Serial.println("Motion Detected!"); // send the string "Motion Detected!" back // to the computer
  StartTime=millis();
  while(millis()-StartTime < 5000)
    {
    long msec = random(MIN_DELAY,MAX_DELAY);
    int pos = random(0,181);
    servo.write(pos);
    delay(msec);
    }
  }
else
  digitalWrite(ledPin, LOW); // turns light off if motion is not detected
delay(100); // we have to make a delay to avoid overloading the serial port
}

The sketch does compile but now the led/motor runs continually. I have no doubt not included something essential but after playing with it for some time, I can’t resolve the problem.

#include <Servo.h>

#define PIN_SERVO 9
#define MIN_DELAY 130
#define MAX_DELAY 350


Servo servo;


int ledPin = 13; // led connected to control pin 13
int PIRSensor = 3; // the PIR sensor will be plugged at digital pin 2
int state = 0; // variable to store the value read from the sensor pin
int statePin = LOW; // variable used to store the last LED status, to toggle the light

void setup() {
  servo.attach(PIN_SERVO);
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(PIRSensor, INPUT); // declare the PIRSensor as as OUTPUT
Serial.begin(9600); // use the serial port
}

void loop() {

static unsigned long StartTime=0;
state = digitalRead(PIRSensor); // read the sensor and store it in "state"

if (state != 0)
  {
  digitalWrite(ledPin, HIGH);
  Serial.println("Motion Detected!"); // send the string "Motion Detected!" back // to the computer
  StartTime=millis();
  while(millis()-StartTime < 5000)
    {
    long msec = random(MIN_DELAY,MAX_DELAY);
    int pos = random(0,181);
    servo.write(pos);
    delay(msec);
    }
  }
}

After checking the wiring, I discovered the ground of the PIR was not connnected. Your sketch works great! Thanks again.

Keith