Challenging programming loop - well for me...

Hi all,

I’ve been trying to work out this afternoon how to solve this problem.

I have a Nano using Nick Gammon’s switch manager library (which is awesome) managing inputs from four switches.

SW1 determines how many participants take part in something and determines how many items they paid for (possibly optional - may actually be redundant) eg SW 1 pressed 4 times equals four participants and 40 items for a total of 160.
SW2 Start/Stop - sets a variable either 1 or 0
SW3 is repeat - move back one step (could be difficult)
SW4 releases the item they paid for

I want to set up a loop that repeats as per the following flow chart. I’ve had a few different variations partially running today but just can’t seem to put it all together. Head’s a bit unclear on advantages of do - while, while, if else. I’ve tried combinations but can’t hit on the right one. Any help appreciated.



My advice would be to take a look at the forum sticky Useful links - check here for reference posts / tutorials and pay particular attention to the topics on state machines.

Image from Original Post so we don't have to download it. See this Simple Image Guide


I agree with the suggestion to study the concept of "state machine", It is just a rather grand name for using variables to keep track of where you are.

I suspect that your flow chart is not appropriate. in the sense that the sections do not, or need not follow one after the other. ie. all the button1 stuff does not need to be completed before there is button2 stuff. Using a state machine concept will allow the effects of the different user-inputs to be taken into account regardless of the order in which they happen.

It is common for us humans to think of a series of actions in a linear fashion even though, in practice, we often don't do them that way. Getting your head around the idea of asynchronous code can be a little tricky to start with but it will make the whole job much easier. Maybe have a look at how the code is organized in Planning and Implementing a Program


Thanks guys. Will take a look at both of those.

I've come back to coding after a very very long time away.