lehrkraefte:blc:math:formi:keypad

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

lehrkraefte:blc:math:formi:keypad [2017/01/22 10:20] – created Ivo Blöchligerlehrkraefte:blc:math:formi:keypad [2017/01/22 10:27] (current) – [Standalone Sketch] Ivo Blöchliger
Line 1: Line 1:
 +{{backlinks>.}}
 +===== 4x4 Button Matrix =====
 +Siehe z.B. http://playground.arduino.cc/code/keypad
  
 +
 +==== Standalone Sketch ====
 +{{:lehrkraefte:blc:math:formi:keypad-anschluss.jpg?direct&200|}} {{:lehrkraefte:blc:math:formi:keypad-arduino.jpg?direct&200|}} 
 +<code c++>
 +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);
 +}
 +</code>