Easy way of detecting state change

ccdubs

Member
Hi all,

Apart from using memory variables, is there an easy way (inbuilt function) of determining whether the state of an input pin has toggled. I am mainly using the 08M.

Many thanks
 

westaust55

Moderator
Not sure about detection a state change in either direction simultaneously for a singular pin but the SETINT command (see manual 2 page 149) may help.

SETINT will allow you to see/act upon an input transition from low to high or high to low but only one direction at a time. Each pin’s transition can be set independently so you could look for a low transition on one pin and a high transition on a different pin.

I guess your code could check the current state of the input pins and set the SETINT parameters accordingly to look for a change to the other state.
 

ccdubs

Member
That is a good idea, unfortunately I am already using the setint command for another pin and as I understand it, you can only use the setint command for one interrupt.
 

westaust55

Moderator
Think you may be confusing the comment that only one interupt pattern can be used at a time.

A pattern can set up the states for interupts on several pins.

Have a look at the third example two-thirds of the way down on page 149 of manual 2
 

ccdubs

Member
What I meant that was for the setint command there is only one "interrupt:" subroutine allowed. You can't have a seperate routine depending on what pin caused the interrupt.

So if I have 3 input pins and all have been setup for interrupts, and I recieve an interrupt because one has toggled...how do I know which one it is?

Also interrupts only work on a high or low edge NOT BOTH. So then I wouldn't know when the input toggled high-low if the interrupt wasn't set for this.

Perhaps I should rephrase my intial question into an example. What is the best way for me to code the following?

If pin1 changes state from low to high, flash LED on pin2 once.
If pin1 changes state from high to low, flash LED on pin2 twice.
At the same time I have a limit switch on pin3 set as an interrupt which increments a counter.

Currently I have (untested):

'Initialise
b13 = 0

'Signal enabled
if pin1 = 1 then

if b13 = 0 then
'Pulse the led to indicate enabled
pin2 = 1
pause 500
pin2 = 0
endif

'Set Marker
b13 = 1

'Signal is disabled
if pin3 = 0 then

if b13 = 1 then

'Pulse the LED
pin2 = 1
pause 500
pin2 = 0
pause 500
pin2 = 1
pause 500
pin2 = 0

endif

'Reset marker
b13 = 0
endif

This is written without much thought and I'm sure some improvements can be made using boolean logic.
 

westaust55

Moderator
Okay re-read the SETINT command by candle light (best time to work) and see what is going on with SETINT.

You can check on more than one pin at a time, but an interupt only occurs when ALL of the specified pins have the specified state.
Does not seem that the interupt will watch for a state change on just one of the specified pins.
 
Last edited:

moxhamj

New Member
I have a rather complex program but faced the same problem. One answer is to test the pin regularly. Use let b0=pins. Then mask out the undesired pins with an AND eg b0=b0 and %00100000. Load the previous state of the pin in b1 (I poke/peek to ram as I want to use the registers in other ways. Then store the new value to that ram location ready for the next test.

Finally, with all the 'housekeeping' out the way, do some tests. The values are in b0 and b1, so to test if they are different use xor, eg b2=b0 xor b1. Look up the truth table for the xor function on wikipedia. Actually, look up OR, AND, NOR and NAND as well as these are all useful.

To test if the new value is high and the old value was low, use if b0>b1 then...

To test if the new value is low and the old value was high, use if b1>b0 then...

To test if the values are both the same (ie most of the time), if b0=b1 then...
 

hippy

Technical Support
Staff member
Minimal polling solution avoiding race conditions and complex state handling ...

Code:
bit0 = pin1
Do
  If bit0 <> pin1 Then
    bit0 = bit0 ^ 1
    If bit0 = 1 Then
      Gosub OffToOnTransition
    Else
      Gosub OnToOffTransition
    End If  
  End If
Loop
The 'bit0' variable can be replaced by any other bit, byte or word variable.
 

ccdubs

Member
Thanks to all who responded, my question has been answered and it appears that with PICAXE you have to manually use a variable to perform this function.

The reason I asked is because I have had experience with PLC's that have had functions called : RTRIG, FTRIG, rising/falling trigger. So use would be.

if rtrig = 1 then
flashonce
endif

if ftrig = 1 then
flashtwice
endif

Cheers
 
Top