lehrkraefte:blc:math:formi:keypad

class ButtonMatrix {
  private:
  int rowPins[4];
  int colPins[4];
 
  public:
  ButtonMatrix(int r1, int r2, int r3, int r4, int c1, int c2, int c3, int c4) {
    rowPins[0] = r1;
    rowPins[1] = r2;
    rowPins[2] = r3;
    rowPins[3] = r4;
    colPins[0] = c1;
    colPins[1] = c2;
    colPins[2] = c3;
    colPins[3] = c4;
    for (int i=0; i<4; i++) {
      pinMode(rowPins[i], INPUT_PULLUP);
      pinMode(colPins[i], OUTPUT);
      digitalWrite(colPins[i], HIGH);
    }
  }
  // return 0 for no button, 1-16 for the number of pressed button
  // -1 for more than one button
  int getButton() {
    int res = 0;
    for (int i=0; i<4; i++) {
      digitalWrite(colPins[i],LOW);
      for (int j=0; j<4; j++) {
        if (digitalRead(rowPins[j])==0) {
          if (res==0) {
            res = i*4+j+1;
          } else {
            res = -1;
          }
        }
      }
      digitalWrite(colPins[i],HIGH);
    }
    return res;
  }
};
 
// Pins für die Anschlüsse am Keypad (Von unten nach oben auf ports 2 bis 9)
ButtonMatrix bm(6,7,8,9,5,4,3,2);
 
void setup() {
  Serial.begin(115200);
}
 
void loop() {
  Serial.println(bm.getButton());
  delay(100);
}
  • lehrkraefte/blc/math/formi/keypad.txt
  • Last modified: 2017/01/22 11:27
  • by Ivo Blöchliger