Rabu, 16 April 2014

ARDUINO - Keypad & Password Example Control Servo & LED indikator

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

Servo myservo;
int pos = 0;

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

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("Tekan '*' untuk Konfirmasi, '#' untuk Reset");
  Serial.println("\nMasukkan Password : ");
  keypad.addEventListener(keypadEvent);
  myservo.attach(9);
  pinMode(indikatorOK, OUTPUT);
}

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

void keypadEvent(KeypadEvent eKey)
{
  switch (keypad.getState())
  {
    case 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);
     for(pos = 178; pos < 180; pos += 1)
  {
    myservo.write(pos);
    delay(15);
  }
  }
  else
  {
    Serial.println("\nPassword yang Anda masukkan Salah, Silakan diulangi !");
    digitalWrite(indikatorOK, LOW);
    for(pos = 3; pos>=1; pos-=1)
  {                               
    myservo.write(pos);
    delay(15);
  }
  }
}

Tidak ada komentar:

Posting Komentar