12-09-2019 09:12 AM
Good day community, I have faced some issues regarding one-time triggers of a boolean.
Because some times I need to trigger an event only once when a state of a boolean changes, so I have made a piece of code that does that, the code is simple, and what it does is when ever the input is a true value, it will set the output to true for 1 frame, then it will set it to false, until the input is set to false (reset) and to true again for an other 1 frame pulse.
I made this to avoid using shift registers, but I also heard that is not a good idea to use feedback inside the code.
So I wonder if there is a better method to achieve this goal, thank you
12-09-2019 11:42 AM
I think that, technically, using a feedback node is a tiny bit more expensive in terms of CPU usage, but there's nothing inherently wrong with using them. In fact, sometimes there is no practical way to avoid them.
12-09-2019 02:09 PM - edited 12-09-2019 02:10 PM
I think the feedback node is fine. I do question the need for the other logic. It looks Rube-Goldberg to me. Why not just AND the current Input and the inverse of the previous Input? Does that not accomplish the stated goal? If Input is TRUE AND previous value of Input is FALSE then Output is TRUE. Otherwise Output is FALSE.
12-09-2019 02:54 PM
@johntrich1971 wrote:
This is the "rising edge" detector. What caught my attention is that the above is the negation of what is probably the most "unused" Function on the Boolean palette, namely "Implies". Here is the same function, with explanation:
In Symbolic Logic, an Implies Statement (A Implies B) is True except when A is True and B is False (in other words, with a False premise, you can imply anything, but with a True premise, you can truthfully imply only something True). Here the Premise (the previous condition) is the Feedback loop, so if the Input is True and the previous input was False (precisely the condition we want to be True), this is exactly the condition where the Implies function returns False, so we negate it and we get "Rising Edge".
Isn't logic fun?
Bob "Makes Your Head Spin" Schor
12-09-2019 03:37 PM
Yeah, I used to get a kick out of using the "Implies" function when it fit, probably mainly *because* of its relative obscurity.
IIRC, I changed my habit a decade or more ago after reading a thread here where altenbach made an offhand comment about the excellent readability of a multi-input AND with a negated input, just like johntrich just posted. He was right (of course).
That's one of several areas where I've learned that code clarity is usually more valuable than cleverness and often more valuable than small improvements in efficiency.
-Kevin P
12-09-2019 04:36 PM
@johntrich1971 wrote:
I think the feedback node is fine. I do question the need for the other logic. It looks Rube-Goldberg to me. Why not just AND the current Input and the inverse of the previous Input? Does that not accomplish the stated goal? If Input is TRUE AND previous value of Input is FALSE then Output is TRUE. Otherwise Output is FALSE.
If I am understanding the OP correctly, they only want a single trigger just once. The rising edge will trigger whenever there is a rising edge. I believe that is the reason for the R-G code, which may or may not, be able to be simplified.
mcduff
12-10-2019 06:59 AM
@mcduff wrote:
If I am understanding the OP correctly, they only want a single trigger just once. The rising edge will trigger whenever there is a rising edge. I believe that is the reason for the R-G code, which may or may not, be able to be simplified.
mcduff
You may be right, mcduff. In that case adding a simple OR would do the work.
I've not really used the Imply that Bob_Schor suggested, but I'm not sure that I would want to as I prefer that my code be obvious to someone who may have to edit it in the future. As Kevin_Price suggested the Imply is less readable because it is less well known.
12-10-2019 07:29 AM
I am in complete agreement that Imply fills (as we used to say) "a much-needed Gap in the list of Boolean Functions"! Indeed, when I first noticed Imply, I had to remind myself of the syllogisms I worked through in PL10 (Symbolic Logic) many, many moons ago. It is great for logical proofs, and I recognized its strange pattern of 3 Trues and one False, and puzzled when I ran into the first time I needed a "Rising Edge" function that was three Falses and one True, and said "Is this Implies-Not?", but after drawing truth tables for a few minutes and trying to puzzle it out, I realized that it was too weird to be used anywhere except "buried" inside a sub-VI (where you could write a little Test Routine and say "Yes, this only detect Rising Edges" and not worry about the obscure details ...).
Bob Schor