Rabu, 16 April 2014

ARDUINO - Keypad & Password Example Control LED indikator

#include <Password.h>
#include <Keypad.h>

int indikatorOK=13;
Password password = Password( "2644272" );

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] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(9600);
  Serial.println("Perhatikan '*' untuk Konfirmasi '#' untuk Reset.");
  Serial.println("\nMasukkan Password Anda Sekarang : ");
  keypad.addEventListener(keypadEvent);
  pinMode(indikatorOK, OUTPUT);
}

void loop()
{
  keypad.getKey();
}

void keypadEvent(KeypadEvent eKey)
{
  switch (keypad.getState())
  {
    case PRESSED:
    //Serial.print("Pressed: ");
    Serial.print(eKey);
    switch (eKey)
        {
      case '*': checkPassword(); break;
      case '#': password.reset(); break;
      default: password.append(eKey);
        }
  }
}

void checkPassword()
{
  if (password.evaluate())
  {
    Serial.println("\nPassword yang Anda masukkan Benar");
    digitalWrite(indikatorOK, HIGH);
  }
  else
  {
    Serial.println("\nPassword yang Anda masukkan Salah, Silakan diulangi !");
    digitalWrite(indikatorOK, LOW);
  }
}

Tidak ada komentar:

Posting Komentar