Fine tune servo help?

tmack

Member
I have a program on an 18A that activates a servo when anyone comes into my work room:) The problem is that it activates the servo too many times (8xish)I would like it to only function 2X each time the PIR is triggered no more. the PIR is on pino, leds are on out 0,2, the servo is on out 3. The code I have now is:

Main: if pin0 = 1 then turn_on
pause 1000
high 0
nap 4
low 0
goto main
turn_on:
high 2
servo 3,80
pause 250
servo 3,180
pause 200
goto turn_off
turn_off:
low 2
pause 50
goto main

Thanks for any help.
 

Wrenow

Senior Member
Just a guess, but it looks like the PIR is giving you a longer trigger than you think. Many stay on for one second after seeing activity, but also stay on for the whole time the activity is going on.

You might want the PIR to set a flag routine that stays on for a while. Or set the pause longer?

Cheers,

Wreno
 

tmack

Member
It is actually staying on too long. I want it to only servo 2x not the 8 that it is currently doing. I dont think I can make the pir stay on less time.
 

hippy

Technical Support
Staff member
What Wrenow is suggesting is that you monitor the PIR, move your servo and then check the PIR to make sure its signal has gone away before looking for its next activation.

At the moment your code runs all the time the PIR signal is active, which it probably is when you complete your two moves, so it moves some more. Simply adding "Do : Loop While Pin0 = 1" before your last "got main" will probably solve your problem.
 

moxhamj

New Member
There is a software solution for this. In general terms if you only want to move 2x then set up a register as a counter and every time you move then add one to the counter. If it starts at 1 and is now >2 then don't move. Then set up another counter that ticks say once a second and starts off at (say)10 and counts down to 0. Set it to 10 when you move the servo. Count down to 0. If it is <0 then make it 0 and also reset the 2x counter to 0 as well. Only enable the servo move when this counter is =0. So now it will wait another 10 seconds before moving again.

Sometimes it is easier to write down the rules on a piece of paper before starting to write the code.
 
Top