Same action for High and Low

I was just wondering if there was a way to code a state change when a switch is toggled on or off but have it perform the same action?

Example: Switch is thrown to on and registers HIGH so the light comes on. But then it is thrown to the off position and instead of turning the light off it detects the change but the result is the light coming on again.

I know this example makes no logical sense for practical use with a light since it just means the light stays on but its the best way I can explain it. But its actually just about registering a state change and then performing the same action for both HIGH and LOW states.

Here's the relevant part from the state change detect tutorial: just turn your light on where I marked XXXXX (switch closes) and at YYYYY (switch opens):

if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      //XXXXXXXXXXXXX turn light on here
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      //YYYYYYYYYYYYYYY turn light on here
      Serial.println("off");
    }

jubukraa:
Here's the relevant part from the state change detect tutorial: just turn your light on where I marked XXXXX (switch closes) and at YYYYY (switch opens):

if (buttonState != lastButtonState) {

// if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      //XXXXXXXXXXXXX turn light on here
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      //YYYYYYYYYYYYYYY turn light on here
      Serial.println("off");
    }

I'm sorry but I am not sure I understand what you are saying. Are you trying to say that I just need to use the same part of the code for off that I would use for on? Basically just repeating the code under else?

jbearnolimits:
I’m sorry but I am not sure I understand what you are saying. Are you trying to say that I just need to use the same part of the code for off that I would use for on? Basically just repeating the code under else?

Yes, exactly. And if that repeated code is lengthy, put it in a function and then just call that function from both places.

consider

#define ButPin  A1
#define LedPin  10

// ---------------------------------------------------------
bool
butChange (void)
{
    static bool butLst   = HIGH;
           bool but      = digitalRead (ButPin);

    if (butLst != but) {
        butLst = but;
        delay (10);     // button deboune
        return true;
    }
    return false;
}

// ---------------------------------------------------------
void
loop (void)
{
    static int state = 0;

    if (butChange ())
        state = 10;


    static unsigned long msecLst  = 0;
           unsigned long msec     = millis ();

    if (msec - msecLst > 100)  {
        msecLst = msec;

        if (0 < state)  {
            state --;
            digitalWrite (LedPin, ! digitalRead (LedPin));
        }
    }
}

// ---------------------------------------------------------
void
setup (void)
{
    Serial.begin (115200);
    pinMode (ButPin, INPUT_PULLUP);
    pinMode (LedPin, OUTPUT);
    digitalWrite (LedPin, HIGH);
}

or you could remove the guff from the StateChangeDetection example and end up with this

const int  buttonPin = 2;

int buttonState = HIGH;
int lastButtonState = HIGH;

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(115200);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    Serial.println("state changed"); // do your thing here
    delay(50); // poor man's debounce
  }
  lastButtonState = buttonState;
}