#define PIN_NUM1 2
#define PIN_NUM2 3
#define PIN_NUM3 4
#define PIN_NUM4 5
#define PIN_NUM5 6
#define PIN_NUM6 7
#define PIN_NUM7 8
#define PIN_NUM8 9
#define PIN_NUM9 10
#define PIN_STAR 11
#define PIN_NUM0 12
#define PIN_HASH 13
unsigned int pins[12] = {PIN_NUM1, PIN_NUM2, PIN_NUM3, PIN_NUM4, PIN_NUM5, PIN_NUM6, PIN_NUM7, PIN_NUM8, PIN_NUM9, PIN_STAR, PIN_NUM0, PIN_HASH};
unsigned char buttons[12] = {'*', '7', '4', '1', '0', '8', '5', '2', '#', '9', '6', '3'};
char key = '\0';
bool flag = false;
void setup()
{
// Set keypad pins
pinMode(PIN_NUM1, INPUT_PULLUP);
pinMode(PIN_NUM2, INPUT_PULLUP);
pinMode(PIN_NUM3, INPUT_PULLUP);
pinMode(PIN_NUM4, INPUT_PULLUP);
pinMode(PIN_NUM5, INPUT_PULLUP);
pinMode(PIN_NUM6, INPUT_PULLUP);
pinMode(PIN_NUM7, INPUT_PULLUP);
pinMode(PIN_NUM8, INPUT_PULLUP);
pinMode(PIN_NUM9, INPUT_PULLUP);
pinMode(PIN_STAR, INPUT_PULLUP);
pinMode(PIN_NUM0, INPUT_PULLUP);
pinMode(PIN_HASH, INPUT_PULLUP);
Serial.begin(9600);
}
//---------------------------------------------------------------------
void loop()
{
char key = getKey();
if (key) // Check for a valid key.
{
if(flag == true) // One time only
{
Serial.print("Key "); Serial.println(key);
flag = false;
}
}
}
//---------------------------------------------------------------------
char getKey()
{
for (int i = 0; i < 12; i++)
{
if (digitalRead(pins[i]) == LOW)
{
return buttons[i];
}
}
delay(100); // Debouncing
flag = true; // allows 1 print
return '\0'; // Clear key
}