boolean function

Hi, How can I set up an boolean function that check if an on/off button have been switched. And this boolean function I want to use in several different control structures without doing the same check over and over again?

The purpose is to use this on/off boolean switch on a main function. After that I have a switch with 5 different modes where I also want to use this on/off switch without focus on HIGH or LOW only focus on if it has changed from HIGH to LOW or the other way around?

//mainswitch if(mainSwitchPin == 0 && onoff ==???? ){....................}

//switch1 if(mainSwitchPin == 1 && onoff ==??? ){} //switch2 if(mainSwitchPin == 2 && onoff ==???? ){} //switch3 if(mainSwitchPin == 3 && onoff ==????? ){} //switch4 if(mainSwitchPin == 4 && onoff ==????? ){}

Best regards amilo

Have a look at the StateChangeDetection example in the IDE.

Incidentally, how are you deriving the value for mainSwitchPin ? Digital pins return HIGH or LOW and analogue pins return a value from 0 to 1023.

Ok, but I wonder how to put together a boolean function that is small and simple that solves my problem. The mainSwitchPin is not the problem it's the onoff boolean that I want to use at different places with focus on change of state. Is this a solution or is there a better way

boolean onoff; boolean current;

//mainswitch if(mainSwitchPin == 0 &&onoff != current(digitalRead(switchPin))){ ** onoff=current** } //switch1 if(mainSwitchPin == 1 && onoff != current(digitalRead(switchPin))){ ** onoff=current** } //switch2 if(mainSwitchPin == 2 && onoff != current(digitalRead(switchPin))){ ** onoff=current** } .............

You can't have a function, current(), and a variable, current, in the same sketch.

You can write a function, changed(), that returns true (a change has happened) or false (no change happened). In that function, you test the current state of the pin against the previous state of the pin. They are not the same, a change happened. If they are the same, no change happened.

You can store the result of calling that function in a boolean variable.

The name onoff is a crappy name. It doesn't indicate in any way what true means. Or false.

sorry for the messy code and bad variable names, but I just playing around and try to figure out how to solve this in a nice way.

But thanks for the advice regarding the state of change function, I will try and get something like that working.