Differences
This shows you the differences between two versions of the page.
| lehrkraefte:blc:math:formi:keypad [2017/01/22 10:20] – created Ivo Blöchliger | lehrkraefte: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:// | ||
| + | |||
| + | ==== Standalone Sketch ==== | ||
| + | {{: | ||
| + | <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], | ||
| + | pinMode(colPins[i], | ||
| + | digitalWrite(colPins[i], | ||
| + | } | ||
| + | } | ||
| + | // 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], | ||
| + | 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], | ||
| + | } | ||
| + | return res; | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | // Pins für die Anschlüsse am Keypad (Von unten nach oben auf ports 2 bis 9) | ||
| + | ButtonMatrix bm(6, | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(115200); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | Serial.println(bm.getButton()); | ||
| + | delay(100); | ||
| + | } | ||
| + | </ | ||