LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Count up then down

Solved!
Go to solution

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.

 

Thanks for your help!

0 Kudos
Message 1 of 9
(5,180 Views)

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


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
0 Kudos
Message 2 of 9
(5,163 Views)

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

0 Kudos
Message 3 of 9
(5,159 Views)

So the number of points in an array

0 Kudos
Message 4 of 9
(5,158 Views)
Solution
Accepted by topic author arem938

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.


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
0 Kudos
Message 5 of 9
(5,138 Views)

@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.

0 Kudos
Message 6 of 9
(5,136 Views)
Solution
Accepted by topic author arem938

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
(5,090 Views)

Awesome - thanks so much for your help!

0 Kudos
Message 8 of 9
(5,049 Views)

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

0 Kudos
Message 9 of 9
(5,047 Views)