รบกวนหน่อยคับ พอดีผมจะเขียนโค๊ค รับค่าจาก keypad 2 ครั้ง ตัวอย่างแบบว่า
1. จะเลือกของชนิดข้าว แบบว่ามี 4 ชนิด
2. เลือกจำนวนน้ำหนัก เช่น 1 กก. 2กก. ....
ตอนเลือกชนิดได้เเล้ว จะไปเลือกน้ำหนัก ตรง(input weight) อะคับ
ลองเขียนเเล้ว มันกดได้แค่ตัวเลข 1 2 3 4 (พอดีมือไหม่อะคับ รบกวนหน่อยคับ)
#include
#include
#include
LiquidCrystal_I2C lcd(0x3F, 20, 4);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'.','0',' '}
};
byte rowPins[ROWS] = {43, 41, 39, 37}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {35, 33, 31}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char num;
int Buzzer = 53;
int pin;
//char checkwieght;
void setup()
{
Serial.begin(9600);
lcd.begin();
lcd.setCursor(0,0);
lcd.print("Select num = ");
lcd.setCursor(0, 1);
lcd.print("Select Weight = ");
lcd.setCursor(0,2);
lcd.print("Coin = ");
lcd.setCursor(0,3);
lcd.print("Coin in= ");
pinMode(Buzzer,OUTPUT);
digitalWrite(Buzzer,HIGH);
lcd.blink();
lcd.setCursor(13,0);
}
void loop(){
char key = customKeypad.getKey(); //read keypad
if(key != NO_KEY){
if(key == '1')
{
lcd.print(key);
Serial.print(key);
Beep();
lcd.setCursor(16,1);
.............................................. // input weight
}
if(key == '2') {
lcd.print(key);
Beep();
lcd.setCursor(16,1);
.............................................. // input weight
}
if(key == '3') {
lcd.print(key);
Beep();
lcd.setCursor(16,1);
.............................................. // input weight
}
if(key == '4'){
lcd.print(key);
Beep();
lcd.setCursor(16,1);
.............................................. // input weight
}
}
}
void Beep()
{
digitalWrite(Buzzer,LOW);
delay(50);
digitalWrite(Buzzer,HIGH);
}
ลองดูโคดการใช้งาน keypad เพิ่มเติม ตามตัวอย่างนี้ครับ
http://tronixstuff.com/2011/11/12/tutorial-arduino-and-numeric-keypads-part-two/
ให้เก็บไว้ในตัวแปรข้างนอกวงเล็บ
เช่น ประกาศ char key ไว้บนสุด ค่าจะไม่หาย สามารถนำไปเปรียบเทียบได้ครับ
char key;
void setup() {
}
void loop() {
int count=0;
while(count < 4)
{ key = customKeypad.getKey();
if(key != NO_KEY){
Serial.print(key);
count++;
if(key== '*') break;
}
}
}