LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Create cycle based on custom values

Solved!
Go to solution

Hello,

 

I've been posting several new topics this weekend.

Not all of them were explained very well and I managed to find a solution for every single one of them myself.

 

I have 1 final question left.

 

I have a variable that switches between 0 and 1. (It toggles between 0 and 1)

For example ... 0 (5secs) 1 (3secs) 0 (4secs) 1 (3secs) 0 (7secs) 1 (2secs) 0

 

I'm looking for a way to count the amount of times a 1 appears.

 

A possible solution is the use of a simple case structure.

If 0 .. nothing happens.

If 1 ... +1

The problem is .. this counter should be located inside a while loop.

If I use this structure my +1 case gets runned so fast (1+1+1+1+1+1+1+1+1+1) that my counter is no longer reliable.

It should only count on a toggle between 0 and 1 .. and not everytime when the while loop is runned.

 

Does anyone have an idea?

0 Kudos
Message 1 of 7
(2,492 Views)
Solution
Accepted by topic author KLI

Use a shift register to record the value of the previous iteration. Update the counter only when the current value is 1 and the previous value is 0.  This detects the transition from 0 to 1.

 

Lynn

0 Kudos
Message 2 of 7
(2,485 Views)

Can you explain this idea more in detail please?

 

If I use shift register on the current while loop the iterations goes so fast ...

This counter should run inside a while loop that is only programmed to stop if you push a stop button.

So should I use a shift register on this loop?

Or create a new loop?

0 Kudos
Message 3 of 7
(2,483 Views)

Please post what you have already tried.  It may be easier to modify that than to introduce a completely new structure or architecture.

 

Lynn

0 Kudos
Message 4 of 7
(2,481 Views)

Thank you very much Lynn,

 

I took another look at your explanation and I understood what you were trying to say.

 

It worked.

 

The only thing left to do is to find a way to reset the counter after it reaches 7.

Right now the iterations keep counting till .... infinity.

Is it possible to reset a shift register inside the while loop?

 

Any idea? 🙂

 

0 Kudos
Message 5 of 7
(2,478 Views)

Use the Greater Than function and the Select function, both on the Comparison palette.

 

It will not really go to infinity. One, you would probably not wait quite that long.  Two, the iteration counter is an I32, os its maximum value is 2^31 - 1.

 

It is considered good practice to put a small delay inside loops so they do not grab all the CPU resources.  Put a Wait function inside the loop.  Even a value of zero will cause the LabVIEW scheduler to allow other nodes to run.

 

Lynn

0 Kudos
Message 6 of 7
(2,475 Views)

Solved !

 

I found a way to reset the shift register.

 

Thank you very much !

0 Kudos
Message 7 of 7
(2,470 Views)