EXAMPLE CODE: #include <LiquidCrystal_I2C.h> #include <Keypad.h> #include <Key.h
ID: 3592674 • Letter: E
Question
EXAMPLE CODE:
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Key.h>
// The wire library is sometimes need you can remove it unless your compiler complains
// about it as a missing library
#include <Wire.h>
// Pin Definitions for the LCD Display
#define lcdSDAPin A4
#define lcdSCLPin A5
#define lcdAddress 0x27
#define lcdColumns 16
#define lcdRows 2
//Pin definitions for the 4x4 matrix keypad
//UPDATE the numbers to match the pin connections from YOUR keypad to YOUR Arduino
#define Row1Pin 7
#define Row2Pin 8
#define Row3Pin 9
#define Row4Pin 10
#define Col1Pin 6
#define Col2Pin 5
#define Col3Pin 4
#define Col4Pin 3
const byte ROWS = 4; // Four rows on the keypad
const byte COLS = 4; // Four columns on the keypad
// Define the keymap for the keypad
char keys[ROWS][COLS] = {
{ '1','2','3','A' },
{ '4','5','6','B' },
{ '7','8','9','C' },
{ '#','0','*','D' }
};
byte rowPins[ROWS] = { Row1Pin, Row2Pin, Row3Pin, Row4Pin }; // Connect to the row pinouts of the keypad
byte colPins[COLS] = { Col1Pin, Col2Pin, Col3Pin, Col4Pin }; // Connect to the column pinouts of the keypad
// Initialize the keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(lcdAddress, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
// Setting up the LCD Display
lcd.begin(lcdColumns, lcdRows);
lcd.backlight();
lcd.clear();
// Setting up the 4x4 keypad matrix
keypad.setDebounceTime(100); // Sets the debounce delay in mS
keypad.setHoldTime(3000); // How long button must be held before considered "held"
keypad.addEventListener(keypadButtonPressed); // Function ran when key is pressed
}
void loop()
{
// Must be called frequently for the event handler to work properly
char key = keypad.getKey();
}
// This is the event handler function, whenever the arduino detects that a button is
// pressed this function is executed, the information for the pressed button is held
// in the variable “key” and then passed to this function this is why the “getKey()”
// function must be called frequently in the main loop
void keypadButtonPressed(KeypadEvent key) {
if (keypad.getState() == PRESSED || keypad.getState() == HOLD) {
//Clear the LCD and set the cursor
lcd.clear();
lcd.setCursor(0, 0);
}
// Check if a key was pressed on the pin pad
if (keypad.getState() == PRESSED) {
lcd.print("Pressed: ");
lcd.print(key);
}
if (keypad.getState() == HOLD) {
lcd.print("Held: ");
lcd.print(key);
}
}
Build Upon your new skills:
Create a simple 4 function calculator
Using the keypad and the LCD screen create a simple calculator to do addition, subtraction, multiplication, and division
One of the buttons should be an equals (=) button
One of the buttons should be a clear button
The LCD screen should show the buttons that are being pressed; when the user presses the (=) button the LCD screen should display the correct result.
Explanation / Answer
#define Row1Pin 7
#define Row2Pin 8
#define Row3Pin 9
#define Row4Pin 10
#define Col1Pin 6
#define Col2Pin 5
#define Col3Pin 4
#define Col4Pin 3
const byte ROWS = 4; // Four rows on the keypad
const byte COLS = 4; // Four columns on the keypad
// Define the keymap for the keypad
char keys[ROWS][COLS] = {
{ '1','2','3','A' },
{ '4','5','6','B' },
{ '7','8','9','C' },
{ '#','0','*','D' }
};
byte rowPins[ROWS] = { Row1Pin, Row2Pin, Row3Pin, Row4Pin }; // Connect to the row pinouts of the keypad
byte colPins[COLS] = { Col1Pin, Col2Pin, Col3Pin, Col4Pin }; // Connect to the column pinouts of the keypad
// Initialize the keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(lcdAddress, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
// Setting up the LCD Display
lcd.begin(lcdColumns, lcdRows);
lcd.backlight();
lcd.clear();
// Setting up the 4x4 keypad matrix
keypad.setDebounceTime(100); // Sets the debounce delay in mS
keypad.setHoldTime(3000); // How long button must be held before considered "held"
keypad.addEventListener(keypadButtonPressed); // Function ran when key is pressed
}
void loop()
{
// Must be called frequently for the event handler to work properly
char key = keypad.getKey();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.