Capture the edge of a state change


#1

I develop applications where I need to capture the edge of a state change. For example, when a discrete point changes from low to high. Currently I use a variable, which I call LastState. I do a comparison between LastState and the current value, if they are different I update the LastState variable to the current state and perform the required reaction.

if ((InputPoint=1) and (LastState=0)) Then
LastState=1;
Perform whatever reaction should happen at the on edge
elseif ((InputPoint=0) and (LastState=1)) Then
LastState=0;
Perform whatever reaction should happen at the off edge
endif

Is there an easier way?


#2

something called Input On-Latch, Input Off-Latch, maybe useful


#3

Hi KJDingwall,

What leeveney mentions, one of our many built-in I/O features, is one of several “best kept secrets” of our I/O, so I’m glad you brought it up!

For sure you’ll want to check out the commands in the Digital Point section of the command reference, like the Get On-Latch leeveney alludes to. Ben would probably go a step further and suggest you print this quick-reference (it once fit on a page–we’ve added a lot more commands since those days!) to study and memorize/wallpaper favorite room. But that might be a bit extreme. :wink:


If you inspect your Ethernet I/O (PAC or older brains too) directly via PAC Manager, you’ll see these digital points features right there at the rack. The PAC Control commands simply read these values on the brain.


This distribution of intelligence frees up your controller from having to keep track of these things constantly (or worry about missing something in your network goes down, etc.). And the “brain” on the rack won’t miss anything (like a quick on/off) that your controller logic might.

Also built-in on the analog side: min/max values w/no programming needed.

Happy latch reading!

-OptoMary


#4

Yep, what those that go before me said…

The only other thing I want to add is that you will probably want to use the command ‘Get and clear on latch’.
If you just get it, it still stays set, handy if you want to do something before you clear it and you don’t care if another edge transition occurs.
Also don’t forget that you can get (and clear) off latches as well.

Really handy stuff.

Ben


#5

I think it’s worth mentioning that using a GetOnLatch or GetOffLatch will get you much faster responses from your inputs too. We were recently playing around with using an Opto controller in a basic lighting system that utilized momentary switches as inputs. When we simply queried the current state of the momentary input, the quick signal was often missed even with a fairly quick monitoring loop. To get around this, we rather used a GetOnLatch command and the response time was almost immediate.

I won’t get into why this is faster as it is explained quite well in the User Guide and Ben and/or Mary could do a better job explaining it than I. It’s definitely worth keeping in the back of your mind though!