Keypad untuk arduino terdiri atas numerik 0-9 serta ‘*’ dan ‘#’ berfungsi sebagai input bagi arduino. Arduino membaca keypad (type membrane) dengan metode scanning 7 kabel (untuk keypad 3×4), atau adc satu kabel. Dengan menggunakan library keypad, nilai input yang diterima arduino sudah berupa karakter ‘0’ – ‘9’, ‘*’ dan ‘#’, nilai ini bisa baca langsung sebagai perintah seperti contoh berikut ”
void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); switch(key) { case '0': digitalWrite(2, HIGH); break; case '1': digitalWrite(3, HIGH); break; case '*'://reset digitalWrite(2, LOW); digitalWrite(3, LOW); break; } } }
Input deret angka
Supaya keypad berfungsi sebagai input nilai angka (misal 0-1000) seperti untuk keperluan input variabel ‘setting batas sensor analog’, maka keypad dibaca beberapa kali dengan ketentuan:
- karakter ‘0’ – ‘9’ sebagai input numerik
- karakter ‘*’ berfungsi sebagai reset (kembali ke 0)
- karakter ‘#’ berfungsi sebagai enter layaknya keybboard laptop dan menyimpan pembacaan keypad sebagai nilai variabel
berikut skema yang digunakan untuk pembacaan keypad dengan arduino:
koding input nilai variabel dari keypad :
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include <Keypad.h> const byte ROWS = 4; const byte COLS = 3; char keys[ROWS][COLS] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; byte rowPins[ROWS] = {8, 7, 6, 5}; byte colPins[COLS] = {4, 3, 2}; Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); LiquidCrystal_I2C lcd(0x3F, 16, 2);//coba juga 0x27 char stringAngka[17]; int indexKeypad = 0; void setup() { Serial.begin(9600); Serial.println("Input angka menggunakan keypad"); Serial.println("https://www.semesin.com/project/"); Serial.println(); Wire.begin(); Wire.beginTransmission(0x3F); if (Wire.endTransmission()) { lcd = LiquidCrystal_I2C(0x27, 16, 2); } lcd.begin(); lcd.backlight(); lcd.print("Input angka"); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); switch (key) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (!indexKeypad) { lcd.clear(); } stringAngka[indexKeypad++] = key; lcd.print(key); break; case '*'://reset lcd.clear(); indexKeypad = 0; break; case '#': stringAngka[indexKeypad] = 0; lcd.setCursor(0, 1); int nilaiAngka = atoi(stringAngka); lcd.print(nilaiAngka); indexKeypad = 0; break; } } }
contoh penggunaan keypad sebagai masukan variabel integer: