Anda di halaman 1dari 4

Kami ingin menyalakan LED dan memutar servo ke posisi tertentu, setelah

memasukkan kode tiga digit kanan pada keypad. Kait dapat dilampirkan ke servo, mis. untuk
membuka kunci dan mengunci pintu.
Peralatan yang diperlukan: Arduino / Breadboard / Keypad (4 × 4 dalam contoh ini) /
kabel / satu LED merah / satu LED hijau / dua resistor 100 Ohm / servo
Hanya jika kita memasukkan kode yang tepat pada keypad, kita ingin LED hijau
menyala dan mengubah servo ke posisi tertentu (kunci terbuka). Jika kunci ditutup, LED merah
akan menyala dan servo akan beralih ke posisi lain. Tutorial ini bisa menjadi saran untuk proyek
yang lebih besar, seperti kunci pintu atau brankas misalnya.
Mari kita mulai dengan pengaturan. Perkabelan dalam tutorial ini tidak rumit dan
diperlihatkan dalam sketsa fritzing di bawah ini.
Petunjuk: Anda dapat menemukan angka 1 dan 7 di kontak terluar papan tombol.
Kontak dengan nomor 1 terhubung ke pin 2 di Arduino. Satu demi satu keypad dihubungi ke
Arduino, sampai kontak dari keypad, dengan angka 7 di atasnya, terhubung ke pin 8 pada
Arduino.
Untuk menjalankan keypad dengan arduino, kita memerlukan perpustakaan, yang harus
ditambahkan ke perangkat lunak arduino.
Buka perangkat lunak arduino> pilih "Sketsa"> "Sertakan Perpustakaan"> "Kelola Perpustakaan
.."> cari setelah "keypad" dengan bilah pencarian di atas> pilih perpustakaan pertama oleh Mark
Stanley dan instal perpustakaan.
Mulai sekarang kita dapat menggunakan perpustakaan keypad dalam kode kita.

CODING:

#include <Keypad.h> //Include Keypad and servo library

#include <Servo.h>

Servo servoblue; //The servo is called „servoblue“ from now on

char* password = „123“; //We set the password. In this case


„123“

int position = 0;

const byte ROWS = 4; //In this two lines we define how many rows
and columns

const byte COLS = 3; //our keypad has

char keys[ROWS][COLS] = { //The characters on the keys are


defined here

{‚#‘, ‚0‘, ‚*‘},

{‚9‘, ‚8‘, ‚7‘},

{‚6‘, ‚5‘, ‚4‘},

{‚3‘, ‚2‘, ‚1‘}

};

byte rowPins[ROWS] = {5, 6, 7, 8}; //The connection with the


arduino is

byte colPins[COLS] = {2, 3, 4}; //listed here

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins,


ROWS, COLS );
int redLED = 12; //The red LED is connected to pin 12

int greenLED = 13; //The green LED is connected to pin 13

void setup()

pinMode(redLED, OUTPUT); //The LEDs are defined as Output

pinMode(greenLED, OUTPUT);

servoblue.attach(11); //The servo is connected to pin 11

setLocked(true);

void loop()

char key = keypad.getKey();

if (key == ‚*‘ || key == ‚#‘) If the lock is open it can be


locked again by pushing „*“ or „#“ on the //keypad

position = 0;

setLocked(true); //The command to close the lock after „*“ or


„#“ is pushed

if (key == password[position])

position ++;

if (position == 3) //This part defines how many digits our code


will have.In this case we have 3 digits //(123).
{

setLocked(false);

delay(100);

void setLocked(int locked)

if (locked) // If the lock is closed..

digitalWrite(redLED, HIGH); //..the red LED should light up..

digitalWrite(greenLED, LOW); //..the green LED not..

servoblue.write(90); //and the servo should turn to a 90 degree


position.

else //if the lock is open..

digitalWrite(redLED, LOW); //..the red LED should be off..

digitalWrite(greenLED, HIGH); //..and the green LED should light


up..

servoblue.write(0); //..and the servo should turn to a 0 degree


position.

Anda mungkin juga menyukai