Pengetahuan Dasar Input Data Arduino menggunakan Keypad 4×4 di tampilkan pada LCD

Hardware

pada percobaan ini sy menggunakan arduino mega2560

ketikKeypad.jpg

#include <LiquidCrystal.h>
//LiquidCrystal(rs, en,d0, d1, d2, d3);
LiquidCrystal lcd( 22, 23, 24, 25, 26,27);

#define kolom1 2 //pin 2
#define kolom2 3
#define kolom3 4
#define kolom4 5

#define baris1 6
#define baris2 7
#define baris3 8
#define baris4 9
int key(void);
void bacaKeypad();
int y=0;

void setup()
{
lcd.begin(16, 4);
// put your setup code here, to run once:

Serial.begin(9600);
pinMode(kolom1, OUTPUT);
pinMode(kolom2, OUTPUT);
pinMode(kolom3, OUTPUT);
pinMode(kolom4, OUTPUT);

pinMode(baris1, INPUT);
pinMode(baris2, INPUT);
pinMode(baris3, INPUT);
pinMode(baris4, INPUT);
digitalWrite(baris1, HIGH);
digitalWrite(baris2, HIGH);
digitalWrite(baris3, HIGH);
digitalWrite(baris4, HIGH);

Serial.println(“test”);
lcd.setCursor(0, 0);
lcd.print(“ketik keypad :”);   // akhiri dengan ketik tanda #
}

void loop()
{
bacaKeypad();

}

int key(void)
{
unsigned char delaykeypad=700;
char KEY = 1 ;
while(KEY){

digitalWrite(kolom1, LOW);
digitalWrite(kolom2, HIGH);
digitalWrite(kolom3, HIGH);
digitalWrite(kolom4, HIGH);

if(digitalRead(baris1) == 0){delay(delaykeypad);KEY = 0; return 13 ; }
if(digitalRead(baris2) == 0){delay(delaykeypad);KEY = 0; return ‘#’ ; }
if(digitalRead(baris3) == 0){delay(delaykeypad);KEY = 0; return 0 ; }
if(digitalRead(baris4) == 0){delay(delaykeypad);KEY = 0; return ‘*’ ; }

//==========================================
digitalWrite(kolom1, HIGH);
digitalWrite(kolom2, LOW);
digitalWrite(kolom3, HIGH);
digitalWrite(kolom4, HIGH);

if(digitalRead(baris1) == 0){delay(delaykeypad);KEY = 0; return 12 ; }
if(digitalRead(baris2) == 0){delay(delaykeypad);KEY = 0; return 9 ; }
if(digitalRead(baris3) == 0){delay(delaykeypad);KEY = 0; return 8 ; }
if(digitalRead(baris4) == 0){delay(delaykeypad);KEY = 0; return 7 ; }
//==========================================

digitalWrite(kolom1, HIGH);
digitalWrite(kolom2, HIGH);
digitalWrite(kolom3, LOW);
digitalWrite(kolom4, HIGH);
if(digitalRead(baris1) == 0){delay(delaykeypad);KEY = 0; return 11 ; }
if(digitalRead(baris2) == 0){delay(delaykeypad);KEY = 0; return 6 ; }
if(digitalRead(baris3) == 0){delay(delaykeypad);KEY = 0; return 5 ; }
if(digitalRead(baris4) == 0){delay(delaykeypad);KEY = 0; return 4 ; }
//==========================================
//==========================================

digitalWrite(kolom1, HIGH);
digitalWrite(kolom2, HIGH);
digitalWrite(kolom3, HIGH);
digitalWrite(kolom4, LOW);

if(digitalRead(baris1) == 0){delay(delaykeypad);KEY = 0; return 10 ; }
if(digitalRead(baris2) == 0){delay(delaykeypad);KEY = 0; return 3 ; }
if(digitalRead(baris3) == 0){delay(delaykeypad);KEY = 0; return 2 ; }
if(digitalRead(baris4) == 0){delay(delaykeypad);KEY = 0; return 1; }
//==========================================

KEY = 1 ;
}
}

//================

void bacaKeypad()
{
int Loop = 1 ;
int inputData=0;
inputData=0;
lcd.setCursor(0, 1);
while(Loop)
{
y = key();
if( y < 10 ) // ambil data 0~9 simpan di inputData
{
inputData = (inputData*10)+y ;
Serial.print(y);
lcd.print(y);
delay(300);
}
else if(y == ‘*’)
{ lcd.setCursor(0, 0);
lcd.clear();

y = 0 ;
Loop = 0 ; //keluar loop2
delay(300);
return ;
}
else if(y == ‘#’) //jika ditekan tanda “#”
{ lcd.setCursor(0, 2);
lcd.print(“anda ketik:”);
lcd.setCursor(0, 3);
lcd.print(inputData);

y = 0 ;
Loop = 0 ; //keluar loop2
delay(300);
return ;
}
} //end while loop
}

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 29/10/2016, in Arduino. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: