LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Making a counter with enable and reset

Hi.  

First of all, I looked online for this solultion and no success.

 

What I am trying to do is have a counter with enable and reset.  

 

1.  Increment from 0 to 5 everytime true boolean is received.  Q, the output should then be 1

2.  If boolean false is received, Q should be 0

3.  Once the count reaches 5, reset the counter to 0 and repeat.

4.  Step 1-3 only happens when enable is true.  Anytime enable is false, the process is stopped.

 

Please help!

 

Rose

0 Kudos
Message 1 of 16
(7,923 Views)

Hi Rose,

 

some pseudocode may help:

IF enable THEN
  IF boolean THEN
    Q := 1
    counter := (counter +1) MOD 5
  ELSE
    Q := 0
  ENDIF
ENDIF

You need two case structures, a feedback node and a Quotient&Remainder function.

 

You didn't define your output Q when ENABLE is FALSE…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 16
(7,909 Views)

This is something called an Action Engine, closely related to "Functional Global Variables" (FGV) or "VI Globals" (VIG).  Basically, you have a While Loop with one or more Shift Registers acting as "memory" and an "Action" input (typically a Boolean or an Enum) that says what the Action should be.

Simple Action Engine.png

 

As you posed your problem, I'm unsure if you require one Boolean (Enable/Disable) or two (Enable/Disable and "Count").  This is a situation where having an Enum input and more "Actions" might be appropriate.  For example:

Load      Save the Max Count (5 in your example) in a Shift Register

Reset    Set the internal Count to 0

Enable   Allow Count Action to proceed

Disable  Don't allow Count Action to proceed

Count    Increment counter if "enabled", and output current count

 

Each time you call this VI (did I say that this should be written as a sub-VI, with inputs Action and Max Count, and output Current Count?), depending on the Action you wire in, you will either perform "setup" actions (Load, Reset, Enable, Disable) or "Counting" actions (Count).  Note that it is easy to modify it so that if the Counter is disabled (ooh, that's a good name for this sub-VI, "Counter", has a mnemonic ring to it), the output is, say, -1, "Not a Count", instead of (as I described it above) the last Count while Enabled.

 

Action Engines (and similar constructs, where there's a While loop with "True" wired to the Stop terminal, useful as a "memory" unit) are pretty common in LabVIEW.  More accomplished LabVIEW users might also accomplish this with a Feedback loop, but I find that the "Do Once" loop shown above is easier for me to recognize and understand.

 

Bob (hard to teach an old dog new tricks) Schor

Message 3 of 16
(7,894 Views)

I think this diagram would meet your requirements if I am reading them correctly.

 

Gerd,

 

I think this works with only one case structure, am I wrong?

 

diagram3.png

Message 4 of 16
(7,886 Views)

Hi griff,

 

you can replace your comparison and select functions by QR as suggested…

 

I just gave that simple pseudocode based on the OP's problem description. From that is was easiest to use two case structures as (atleast for me) the problem isn't defined very well for all possible input cases…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 5 of 16
(7,879 Views)

griff32 wrote:

I think this works with only one case structure, am I wrong?


You can get away with just 1 case structure.  But not exactly the way you did it.

The False case just passes the count straight through.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 6 of 16
(7,865 Views)

HI Gerd,

 

Thanks I put that replacement in and it worked.

Message 7 of 16
(7,856 Views)

 

For fun, I decided to implement it using flip flop representations, since it's a common ring counter design:

 

Ring_Counter_Snippet.png

 

There are 6 unique combinations of Q0, Q1, and Q2 that will show up, which can be correlated to 0, 1, 2, 3, 4, and 5.

 

Edit: "Counter Enabled" is the Q output mentioned in the original post.



Message 8 of 16
(7,820 Views)
I've decided to take your recommendation and tried it on my code. However, when I press enable, I get the multiple counts meaning one click registers multiple actions. So, I put the delay in the loop but it's inconsistent. Also, it seems like output q is rapidly switching between true and false disregarding the count. Help!
0 Kudos
Message 9 of 16
(7,757 Views)
I've decided to take your recommendation and tried it on my code. However, when I press enable, I get the multiple counts meaning one click registers multiple actions. So, I put the delay in the loop but it's inconsistent. Also, it seems like output q is rapidly switching between true and false disregarding the count. Help!
0 Kudos
Message 10 of 16
(7,756 Views)