Continually test an input?

k_shirtcliffe

New Member
I'm new to picaxe and I'm just having a play around on a breadboard to get the hang of things but I have a question to which I am sure there is a very obvious answer!

I would like to know how to monitor an input continually - so I have attempted to create a simple program that will do the following:

Make an LED flash on and off when a switch is pressed. If the switch is pressed a second time it will switch the LED off and revert back to the beginning and start waiting for the switch to be pressed again...repeat.

The code I have attempted is as follows:

<code>
symbol pswitch=pin3
symbol led=4
symbol pressed=1
symbol notpressed=0

main:
if pswitch=pressed then goto flash
if pswitch=notpressed then goto main

flash:
high 4
wait 1
low 4
wait 1
if pswitch=pressed then goto ledoff
goto flash

ledoff:
low 4
goto main
</code>

My problem is that, in the flash loop, the program doesn't listen for an input until it has done the flash cycle. This is what I mean by "continually test an input" I would like the program to act upon a switch press no matter what point through the flash cycle the program has reached.

Also pressing the switch a second time doesn't actually acheive anything!

Thanks for your patience with what I'm sure is a stupid question!
Cheers,
Kev
 

BeanieBots

Moderator
The best way is to use interrupts.
Rather than try to explain it here, have a read of the section in the manual.
Basically, you set it up such that a specified input pattern will trigger a special subroutine to run as soon as the condition is met.
Your "interrupt" subroutine can then set up conditions required for your program to take into account the button press.

The only alternative is to break the waits down into smaller time slices and test in each slice.
 

steliosm

Senior Member
Try looking the 'Interrupt' command in the manual.
You can create an interrupt event every time you press the button you specify.
Mind you that you will need to rename the flash routing to interrupt in order for this command to work.
 

BeanieBots

Moderator
The command is actually called "setint".
It is used to set the input conditions which will direct program control to a special subroutine that must be named "interrupt".
 
Top