Russell Gillie wrote:I can't for the life of me figure out the following problem:
Red led is turned on/ off by button 1.
Only when button one is "on", will control go to button two which turns yellow led on/off.
Only when button two is "on", will control be allowed for button 3 to turn green led on/off.
Only when green is off will yellow be allowed to turn off/on.
Only when yellow is off will red be allowed to turn off/on.
My normal structure is that the loop() function has a state switch statement in it (and I use enums as states). When I want to change state, I call a newState() function, passing the next desired state as its argument. The newState() function is where I usually call action functions relating to leaving the old state and entering the new one. So actually, a green-to-yellow operation in newState would see that the new state was no longer green, but since the prior state was green, it would turn off the green light. I'd then set the current state to the new state value, and a switch statement for the new state would turn on the yellow light.
So in total, I'd have 3 switch statements. One in the main loop, where I monitor for events and, depending on what state I'm currently in, possible switch to a new state. Then in the newState method, there'd be a switch to handle leaving the old event, a statement changing the current state value, and a switch to fire actions related to the new state.