/*
references: Lesson_23, Lesson_17
*/
#include <Keypad.h>
#include <LiquidCrystal.h>
// set up buzzer
const int BUZZER = 21;
//set up keypad
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 11, 10};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//set up lcd
LiquidCrystal lcd(19, 18, 14, 15, 16, 17);
/*
* LCD RS pin to digital pin 19
* LCD Enable pin to digital pin 18
* LCD D4 pin to digital pin 14
* LCD D5 pin to digital pin 15
* LCD D6 pin to digital pin 16
* LCD D7 pin to digital pin 17
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
*/
// setup methods
void setUpLCD() {
lcd.begin(16, 2);
lcd.clear();
}
void setUpBuzzer() {
pinMode(BUZZER, OUTPUT);
}
void startMonitoring() {
Serial.begin(9600);
}
// main methods
void setup() {
setUpBuzzer();
startMonitoring();
setUpLCD();
delay(1000);
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey) {
PlayTone();
Serial.println(customKey);
HandleButtonPress(customKey);
}
}
// helper methods
void HandleButtonPress(char customKey) {
if(!CheckAndRunABC(customKey)) lcd.print(customKey);
}
bool CheckAndRunABC(char customKey) {
switch(customKey) {
case 'C':
lcd.clear();
return true;
case 'B':
lcd.noDisplay();
return true;
case 'A':
lcd.display();
return true;
default:
return false;
}
}
void PlayTone() {
tone(BUZZER, 500, 500);
}