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]