#include "Password.h" 
#include "Keypad.h" 
#include "Wire.h"

#define REMOTE_LED_PIN 113          // Pin 2 on remote

Password password = Password( "123");

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'F','0','E','D'}
};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 2, 3, 4, 5 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 6, 7, 8, 9 }; 

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

void setup(){

  //Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT); 
  for(int i=0;i<=6;i++)
  {
  digitalWrite(11,HIGH);digitalWrite(10,HIGH);
  delay(5);
  digitalWrite(11,LOW);digitalWrite(10,LOW);
  }
  Wire.begin();
}

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

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
  //Serial.print("Pressed: ");
  //Serial.println(eKey);
  switch (eKey){
    case 'F': checkPassword(); break;
    case 'E': password.reset(); break;
    default: password.append(eKey);
     }
  }
}

void checkPassword(){
  if (password.evaluate()){
    //Serial.print("Success");
    digitalWrite(11,HIGH); //Green Light ON
    digitalWrite(10,LOW);  //Red Light OFF
    expansionWrite(113,HIGH);
    //Add code to run if it works

    digitalWrite(12,HIGH);//These Lines will Rotate the Motor
    digitalWrite(13,LOW); //in Clockwise Direction

    delay(1000); //Delay between the door opening and closing

    digitalWrite(11,LOW); //Green LED OFF
    digitalWrite(10,LOW); //Red LED OFF

    digitalWrite(12,LOW);//These lines will rotate the 
    digitalWrite(13,HIGH); //motor anticlockwise.

    delay(1000);

    digitalWrite(12,LOW);//These lines will rotate the 
    digitalWrite(13,LOW); //motor anticlockwise.

  }else{
    //Serial.print("Wrong");
    digitalWrite(10,HIGH); //Red LED ON
    digitalWrite(11,LOW);  

    expansionWrite(113,LOW);
    //add code to run if it did not work
    delay(500);
    digitalWrite(11,LOW); //Red LED OFF
    digitalWrite(10,LOW);    
  }
}

void expansionWrite(int pin, int value)
{
  pin = pin-100;  // substracts 100 so it maps to the real ports on the expansion arduino
  Wire.beginTransmission(2); // transmit to device #2
  Wire.write(pin);              // sends one byte stating the pin to be addressed
  Wire.write(value);          // sends the value to be transmitted to the pin selected
  Wire.endTransmission();    // stop transmitting
}

#define REMOTE_LED_PIN 113          // Pin 2 on remote
Password password = Password( "123");
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'F','0','E','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 2, 3, 4, 5 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 6, 7, 8, 9 }; 
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
  //Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT); 
  for(int i=0;i<=6;i++)
  {
  digitalWrite(11,HIGH);digitalWrite(10,HIGH);
  delay(5);
  digitalWrite(11,LOW);digitalWrite(10,LOW);
  }
  Wire.begin();
}
void loop(){
  keypad.getKey();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
  //Serial.print("Pressed: ");
  //Serial.println(eKey);
  switch (eKey){
    case 'F': checkPassword(); break;
    case 'E': password.reset(); break;
    default: password.append(eKey);
     }
  }
}
void checkPassword(){
  if (password.evaluate()){
    //Serial.print("Success");
    digitalWrite(11,HIGH); //Green Light ON
    digitalWrite(10,LOW);  //Red Light OFF
    expansionWrite(113,HIGH);
    //Add code to run if it works

    digitalWrite(12,HIGH);//These Lines will Rotate the Motor
    digitalWrite(13,LOW); //in Clockwise Direction

    delay(1000); //Delay between the door opening and closing

    digitalWrite(11,LOW); //Green LED OFF
    digitalWrite(10,LOW); //Red LED OFF

    digitalWrite(12,LOW);//These lines will rotate the 
    digitalWrite(13,HIGH); //motor anticlockwise.

    delay(1000);

    digitalWrite(12,LOW);//These lines will rotate the 
    digitalWrite(13,LOW); //motor anticlockwise.

  }else{
    //Serial.print("Wrong");
    digitalWrite(10,HIGH); //Red LED ON
    digitalWrite(11,LOW);  

    expansionWrite(113,LOW);
    //add code to run if it did not work
    delay(500);
    digitalWrite(11,LOW); //Red LED OFF
    digitalWrite(10,LOW);    
  }
}
void expansionWrite(int pin, int value)
{
  pin = pin-100;  // substracts 100 so it maps to the real ports on the expansion arduino
  Wire.beginTransmission(2); // transmit to device #2
  Wire.write(pin);              // sends one byte stating the pin to be addressed
  Wire.write(value);          // sends the value to be transmitted to the pin selected
  Wire.endTransmission();    // stop transmitting
}