4x4 Keypad with Arduino pins using PCF8574P

Used Products:

1. Arduino UNO
2. PCF8574P

Content:

Download Library: https://github.com/joeyoung/arduino_keypads Çeşme: http://bradsduino.blogspot.com.tr/2013/01/i2c-version-of-using-4x4-universal-16.html

Used Libraries:

1. Keypad_I2C

Code:

#include <Wire.h> #include <Keypad_I2C.h> #include <Keypad.h> const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; byte rowPins[ROWS] = {0,1,2,3}; byte colPins[COLS] = {4,5,6,7}; int i2caddress = 0x20; Keypad_I2C kpd = Keypad_I2C( makeKeymap(keys), rowPins, colPins, ROWS, COLS, i2caddress ); const int LED_ANODE_A = 2; const int LED_ANODE_B = 3; const int LED_ANODE_C = 4; const int LED_ANODE_D = 5; const int LED_ANODE_E = 6; const int LED_ANODE_F = 7; const int LED_ANODE_G = 8; void setup(){ Serial.begin(9600); kpd.begin(); for (int i = 2; i <= 8; i++) { pinMode(i, OUTPUT); } } void loop(){ char key = kpd.getKey(); if (key){ Serial.println(key); offLedsAnodes(); display_anode(key-'0'); } } //------------------------------- void number_anode_9() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_C, LOW); digitalWrite(LED_ANODE_D, LOW); digitalWrite(LED_ANODE_F, LOW); digitalWrite(LED_ANODE_G, LOW); } void number_anode_8() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_C, LOW); digitalWrite(LED_ANODE_D, LOW); digitalWrite(LED_ANODE_E, LOW); digitalWrite(LED_ANODE_F, LOW); digitalWrite(LED_ANODE_G, LOW); } void number_anode_7() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_C, LOW); } void number_anode_6() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_F, LOW); digitalWrite(LED_ANODE_G, LOW); digitalWrite(LED_ANODE_C, LOW); digitalWrite(LED_ANODE_D, LOW); digitalWrite(LED_ANODE_E, LOW); } void number_anode_5() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_F, LOW); digitalWrite(LED_ANODE_G, LOW); digitalWrite(LED_ANODE_C, LOW); digitalWrite(LED_ANODE_D, LOW); } void number_anode_4() { digitalWrite(LED_ANODE_F, LOW); digitalWrite(LED_ANODE_G, LOW); digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_C, LOW); } void number_anode_3() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_G, LOW); digitalWrite(LED_ANODE_C, LOW); digitalWrite(LED_ANODE_D, LOW); } void number_anode_2() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_G, LOW); digitalWrite(LED_ANODE_E, LOW); digitalWrite(LED_ANODE_D, LOW); } void number_anode_1() { digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_C, LOW); } void number_anode_0() { digitalWrite(LED_ANODE_A, LOW); digitalWrite(LED_ANODE_B, LOW); digitalWrite(LED_ANODE_C, LOW); digitalWrite(LED_ANODE_D, LOW); digitalWrite(LED_ANODE_E, LOW); digitalWrite(LED_ANODE_F, LOW); } void offLedsAnodes() { for (int i = 2; i <= 8; i++) { digitalWrite(i, HIGH); } } void display_anode(int i) { switch (i) { case 0: number_anode_0(); break; case 1: number_anode_1(); break; case 2: number_anode_2(); break; case 3: number_anode_3(); break; case 4: number_anode_4(); break; case 5: number_anode_5(); break; case 6: number_anode_6(); break; case 7: number_anode_7(); break; case 8: number_anode_8(); break; case 9: number_anode_9(); break; } }