PDA

View Full Version : Sensor and servo



bboy8012
05-17-2010, 01:19 AM
I'm using a PIR sensor and servo, my problem is when I load the prop it everything is fine, but when it first detects motion it does what its supposed to, but after the servo keeps repeating the servo ramp.

PUB detectAnimal | smPos
'servo.Set(pServo, 1500) 'Setting servo to center postion

'repeat ' Repeat
if ina[pirSen] == 1 ' If PIN1 equals a 1 (PIR is triggered)
!outa[detLed] ' Toggle PIN16 (LED on)
waitcnt(clkfreq/10 + cnt) ' Wait a 10th of a second
!outa[detLed]
repeat 2
repeat smPos from 1200 to 1700 step 20
servo.Set(pServo, smPos) 'Move servo to servoPos postion
'pwm.Duty(pump,50,16665) 'Create a 60Hz 50% duty cycle
waitcnt(clkfreq/1000 * 20 + cnt) '20ms synchronized delay
repeat smPos from 1700 to 1200 step 20
servo.Set(pServo, smPos) 'Move servo to servoPos postion
'pwm.Duty(pump,50,16665) 'Create a 60Hz 50% duty cycle
waitcnt(clkfreq/1000 * 20 + cnt) '20ms synchronized delay

I have testeted the servo ramping code by itself and it works fine, with the finite loop of 2, its when I add the PIR sensor it gets all whacky.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

T Chap
05-17-2010, 02:58 AM
My experience with the PIR is that is stays on a little longer than I would like it to after it detects motion, so you may be getting the PIR staying high when the loop rolls around again. You may want to try first setting a dedicated LED to display the PIR input, just run another cog watching the PIR Pin and changing an LED state. You have to see what the PIR is actually doing to trace out the problem. Try putting a line at the bottom that traps the code there until the PIR resets to 0. Like, repeat while INA[PIRPIN} ==1 That way the code does not repeat if the PIR is STILL high from the initial trigger, the PIR must toggle back off to end the cycle.

The other thing of interest is, if you have commented out the first repeat, then how is the cog staying alive at all after it hits bottom of the repeat2? You must loop the entire code to keep things running, else the servo outputs are floating around.

Post Edited (Todd Chapman) : 5/16/2010 7:15:11 PM GMT