#include /* Keypadlock * * Control a gate locker / unlocker mechanism with a keypad * and a button. * * Keeps track of locked vs unlock position * */ #include // Create servo objects for later reference Servo myservo; Servo myservo2; const int ButtonPin = 12; // the number of the pushbutton pin const int Servo1Pin = 9; // the number of the first servo pin const int Servo2Pin = 10; // the number of the second servo pin // Server values for Locked and Unlocked positions // Servo 1 const int Locked = 69; const int Unlocked = 92; // Servo 2 const int Locked2 = 52; const int Unlocked2 = 72; // How large is your keypad? const byte ROWS = 4; // Four rows const byte COLS = 3; // Three columns // Define the Keymap char keys[ROWS][COLS] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; char lastfour[5] = " "; // initialize our character string (array) that holds the last 4 keys pressed // List of unlock codes char unlockcode[5] = "1234"; char Guestunlockcode[5] = "1111"; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. byte rowPins[ROWS] = { 5, 4, 3, 2 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins. byte colPins[COLS] = { 8, 7, 6 }; // Create the Keypad Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Variables to hold the button state: int ButtonState = 0; // variable for reading the pushbutton status int LockState = 0; // variable for saving the lock state. Locked = 1, Unlocked = 0 void setup() { // pinMode(ledpin,OUTPUT); // digitalWrite(ledpin, HIGH); // Serial.begin(9600); // attach the servos to their pins myservo.attach(Servo1Pin); myservo2.attach(Servo1Pin); // lock the servos by default myservo.write(Locked); myservo2.write(Locked2); // Serial.println("We begin!"); // Initialize the unlock / lock button (goes inside the gate) pinMode(ButtonPin, INPUT); // Initialize the lock state - locked when started LockState = 1; } void loop() { char key = kpd.getKey(); if(key) // Check for a valid key. { // Move the values up the string to make room for our keypress lastfour[0] = lastfour[1]; lastfour[1] = lastfour[2]; lastfour[2] = lastfour[3]; lastfour[3] = key; // Serial.println(key); // Serial.println(lastfour); switch (key) { case '*': // Serial.println("Locked"); myservo.write(Locked); delay(500); myservo2.write(Locked2); LockState = 1; break; case '#': // Serial.println("Locked"); myservo.write(Locked); delay(500); myservo2.write(Locked2); LockState = 1; break; } // check to see if our keypress resulted in a valid code if (checker(lastfour,unlockcode) ==1 or checker(lastfour,Guestunlockcode)==1) { myservo.write(Unlocked); delay(500); myservo2.write(Unlocked2); // Serial.println("Open Sesame!"); lastfour[3] = ' '; LockState = 0; } } // Pushbutton Controls ButtonState = digitalRead(ButtonPin); // Check to see if the button was pressed. if (ButtonState == HIGH) { if (LockState == 0) { // Lock was previously unlocked. Lock it. myservo.write(Locked); delay(500); myservo2.write(Locked2); LockState = 1; } else { // Lock was previously locked. Unlock it. myservo.write(Unlocked); delay(500); myservo2.write(Unlocked2); LockState = 0; } delay(2000); // Don't accept any other input for another 2 seconds. } } // procedure used to check the code entered vs our unlock codes int checker(char input[],char check[]) { int i,result=1; for(i=0;input[i]!='\0' && check[i]!='\0';i++){ if(input[i]!=check[i]){ result=0; break; } } return result; }