Ron_S Posted February 24, 2015 Report Posted February 24, 2015 Can someone give me a practical use of the instruction for/next. When things are slow I always try to advance my knowledge of programming instructions. I have been using it in practice programs and can't make any sense of it's use. I see it goes through its number of predetermined numbers each scan but it then scans the rest of the program and starts again without any control to stop it. Has anyone actually used it - and how. Quote
kaare_t Posted February 24, 2015 Report Posted February 24, 2015 There are many uses for this, like searching through arrays and doing math. I've created a small example showing how to loop through an array of words, looking for the number "5". This isn't very useful, but maybe it can give you some clues? I would say that for normal machine control logic there isn't much use of FOR-NEXT since machine control relies on sequences. FOR-NEXT is more of a way to crunch numbers and to avoid creating a whole sequence just to work around the whole SCAN problem. I can't remember any "simple" real-life examples right now, but what kind of programming do you normally do? Quote
Crossbow Posted February 25, 2015 Report Posted February 25, 2015 For Next creates a repeating loop of code. The code inside will repeat the specified number of times. I used it once to perform 61 compares. Instead of writing them all, I wrote one in a loop and incremented a pointer. Just be careful, because it runs that number of loops every single PLC scan. So it can be a great way to extend a PLC scan time and crash on a watchdog timer if used wrong. Quote
Ron_S Posted February 26, 2015 Author Report Posted February 26, 2015 Right thanks. I can see the scan time would increase and maybe fault out if you didn't extend the watchdog timer The bit I don't like is that it executes every scan - so maybe if I use it, I could put the instruction on a pointer and use it when needed - maybe? Thanks for the examples. Quote
kaare_t Posted February 26, 2015 Report Posted February 26, 2015 Yes, if you want to skip the FOR-NEXT, simply create a pointer around it, and enable the jump when needed. Quote
waynes Posted February 27, 2015 Report Posted February 27, 2015 This instruction, together with index registers, makes for very exciting coding possibilities. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.