Arduino Projekt 28 bringt uns zu den numerischen Tastenfeldern.
Vorbereitungen:
Für dieses Projekt werden folgende Bauteile benötigt:
Schaltungsaufbau:
Die Verkabelung wird nach den Angaben im Datenblatt vorgenommen. Für das Tastenfeld in diesem Projekt gilt folgender Schaltungsaufbau:
| Tastenfeld-Pin | Arduino-Pin |
| 1 | 3 |
| 2 | 5 |
| 3 | 2 |
| 4 | 8 |
| 5 | 4 |
| 6 | 7 |
| 7 | 6 |
Programmierung 1:
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {2, 3, 4};
Keypad keypad = Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Ende des erforderlichen Codes für Tastenfeld
void setup()
{
Serial.begin (9600);
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
Serial.print(key);
}
}
Programmierung 2:
#include "Keypad.h"
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5,6,7,8};
byte colPins[COLS] = {2,3,4};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char PIN[6]={'1','2','3','4','5','6'}; // Die Geheimzahl
char attempt[6]={0,0,0,0,0,0};
int z=0;
void setup()
{
Serial.begin(9600);
}
void correctPIN() // wird bei korrekter PIN ausgeführt
{
Serial.println("richtige PIN eingegeben");
}
void incorrectPIN()
{
Serial.println("falsche PIN eingegeben");
}
void checkPIN()
{
int correct=0;
int i;
for(i=0; i<6; i++)
{
if(attempt[i]==PIN[i])
{
correct++;
}
}
if (correct==6)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz=0; zz<6; zz++) // entfernt den vorherigen Eingabeversuch
{
attempt[zz]=0;
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
switch(key)
{
case '*':
z=0;
break;
case '#':
delay(100); // zum Entprellen
checkPIN();
break;
default:
attempt[z]=key;
z++;
}
}
}
void loop()
{
readKeypad();
}