鍵盤結合 LCD 作業(使用 virtualbreadboard) 2011.12.5
1. 請說明你的程式要做什麼事。
答:要按鍵盤的﹍﹍﹍鍵時使 LCD 顯示﹍﹍﹍
要按鍵盤的﹍﹍﹍鍵時使 LCD 顯示﹍﹍﹍
請在以下的程式中將要修改的地方用 pdfXViewer 註明。
2. 程式電路圖:
以下程式功能為:按鍵盤的”*”鍵時 LCD 顯示*,按鍵盤的”#”鍵時 LCD 顯示#
1 1
9 9
#include <LiquidCrystal.h>
#include <Keypad.h>
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 7, 8, 9, 10) ;
char keys[rows][cols] = { {'1','2','3'}, {'4','5','6'},
{'7','8','9'}, {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these pins, // eg. ROW0 = Arduino pin2.
byte rowPins[] = { 0, 1, 2, 3 };
// Connect keypad COL0, COL1 and COL2 to these pins, // eg. COL0 = Arduino pin6.
byte colPins[] = { 6, 5, 4 };
//create a new Keypad
Keypad keypad = Keypad(rowPins, colPins, sizeOf(rowPins) , sizeOf(colPins) );
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop() {
char Key = keypad.getKey();
switch (Key) { case '*':
lcd.setCursor(0, 1);
lcd.print("*");
break;
case '#':
case'1';
lcd.print("1");
case'9';
lcd.setCursor(0, 1);
lcd.print("#");
break;
case KEY_RELEASED:
Serial.println("Key Released");
break;
default:
if (Key != NO_KEY){
Serial.println(Key);
} }
}
註: Keypad 程式的較簡易寫法(相較於 VBB 提供的程式而言):
#include <Keypad.h>
char keys[rows][cols] = { {'1','2','3'}, {'4','5','6'},
{'7','8','9'}, {'*','0','#'}
lcd.print("9");
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these pins, // eg. ROW0 = Arduino pin2.
byte rowPins[] = { 0, 1, 2, 3 };
// Connect keypad COL0, COL1 and COL2 to these pins, // eg. COL0 = Arduino pin6.
byte colPins[] = { 6, 5, 4 };
//create a new Keypad
Keypad keypad = Keypad(rowPins, colPins, sizeOf(rowPins) , sizeOf(colPins) );
#define ledPin 13 void setup(){
Serial.begin(9600);
digitalWrite(ledPin, HIGH); // sets the LED on }
void loop(){
char Key = keypad.getKey();
switch (Key) { case '*':
digitalWrite(ledPin, HIGH);
break;
case '#':
digitalWrite(ledPin, LOW);
break;
case KEY_RELEASED:
Serial.println("Key Released");
break;
default:
if (Key != NO_KEY){
Serial.println(Key);
}
} }