Detect state change when using keypad?

Hi (again),

Well, I'm almost done with my project now.. it works wonders with just 1 key to change states. Mainly because with 1 key, I can use this code for button state change, where a digital pin is read and the button state is changed accordingly:

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      Serial.println("off");
    }
    else {
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  lastButtonState = buttonState;
}

Well, I replaced that button with a keypad now. It's the EventKeypad from the downloadable keypad library. And suddenly it doesnt seem so simple anymore, cause the keypad matrix connects various digital pins in all sorts of patterns to be able to read which key is pushed.

But I still need that button state change function! So how would I change the above to be used for something like this: (pseudo code)

If key pressed = '#'
Run Function1 (also after key '#' is released)
If key pressed = '#' again
Run Function2 (also after key '#' is released)
If key pressed = '#' again
Run Function1 (also after key '#' is released)

And so on and so on...

Point is I want the arduino to remember that '#' has been pressed and do Function1, until I press it again, and then it shifts to Function2, and then back when I press again.

An understandable answer in correct code, as I'm rather green, will be really appreciated! :slight_smile:

If you have multiple keys read them all in a single function called from loop().

...R

Made it work... now on to next problem. lol

Thank you. :slight_smile:

cajodk:
Made it work... now on to next problem. lol

Thank you. :slight_smile:

any explanation or example on the code ? I need exactly the same THING and still confused :frowning:

Look up Flag or flag byte.