2020年12月28日 星期一

2020/12/28

 

期末



#include <Keypad.h>    // 引用Keypad程式庫

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,16,2); 

#define KEY_ROWS 4 // 按鍵模組的列數

#define KEY_COLS 4 // 按鍵模組的行數


#define BUZZER 8


#define NOTE_C1 261 //F,C

#define NOTE_C2 293 //E,D

#define NOTE_C3 329 //D,E

#define NOTE_C4 349 //C,F

#define NOTE_C5 391 //B,G

#define NOTE_C6 440 //3,A

#define NOTE_C7 493 //6,B

#define NOTE_C8 532 //9,C



#define NOTE_C9 587 //A,D

#define NOTE_C10 659 //2,E

#define NOTE_C11 698 //5,F

#define NOTE_C12 783 //8,G

#define NOTE_C13 880 //0,A

#define NOTE_C14 987 //1,B

#define NOTE_C15 1046 //4,C

#define NOTE_C16 1174 //7,D



// 依照行、列排列的按鍵字元(二維陣列)

char keymap[KEY_ROWS][KEY_COLS] = {

  {'F', 'B', 'A', '0'},

  {'E', '3', '2', '1'},

  {'D', '6', '5', '4'},

  {'C', '9', '8', '7'}

};

byte colPins[KEY_COLS] = {7, 6, 5, 4};     // 按鍵模組,行1~4接腳。

byte rowPins[KEY_ROWS] = {12, 11, 10, 9}; // 按鍵模組,列1~4接腳。 

// 初始化Keypad物件

// 語法:Keypad(makeKeymap(按鍵字元的二維陣列), 模組列接腳, 模組行接腳, 模組列數, 模組行數)

Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, KEY_ROWS, KEY_COLS);

void setup(){

  Serial.begin(9600);

  lcd.init();


lcd.backlight();//是否開啟背光

/*

lcd.setCursor(2,0);//設定游標位置


lcd.print("TAIWAN 2021");//寫入字串


lcd.setCursor(1,1);//設定游標位置


lcd.print("Happy New Year");//寫入字串

*/

void loop(){

  // 透過Keypad物件的getKey()方法讀取按鍵的字元

  char key = myKeypad.getKey();

  

  if (key){  // 若有按鍵被按下…

    Serial.println(key);  // 顯示按鍵的字元

    

     if(key=='F') {tone(BUZZER, NOTE_C1, 200);lcd.setCursor(0,0);lcd.print("0");}

     if(key=='E') {tone(BUZZER, NOTE_C2, 200);lcd.setCursor(1,0);lcd.print("1");}

     if(key=='D') {tone(BUZZER, NOTE_C3, 200);lcd.setCursor(2,0);lcd.print("2");}

     if(key=='C') {tone(BUZZER, NOTE_C4, 200);lcd.setCursor(3,0);lcd.print("3");}

     if(key=='B') {tone(BUZZER, NOTE_C5, 200);lcd.setCursor(4,0);lcd.print("4");}

     if(key=='3') {tone(BUZZER, NOTE_C6, 200);lcd.setCursor(5,0);lcd.print("5");}

     if(key=='6') {tone(BUZZER, NOTE_C7, 200);lcd.setCursor(6,0);lcd.print("6");}

     if(key=='9') {tone(BUZZER, NOTE_C8, 200);lcd.setCursor(7,0);lcd.print("7");}


    if(key=='A') {tone(BUZZER, NOTE_C9, 200);lcd.setCursor(8,0);lcd.print("8");}

    if(key=='2') {tone(BUZZER, NOTE_C10, 200);lcd.setCursor(9,0);lcd.print("9");}

    if(key=='5') {tone(BUZZER, NOTE_C11, 200);lcd.setCursor(10,0);lcd.print("A");}

    if(key=='8') {tone(BUZZER, NOTE_C12, 200);lcd.setCursor(11,0);lcd.print("B");}

    if(key=='0') {tone(BUZZER, NOTE_C13, 200);lcd.setCursor(12,0);lcd.print("C");}

    if(key=='1') {tone(BUZZER, NOTE_C14, 200);lcd.setCursor(13,0);lcd.print("D");}

    if(key=='4') {tone(BUZZER, NOTE_C15, 200);lcd.setCursor(14,0);lcd.print("E");}

    if(key=='7') {tone(BUZZER, NOTE_C16, 200);lcd.setCursor(15,0);lcd.print("F");}

  }

}

沒有留言:

張貼留言