From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

Count up then down

I am trying to generate an array that counts up to 5 then back down to 1 then back up to 5. (1,2,3,4,5,4,3,2,1,2,3,4...)

I want it to be in a for loop to allow for the number of iterations to be set. I can make it work in a while loop, but I am not sure how to make it work in a for loop so the number of iterations can be set.

Message 1 of 9
(4,870 Views)

Re: Count up then down

What exactly do you mean by "number of iterations"? Number of points in your array?  Number of cycles (up and down is one cycle)?

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 2 of 9
(4,853 Views)

Re: Count up then down

Basically, I need the user to input a number so for example 7 would give an array of 1,2,3,4,5,4,3 - so a for loop would be needed

Message 3 of 9
(4,849 Views)

Re: Count up then down

So the number of points in an array

Message 4 of 9
(4,848 Views)
Solution
Accepted by topic author arem938

Re: Count up then down

Use a shift register to keep track of if you are counting up or down.  Then use a case structure to perform whichever task is needed.

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 5 of 9
(4,828 Views)

Re: Count up then down

@arem938 wrote:

I am trying to generate an array that counts up to 5 then back down to 1 then back up to 5. (1,2,3,4,5,4,3,2,1,2,3,4...)

I want it to be in a for loop to allow for the number of iterations to be set. I can make it work in a while loop, but I am not sure how to make it work in a for loop so the number of iterations can be set.

Actually, your For Loop iteration is much closer to being correct than the while loop iteration.  The while loop version is so convoluted, I don't believe it is actually working for you.

Here is the secret.  Put in another shift register that is a boolean.  That holds the state whether you are counting up (True) or counting down (False) and drives your case structure, not your less than 5 that you have now.  Now put in one more comparison that if your value equals 5, then you put a False into the shift register.  If it equals 0, put in a True.  If it is anything else, just feed in the wire from the left shift register terminal since you don't want to change the counting direction.

Since you are counting, I would change your constants and indicators to integers rather than floating point.

Message 6 of 9
(4,826 Views)
Solution
Accepted by topic author arem938

Re: Count up then down

Here's what I would do. (See if you can code it. Watch for the limit inclusion setting, i.e. the little diamonds).

(Having all code visible (e.g. no case structures) makes it easier to maintain).

(A full implementation would also do some sanity checks on the inputs, e.g. to make sure that the upper limit is larger than the lower limit, etc. So modify as needed.)

Message 7 of 9
(4,780 Views)

Re: Count up then down

Awesome - thanks so much for your help!

Message 8 of 9
(4,739 Views)

Re: Count up then down

Thanks -  I really like this method - really appreciate your help!

Message 9 of 9
(4,737 Views)