Code block not executing on PIR sensor from Robotshop

The following code is supposed to execute a series of servo commands when motion is detected. Motion detection seems to be working fine, when tested in a stub program. Servo control works fine when tested with the sample sweep program.

The problem is that I am getting random behavior when putting it all together. I have powered the Uno, servo and motion detector both from the USB and wall adapter thinking it may be a power issue, No change nor any indication that the Uno is RESETTING.

When motion is detected, the eyes open then close. No blinking occurs.

The code is listed below (some of the strange constructs were me testing different structures):

[quote]

[color=#7E7E7E]// Sketch to blink prop eyes when someone approaches.[/color]
[color=#7E7E7E]// Behavior can be modified depending on how long they stay[/color]
[color=#7E7E7E]// Test version blinks LED[/color]

#include <[color=#CC6600]Servo[/color].h> 

[color=#CC6600]void[/color] Open_Eyes();
[color=#CC6600]void[/color] Close_Eyes();
[color=#CC6600]void[/color] Blink_Eyes();

[color=#CC6600]Servo[/color] myservo;  [color=#7E7E7E]// create servo object to control a servo [/color]
 
[color=#CC6600]int[/color] motionPin = 2;  [color=#7E7E7E]// analog pin used to connect the motion detector[/color]
[color=#CC6600]int[/color] motionVal;      [color=#7E7E7E]// variable to read the value from the analog pin [/color]

[color=#7E7E7E]// Servo is attached on pin 9[/color]
[color=#CC6600]int[/color] ServoPin = 9;

[color=#7E7E7E]// Pin 13 has an LED connected on most Arduino boards.[/color]
[color=#7E7E7E]// give it a name:[/color]
[color=#CC6600]int[/color] led = 13;

[color=#7E7E7E]// Flag for previous state[/color]
[color=#CC6600]boolean[/color] wasHIGH = [color=#CC6600]false[/color];


[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);

  [color=#7E7E7E]// initialize the servo on 'ServoPin'[/color]
  myservo.[color=#CC6600]attach[/color](ServoPin);  [color=#7E7E7E]// attaches the servo on defined pin to the servo object [/color]

  [color=#7E7E7E]// initialize the digital pin as an output.[/color]
  [color=#CC6600]pinMode[/color](led, [color=#006699]OUTPUT[/color]); 
  [color=#CC6600]digitalWrite[/color](led, [color=#006699]LOW[/color]);    [color=#7E7E7E]// turn the LED off by making the voltage LOW[/color]

  [color=#7E7E7E]// Allow the motion detector to warm up.[/color]
  [color=#CC6600]delay[/color](20000);
  
  [color=#7E7E7E]// Start with eyes closed[/color]
  Close_Eyes();

}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {

  motionVal=[color=#CC6600]digitalRead[/color](motionPin);
  
  [color=#CC6600]if[/color] (motionVal == 1) {
    Open_Eyes();
    [color=#CC6600]delay[/color](2000);
    Blink_Eyes();
    Blink_Eyes();
    [color=#CC6600]delay[/color](3000);
    Blink_Eyes();
    [color=#CC6600]delay[/color](60000);
    wasHIGH=[color=#CC6600]true[/color];
    [color=#CC6600]digitalWrite[/color](led,[color=#006699]HIGH[/color]);
  }
  
  [color=#CC6600]if[/color] (motionVal == 0) {
    [color=#CC6600]if[/color] (wasHIGH) {
      Close_Eyes();
      wasHIGH = [color=#CC6600]false[/color];
    }
    [color=#CC6600]digitalWrite[/color](led,[color=#006699]LOW[/color]);
  }
   
  [color=#CC6600]delay[/color](200);
}

[color=#CC6600]void[/color] Close_Eyes() {
  myservo.[color=#CC6600]write[/color](0);
}

[color=#CC6600]void[/color] Open_Eyes() {
  myservo.[color=#CC6600]write[/color](180);
}

[color=#CC6600]void[/color] Blink_Eyes() {
  Close_Eyes();
  [color=#CC6600]delay[/color](30);
  Open_Eyes;
  [color=#CC6600]delay[/color](30);
}

[/quote]

Try using longer delays in the blink routine. Also, the servo likely needs a separate power supply - Sweep may be working, but it's probably damaging the arduino.

I got it to work by stepping through each servo position in increments of 2, with a for loop in the Open_Eyes and Close_Eyes routines. This technique does result in a longer delay as you suggest. There are 78 steps, (178/2), and a delay of 3ms between each step for a total delay of 234ms; I had 30ms.

I think a delay as low as 1ms works; thus by adjusting the increments and delay, I can adjust the speed and movement of the servo.

void Close_Eyes() {
for(int pos = 179; pos>=1; pos-=2){
myservo.write(pos);
delay(3);
}
}

What do you mean by damaging the Arduino? There is only one micro-servo and its been running 24 hours.

Thanks for your comment. It helps me to understand using servos. By the way, the project was at the request of an artist who had seen something else I had built. He wanted a set of blinking eyes whenever his sculpture of a rock detected movement. You can see a video of the sequence running here Rock Eyes

What do you mean by damaging the Arduino?

Check the stall current for your servo (look it up or measure it). If it's more than 200mA, it's exceeding the recommended current draw for the Arduino. It may not immediately show any ill effects, but it would be reducing its expected life.

Measuring about 125ma under load, so I think I should be safe. This servo is vastly overpowered in torque for the application. The load is very light and has no major stresses on it. (It's the eyelids in the vid; they're two 1/2s of a plastic Easter Egg)

Hopefully, I've got it right.