Running timer together with other running loops.

Lobobo

Member
Hi. I am doing burglar alarm and stuck with last bit, when I come home. My one of PIR's detects me and it must give me 10s for alarm deactivation, but the same time it checks the serial keyboard input and on timeout trigers sirens. Im using Picaxe 18X1. There's copy of my program bit where picaxe reading keyboard input:

symbol key_input = b0 'keyboard input
INTRUDER_CHECK:
key_pos = 0

scan:
KEY_INPUT = 0
kbin KEY_INPUT
pause 170
goto key_pos_check

key_pos_check:
key_pos = key_pos + 1
if key_pos = 1 then
goto test1
elseif key_pos = 2 then
goto test2
elseif key_pos = 3 then
goto test3
elseif key_pos = 4 then
goto test4
ELSE
goto INTRUDER_CHECK
endif

test1:
if KEY_INPUT = b1 then
goto scan
else
goto INTRUDER_CHECK
endif

test2:
if KEY_INPUT = b2 then
goto scan
else
goto INTRUDER_CHECK
endif

test3:
if KEY_INPUT = b3 then
goto scan
else
goto INTRUDER_CHECK
endif

test4:
if KEY_INPUT = b4 then
INTRUDER_STATUS = 0
goto DEACTIVATE_ALARM
else GOTO INTRUDER_CHECK
endif

So if it doesn't go to DEACTIVATE_ALARM within 10s timer, it must start another sub..
Please help me a bit where I should insert or start my (what)timer, keeping in mind that it shouldn't be affected if burglar starts messing with keypad or I myself need to disable siren after more than 10s. Thank you very much.
 

inglewoodpete

Senior Member
First a couple of tips to help get members to read your code.

Have a look at the 'stickys' at the top of the forum topics list. There is an item on how to use the features of the posting editors to better format your code. Ie 'code' tags.

Secondly, use comments to discribe what chucks or lines of code are doing Eg "if KEY_INPUT = b1 then". What is b1? You know but everyone else must delve deeply into the code to understand it.

If you are logged into the forum, you can re edit your original posting.
 

hippy

Ex-Staff (retired)
The KBIN is a 'blocking command'; that is it will stay executing that command until some input is received. While it is doing that it cannot be doing anything else.

There is however a 'timeout' available for KBIN which will allow the program to continue elsewhere if nothing is received within a certain time. This is described in PICAXE Manual 2 - Basic Commands.

I would suggest writing a small test program using KBIN and timeout to get a feel for it before adding it to your main program. This is untested but will light a LED for one second if nothing is typed on the keyboard for 10 seconds, the LED stays off while typing keeps happening -

Code:
MainLoop:
  Do
    KbIn [1000,NoKeyPress], b0
    b1 = 0
  Loop

NoKeyPress: 
  b1 = b1+1
  If b1 < 10 Then MainLoop
  Gosub SetLed
  Goto MainLoop
 

Lobobo

Member
Sorry guys for unclear question. But answer is pretty clear. I see I have trouble in this case. Im using kbin because I couldn't find nice contact keyboard, so I use mini pc serial keyboard instead. As I understood choise one of two, or I must go back to contact keyboard, because timming and "kbin" cannot work together how I want basicaly or I will use two picaxe chips. One for reading kbin and other for timing suppose... Hippy, you are STAR! Thank you.
 
Last edited:
Top