Toggle between states


I have finally got my program ready, but now I need some help/ideas how to do the following:

I have six states that are launched by different start switches = startSWITCH1 starts stateONE etc...
this is done by "if" so that switch that gives out gnd will the one to go with.

I would like to have one switch that I can toggle thru these 6 states and then one start switch that starts the state I have choosed. Like one push stateONE, two pushes stateTWO etc.

I also need a led for each state, so I can follow which one is selected.


The state change detection example shows how to determine if a switch has become pressed. That is different from determining that a switch is pressed.

When a switch becomes pressed, increment a state variable, and then act on the value of the state variable. That is, automatically advance to the next state. I can't be clearer than that, since you used such lousy names in your post.

Lighting up the LED corresponding to state one should be no different if the stateONE switch was used to trigger the state vs. the stateNEXT switch being used.


The point is to choose between the start states with one button and then start selected state with another one.

So I would like to have 2 switch button system, instead of either 6 switches or 1 switch and a rotary multiple way switch.

Sorry about my lousy name for topic. English is not my native as I've borned and lived in Finland most of my life.


The problem is one of changing a value when a switch BECOMES pressed, rather than when a switch IS pressed.

What you do with that value is up to you.

The state change detection example shows how to distinguish between IS pressed and BECOMES pressed.

Every time the switch becomes pressed increment the variable that records the state - 0 to 5. Go back to 0 when it exceeds 5. Like this pseudo code.

if (button becomes pressed) {
   myStateVariable ++;
if (myStateVariable > 5) {
   myStateVariable = 0;


I also need a led for each state, so I can follow which one is selected.

6 states could be also be described with 3 LEDs (binary 0 to 6).

I have exactly the same situation. I set up an analog pin with a single pole, double throw toggle switch which is spring-loaded to a center "off" position. The two poles of the switch are tied to ground and 5 v. The analog pin is voltage-divided so that it is at 2.5 volts when the toggle is centered, but receives the gnd or 5 v. level when the toggle is activated. I just read this value each time through the loop, to see if the user is trying to change the state. I'm toggling though about 10 states this way.