02-25-2017 09:09 AM
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!
Solved! Go to Solution.
02-25-2017 09:30 AM
What exactly do you mean by "number of iterations"? Number of points in your array? Number of cycles (up and down is one cycle)?
02-25-2017 09:36 AM
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
02-25-2017 09:37 AM
So the number of points in an array
02-25-2017 09:46 AM - edited 02-25-2017 09:46 AM
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.
02-25-2017 09:48 AM - edited 02-25-2017 09:50 AM
@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.