Posted 24 Feb 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. Share this post Link to post Share on other sites
Posted 24 Feb 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? Share this post Link to post Share on other sites
Posted 25 Feb 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. Share this post Link to post Share on other sites
Posted 26 Feb 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. Share this post Link to post Share on other sites
Posted 26 Feb 2015 Yes, if you want to skip the FOR-NEXT, simply create a pointer around it, and enable the jump when needed. Share this post Link to post Share on other sites
Posted 27 Feb 2015 This instruction, together with index registers, makes for very exciting coding possibilities. Share this post Link to post Share on other sites