/*
ESP32 Keypad薄膜式(4x4)鍵盤
按鍵->蜂鳴器發音
*/
#include <Keypad.h>
uint8_t row[4] = {27, 26, 25, 33}; // 鍵盤列腳
uint8_t col[4] = {19, 18, 5, 17}; // 鍵盤行腳
byte bz = 15; //buzzer
char keys[4][4] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), row, col, 4, 4 );
//#中音音階---------------------------------
#define C5 523 //0
#define D5 587 //1
#define E5 659 //2
#define F5 698 //3
#define G5 784 //4
#define A5 880 //5
#define B5 988 //6
//#高音音階---------------------------------
#define C6 1047 //7
#define D6 1175 //8
#define E6 1319 //9
#define F6 1397 //A
#define G6 1568 //B
#define A6 1760 //C
#define B6 1976 //D
void setup(){;
pinMode(bz, OUTPUT);
}
void loop(){
char key = keypad.getKey();
if(key) sound(key);
}
void sound(char k){
int t=50;
switch (k){
case '0': tone(bz,C5,t);break;
case '1': tone(bz,D5,t);break;
case '2': tone(bz,E5,t);break;
case '3': tone(bz,F5,t);break;
case '4': tone(bz,G5,t);break;
case '5': tone(bz,A5,t);break;
case '6': tone(bz,B5,t);break;
case '7': tone(bz,C6,t);break;
case '8': tone(bz,D6,t);break;
case '9': tone(bz,E6,t);break;
case 'A': tone(bz,F6,t);break;
case 'B': tone(bz,G6,t);break;
case 'C': tone(bz,A6,t);break;
case 'D': tone(bz,B6,t);break;
}
}