09-29-2010 08:33 AM
I have a state machine with a case structure containing about 30 cases. Whenever case #20 is called, what is the best way to repeat cases #20 throught #25 within the case structure 10 times before moving on to another case number?
Solved! Go to Solution.
09-29-2010 08:40 AM
There are a probably a number of "best"! Is this a "fixed" thing, or do you need the flexibility to repeat other sections at other times? Is it going to be the same number of repeats? How do you signal the transition from one state to the next? You could have a selector there that determines whether to go back to the previous (i.e 20) or on to the next (i.e.26 or whatever is outside of the "block" being repeated). Then keep track of the repeat count, with a shift register, incremented each time through the block's loop, reset to zero when the count has reached a predetermined value. This shift register could even be encapsulated to allow the target value to be "set" in another section of your program, "incremented" each time through, and "checked" where you need to know whether the incremented number = target.
Many other ways possible too, just a quicky off the top of my head.
09-29-2010 08:41 AM - edited 09-29-2010 08:43 AM
Once you hit case 20, initialize a counter at 0, feed it to a shift register.
1) move your cases to:
20 >> 21 >> 22 >> 23 >>24 >>25
2) at case 25:
If counter <= 10:
counter += 1
25 >> 20
else:
25 >> 26
That should be relatively straight forward.
Edit: LV_Pro beat me to it! I guess thats why he's a pro
09-29-2010 08:56 AM
Yes it is fixed, and I use an enum to go between states, as in the LV state machine template. I thought about using a shift register, I just wasn't sure how to set up the counter for just five of the cases, but I think I can get there by combining the cases into one case and using a for loop.
Thank you.
09-29-2010 04:01 PM
Put the shift register outside of the case structure that changes states.
Set it to 1 initially.
Through every single case, pass it straight through without modifying it.
The only case that might modify the value is case 25.
If the value is 9 or less, move back to case 20 and increment the counter by 1.
If the value is 10, move to case 26, and reset the counter back to 1.