PDA

View Full Version : Time limit while waiting for input ?



Sam
- 3rd July 2017, 00:13
I'm using a 16F628A to control a DC motor, also using hall effect sensors as limit switches. All working fine however I'm concerned that if one of the hall sensors should fail the motor will keep running, NOT GOOD !

The code waits for a low from the hall sensor, is it possible to have a time limit on how long to wait for that low input and if it doesn't happen, the timer will effectively provide the low and the program can continue just as if the sensor worked ?

For example, the sensor should go low at about 16 seconds, At 18 seconds the timer could provide the low if the sensor didn't. (?)

Thanks for advice !

richard
- 3rd July 2017, 02:31
is it possible to have a time limit on how long to wait for that low input and if it doesn't happen, the timer will effectively provide the low and the program can continue just as if the sensor worked ?


the code I posted in this link

http://www.picbasic.co.uk/forum/showthread.php?t=21066&p=136604&highlight=#post136604

effectively does this. that's what "run_count" is all about

when the motor is moving the limit switches are checked every so many mS and the run count decremented
if a limit is reached or the run count reaches 0 then movement is terminated.

a run count of 2000 @ 50mS = 100 seconds till timeout
or
a run count of 185 @ 100mS = 18.5 seconds till timeout , simply adjust to suit

Sam
- 4th July 2017, 16:46
Hi Richard,

I've been studying your code, (again) I haven't tried to compile the items added in red below as I'm on a different PC right now. I just posted a small part of it for learning.

Can you tell me if I'm on the right track with how I've added it ?

So after 100 seconds, if the limit hasn't sent a low, limup pin will still go low ?

Thanks !



maxrun con 2000 ; 2000 = 100 sec
RUN_COUNT VAR word


moveup:
high blue
pause 500
low blue
while limup = 1
high up
wend
IF RUN_COUNT THEN ;should the motion continue ?
RUN_COUNT = RUN_COUNT -1
ENDIF
if limup = 0 then
low up
goto run
else
goto moveup
endif

richard
- 4th July 2017, 23:31
hi sam

I will make some assumptions here
blue is a led that should flash when moving
up enables movement
limup stops movement when low


this is the movement loop / the only way it stops is if limup = 0

while limup = 1
high up
wend
its not what you want

try some thing like this


moveup:
high blue ; led on
high up ; motor on
RUN_COUNT=200 ; set time limit 200*500mS ie 100 sec
while (limup = 1 ) && ( RUN_COUNT>0) ; do this till time runs out or limit sw trippe
pause 500
toggle blue ; flash led
RUN_COUNT = RUN_COUNT -1
wend ; all done
low blue
low up

Sam
- 9th July 2017, 19:58
Richard, you're awesome ! thanks very much !

This morning is the first chance I've had to try it without constant interruption. I must have done something a little different because it wouldn't work until I added...

moveup:
high blue ; led on
high up ; motor on
RUN_COUNT=200 ; set time limit 200*500mS ie 100 sec
while (limup = 1 ) && ( RUN_COUNT>0) ; do this till time runs out or limit sw trippe
pause 500
toggle blue ; flash led
RUN_COUNT = RUN_COUNT -1
wend ; all done
IF limup = 0 OR RUN_COUNT =>0 THEN
low blue
low up
ENDIF

Not sure why it doesn't work right without the ">0" but in any case, it's working great. Thanks again !!

richard
- 10th July 2017, 00:59
I have some doubts about your modification, perhaps you have some noise entering the system, or the limit switch is "bouncing"


IF limup = 0 OR RUN_COUNT =>0 THEN
low blue
low up
ENDIF


firstly operator precedence make this If test dodgy, parentheses ensure correct result
IF (limup = 0 ) OR (RUN_COUNT =>0) THEN
secondly

RUN_COUNT =>0
since pbp vars are unsigned , this will always be true it can only be 0 or greater
since the run_count test always tests true the if statement is always true

thirdly
the while loop will only exit if limup is 0 or run_count is 0 , the statement is or should be redundant