top of page

Code


#include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip //tells to use password library
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip  //tells to use keypad library

​

const int ledGreenPin = 11;
const int solenoid = 13;
const int buttonPin = 12;
const int buttonClose = 10;
const int buzzer = 1; //buzzer to arduino pin 9


Password password = Password("1234"); //password to unlock, can be changed

​

const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] =
{
{'D','#','0','*'},
{'C','9','8','7'},
{'B','6','5','4'},
{'A','3','2','1'}
};
byte rowPins[ROWS] = {6,7,8,9};  //connect to the row pinouts of the keypad
byte colPins[COLS] = {2,3,4,5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup() {
        Serial.begin(9600);
        delay(200);
        pinMode(ledGreenPin, OUTPUT);  //green light
        pinMode(solenoid, OUTPUT);  //lock
        pinMode(buttonPin, INPUT_PULLUP);
        pinMode(buttonClose, INPUT_PULLUP);
        digitalWrite(ledGreenPin, LOW);
        digitalWrite(solenoid, LOW);
        pinMode(buzzer, OUTPUT);
 
 
       keypad.setDebounceTime(50);
        keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

 

void loop() {
         keypad.getKey();
              if ( digitalRead(buttonPin ) == LOW ) {
                     activateLock();
                     delay(1000);
              }
  }

​

         //take care of some special events
void keypadEvent(KeypadEvent eKey) {
            switch (keypad.getState()) {
                     case PRESSED:

                     Serial.print("Enter:");
                     Serial.println(eKey);
                     delay(10);

      

            switch (eKey) {
                     case '*': checkPassword(); delay(1); break;

                    //case '#': password.reset(); delay(1); break;

                    default: password.append(eKey); delay(1);
             }
             }
}


void checkPassword() {

             if (password.evaluate()) { //if password is right open

                     Serial.println("Accepted");
                    delay(10);
                    activateLock();
                    password.reset(); delay(1);
            }

​

            else {
                    Serial.println("Denied"); //if passwords wrong keep locked
                    delay(10);
                    password.reset(); delay(1);
            }
}

​

void activateLock() {
            digitalWrite(solenoid, HIGH);//turn on
            Serial.println("OPEN");
            delay(5000);
            digitalWrite(solenoid, LOW);

 

while ( digitalRead(buttonPin) == HIGH && digitalRead(buttonClose) == HIGH) {
             }

            digitalWrite(solenoid, HIGH);//turn on
            Serial.println("CLOSED");
            delay(5000);
            digitalWrite(solenoid, LOW);
  }

​

​

​

​

​

​

bottom of page