#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Variables
float buttonTimer = 0;
bool buttonPressed = false;
String fullString = "";
String pressedCharacter = "";
bool stringAdded = false;
double convertedNumber = 0;
// Keypad constants
// Mapping of rows and corresponding characters
struct KeyMapping {
byte rowValue; // Value to write for the row
byte cases[4]; // Possible return values from Wire.read()
const char *chars[4]; // Corresponding characters for each case
};
KeyMapping keyMap[] = {
{B11101111, {238, 237, 235, 231}, {"1", "2", "3", "A"}},
{B11011111, {222, 221, 219, 215}, {"4", "5", "6", "B"}},
{B10111111, {190, 189, 187, 183}, {"7", "8", "9", "C"}},
{B01111111, {126, 125, 123, 119}, {"*", "0", "#", "D"}}
};
// Objects
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Wire.begin();
Wire.setClock(400000L);
Serial.begin(9600);
Serial.println("PCF8574 with a keypad");
// lcd
lcd.init();
lcd.setCursor(0, 0);
lcd.print("Keypad 123-ABC");
lcd.setCursor(0, 1);
lcd.print("Demo");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("String: ");
lcd.setCursor(0, 1);
lcd.print("Double: ");
}
void loop() {
readKeypad();
checkInput();
}
void checkInput() {
if(buttonPressed) {
if(stringAdded) {
fullString = fullString + pressedCharacter;
lcd.setCursor(8, 0);
lcd.print(fullString);
lcd.setCursor(8, 1);
lcd.print(" ");
stringAdded = false;
}
}
}
/****/
void readKeypad() {
if (buttonPressed) {
if (millis() - buttonTimer > 300) {
buttonTimer = millis();
buttonPressed = false;
}
return;
}
for (auto &row : keyMap) {
Wire.beginTransmission(0x20);
Wire.write(row.rowValue);
Wire.endTransmission();
Wire.requestFrom(0x20, 1);
byte readValue = Wire.read();
for (int i = 0; i < 4; i++) {
if (readValue == row.cases[i]) {
Serial.print(row.chars[i]);
pressedCharacter = row.chars[i];
buttonPressed = true;
stringAdded = true;
return; // Exit after finding the pressed key
}
}
}
}
/** Demonstration of keypad on i2c
void readKeypad() {
if(buttonPressed == true) {
if(millis() - buttonTimer > 300) {
// The buttonPressed is only set back after 300ms so we cannot press button quickly
buttonTimer = millis();
buttonPressed = false;
}
}
else {
//========== First row ============//
Wire.beginTransmission(0x20);
Wire.write(B11101111);
//B[P7]11101111[P0] -> [P7]1110[P4] - activates first row, [P3]1111[P0] - Sets all pins high on the MUX
Wire.endTransmission();
Wire.requestFrom(0x20, 1);
switch(Wire.read()) {
//11101110 - P0 pin went low after pressing the button 1
case 238: // B11101110 -> D238
Serial.print("1");
pressedCharacter = "1";
buttonPressed = true;
stringAdded = true;
break;
//11101101 - P1 pin went low after pressing the button 2
case 237: // B11101101 -> D237
Serial.print("2");
pressedCharacter = "2";
buttonPressed = true;
stringAdded = true;
break;
//11101011 - P2 pin went low after pressing the button 3
case 235: // B11101011 -> D235
Serial.print("3");
pressedCharacter = "3";
buttonPressed = true;
stringAdded = true;
break;
//11100111 - P3 pin went low after pressing the button A
case 231: // B11100111 -> D231
Serial.print("A");
pressedCharacter = "A";
buttonPressed = true;
stringAdded = true;
break;
}
//========== Second row ============//
Wire.beginTransmission(0x20);
Wire.write(B11011111);
//B[P7]11011111[P0] -> [P7]1101[P4] - activates second row, [P3]1111[P0] - Sets all pins high on the MUX
Wire.endTransmission();
Wire.requestFrom(0x20, 1);
switch(Wire.read()) {
//11011110 - P0 pin went low after pressing the button 4
case 222: // B11011110 -> D222
Serial.print("4");
pressedCharacter = "4";
buttonPressed = true;
stringAdded = true;
break;
//11011101 - P1 pin went low after pressing the button 5
case 221: // B11011101 -> D221
Serial.print("5");
pressedCharacter = "5";
buttonPressed = true;
stringAdded = true;
break;
//11011011 - P2 pin went low after pressing the button 6
case 219: // B11011011 -> D219
Serial.print("6");
pressedCharacter = "6";
buttonPressed = true;
stringAdded = true;
break;
//11010111 - P3 pin went low after pressing the button B
case 215: // B11010111 -> D215
Serial.print("B");
pressedCharacter = "B";
buttonPressed = true;
stringAdded = true;
break;
}
//========== Third row ============//
Wire.beginTransmission(0x20);
Wire.write(B10111111);
//B[P7]10111111[P0] -> [P7]1011[P4] - activates second row, [P3]1111[P0] - Sets all pins high on the MUX
Wire.endTransmission();
Wire.requestFrom(0x20, 1);
switch(Wire.read()) {
//10111110 - P0 pin went low after pressing the button 7
case 190: // B10111110 -> D190
Serial.print("7");
pressedCharacter = "7";
buttonPressed = true;
stringAdded = true;
break;
//10111101 - P1 pin went low after pressing the button 8
case 189: // B10111101 -> D189
Serial.print("8");
pressedCharacter = "8";
buttonPressed = true;
stringAdded = true;
break;
//10111011 - P2 pin went low after pressing the button 9
case 187: // B10111011 -> D187
Serial.print("9");
pressedCharacter = "9";
buttonPressed = true;
stringAdded = true;
break;
//10110111 - P3 pin went low after pressing the button C
case 119: // B10110111 -> D183
Serial.print("C");
pressedCharacter = "C";
buttonPressed = true;
stringAdded = true;
break;
}
//========== Fourth row ============//
Wire.beginTransmission(0x20);
Wire.write(B01111111);
//B[P7]01111111[P0] -> [P7]0111[P4] - activates second row, [P3]1111[P0] - Sets all pins high on the MUX
Wire.endTransmission();
Wire.requestFrom(0x20, 1);
switch(Wire.read()) {
//01111110 - P0 pin went low after pressing the button *
case 126: // B01111110 -> D126
Serial.print("*");
pressedCharacter = "*";
buttonPressed = true;
stringAdded = true;
break;
//01111101 - P1 pin went low after pressing the button 0
case 125: // B01111101 -> D125
Serial.print("0");
pressedCharacter = "0";
buttonPressed = true;
stringAdded = true;
break;
//01111011 - P2 pin went low after pressing the button #
case 123: // B01111011 -> D123
Serial.print("#");
pressedCharacter = "#";
buttonPressed = true;
stringAdded = true;
break;
//01110111 - P3 pin went low after pressing the button D
case 119: // B01110111 -> D119
Serial.print("D");
pressedCharacter = "D";
buttonPressed = true;
stringAdded = true;
break;
}
}
}
**/