├── .gitattributes ├── Circuit └── circuit.png ├── README.md └── arduino_code └── arduino_code.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Circuit/circuit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HighVoltages/Arduino-Push-Button-Combination-Lock/b0e08eae88fea999f2225f38f9fecb454647a1f7/Circuit/circuit.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Push Button Combination Lock 2 | 3 | Keypad Modules are expensive so to relace them in this video i am going to show you how you can make combinational lock using push button and Arduino. You will need Arduino, 8x LEDs, 6X Buttons, breadboard and jumper wires. 4 | -------------------------------------------------------------------------------- /arduino_code/arduino_code.ino: -------------------------------------------------------------------------------- 1 | const int button1 = 2; //first button is on pin 8 2 | const int button2 = 3; //second is on pin 9 3 | const int button3 = 4; //third is pin 10 4 | const int button4 = 5; //fourth is pin 11 5 | const int button5 = 6; //third is pin 10 6 | const int button6 = 7; //fourth is pin 11 7 | const int LED[] = {14,15,16,17,18,19}; 8 | 9 | const int Red = 8; //red LED is on pin 4 10 | const int greenLed = 9; //green LED is pin 12 11 | void checkEntered1(int button); 12 | 13 | int code[] = {6,5,5,4,3,2}; //the desired code is entered in this array, 14 | //separated by commas 15 | 16 | int entered[7]; //create a new empty array for the code entered by 17 | //the user (has 4 elements) 18 | 19 | void setup(){ //run once at sketch startup 20 | Serial.begin(9600); //begin Serial 21 | 22 | pinMode(button1, INPUT_PULLUP); //button 1 is an input 23 | pinMode(button2, INPUT_PULLUP); //button 2 is an input 24 | pinMode(button3, INPUT_PULLUP); //button 3 is an input 25 | pinMode(button4, INPUT_PULLUP); //button 4 is an input 26 | pinMode(button5, INPUT_PULLUP); //button 3 is an input 27 | pinMode(button6, INPUT_PULLUP); //button 4 is an input 28 | 29 | pinMode(Red, OUTPUT); //the red LED is an output 30 | pinMode(greenLed, OUTPUT); // the green LED is an output 31 | // setupLights(); //run the setupLights routine 32 | // setupLights(); //run it again 33 | // delay(650); //delay (only for effect, :P not needed) 34 | digitalWrite(Red, LOW); //turn the red LED on 35 | for (int i = 0; i < 6;i++){ //work through numbers 0-3 36 | Serial.println(code[i]); //print each digit of the code 37 | Serial.println(entered[i]); //print each element of the entered[] 38 | //array (this was for me to check that it 39 | //started at 0 40 | pinMode(LED[i],OUTPUT); 41 | } 42 | } 43 | 44 | void loop(){ //run repeatedly 45 | if (digitalRead(button1) == LOW){ //if button1 is pressed 46 | checkEntered1(1); //call checkEntered and pass it a 1 47 | 48 | delay(250);//wait, needed for correct functioning, otherwise 49 | //buttons are deemed to be pressed more than once 50 | 51 | } 52 | else if (digitalRead(button2) == LOW){ //if button2 is pressed 53 | checkEntered1(2); //call checkEntered1 and pass it a 2 54 | 55 | delay(250); //wait 56 | 57 | } 58 | else if (digitalRead(button3) == LOW){ //if button3 is pressed 59 | checkEntered1(3); //call checkEntered1 and pass it a 3 60 | 61 | delay(250); //wait 62 | 63 | } 64 | else if (digitalRead(button4) == LOW){ //if button4 is pressed 65 | checkEntered1(4); //call checkEntered1 and pass it a 4 66 | 67 | delay(250); //wait 68 | 69 | } 70 | else if (digitalRead(button5) == LOW){ //if button4 is pressed 71 | checkEntered1(5); //call checkEntered1 and pass it a 4 72 | 73 | delay(250); //wait 74 | 75 | } 76 | else if (digitalRead(button6) == LOW){ //if button4 is pressed 77 | checkEntered1(6); //call checkEntered1 and pass it a 4 78 | 79 | delay(250); //wait 80 | 81 | } 82 | 83 | 84 | } 85 | 86 | void checkEntered1(int button){ //check the first element of the entered[] array 87 | digitalWrite(LED[button-1],HIGH); 88 | if (entered[0] != 0){ //if it is not a zero, i.e. it has already been inputted 89 | checkEntered2(button); //move on to checkEntered2, passing it "button" 90 | } 91 | 92 | else if(entered[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet 93 | entered[0] = button; //set the first element as the button that has been pressed 94 | Serial.print("1: ");Serial.println(entered[0]); //for debugging 95 | } 96 | 97 | } 98 | 99 | void checkEntered2(int button){ //check the second element of the entered[] array 100 | digitalWrite(LED[button-1],HIGH); 101 | if (entered[1] != 0){ //if it is not a zero, i.e. it has already been inputted 102 | checkEntered3(button); //move on to checkEntered3, passing it "button" 103 | } 104 | 105 | else if(entered[1] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet 106 | entered[1] = button; //set the second element as the button that has been pressed 107 | Serial.print("2: ");Serial.println(entered[1]); //for debugging 108 | } 109 | 110 | } 111 | 112 | void checkEntered3(int button){ //check the third element of the entered[] array 113 | digitalWrite(LED[button-1],HIGH); 114 | if (entered[2] != 0){ //if it is not a zero, i.e. it has already been inputted 115 | checkEntered4(button); //move on to checkEntered4, passing it "button" 116 | } 117 | 118 | else if (entered[2] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet 119 | entered[2] = button; //set the third element as the button that has been pressed 120 | Serial.print("3: ");Serial.println(entered[2]); //for debugging 121 | } 122 | 123 | } 124 | 125 | void checkEntered4(int button){ //check the third element of the entered[] array 126 | digitalWrite(LED[button-1],HIGH); 127 | if (entered[3] != 0){ //if it is not a zero, i.e. it has already been inputted 128 | checkEntered5(button); //move on to checkEntered4, passing it "button" 129 | } 130 | 131 | else if (entered[3] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet 132 | entered[3] = button; //set the third element as the button that has been pressed 133 | Serial.print("4: ");Serial.println(entered[3]); //for debugging 134 | } 135 | 136 | } 137 | 138 | 139 | void checkEntered5(int button){ //check the third element of the entered[] array 140 | digitalWrite(LED[button-1],HIGH); 141 | if (entered[4] != 0){ //if it is not a zero, i.e. it has already been inputted 142 | checkEntered6(button); //move on to checkEntered4, passing it "button" 143 | } 144 | 145 | else if (entered[4] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet 146 | entered[4] = button; //set the third element as the button that has been pressed 147 | Serial.print("5: ");Serial.println(entered[4]); //for debugging 148 | } 149 | 150 | } 151 | 152 | void checkEntered6(int button){ //check the fourth element of the entered[] array 153 | digitalWrite(LED[button-1],HIGH); 154 | if (entered[5] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet 155 | entered[5] = button; //set the final element as the button that has been pressed 156 | Serial.print("6: ");Serial.println(entered[5]); //for debugging 157 | delay(100); //allow time for processing 158 | compareCode(); //call the compareCode function 159 | } 160 | } 161 | 162 | void compareCode(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array 163 | for (int i = 0; i<6;i++){ //these three lines are for debugging 164 | Serial.println(entered[i]); 165 | } 166 | if ((entered[0]==code[0]) && (entered[1]==code[1]) && (entered[2]==code[2]) && (entered[3]==code[3]) && (entered[4]==code[4])&& (entered[5]==code[5])){ //if all the elements of each array are equal 167 | digitalWrite(Red, LOW); // turn the red LED off 168 | digitalWrite(greenLed, HIGH); //turn the green LED on 169 | delay(1000); //wait for a bit 170 | digitalWrite(greenLed, LOW); //turn the green LED off 171 | 172 | 173 | 174 | for (int i = 0; i < 7; i++){ //this next loop is for debugging 175 | entered[i] = 0; 176 | 177 | } 178 | 179 | loop(); //return to loop() (not really necessary) 180 | } 181 | 182 | else { //if you (or the intruder) get the code wrong 183 | 184 | digitalWrite(Red,HIGH); 185 | delay(1000); 186 | digitalWrite(Red,LOW); 187 | Serial.println("Red OFF"); 188 | for (int i = 0; i < 7; i++){ //this next loop is for debugging 189 | entered[i] = 0; 190 | 191 | } 192 | 193 | } 194 | close_all(); 195 | } 196 | 197 | 198 | 199 | void close_all(){ 200 | digitalWrite(LED[0],LOW); 201 | digitalWrite(LED[1],LOW); 202 | digitalWrite(LED[2],LOW); 203 | digitalWrite(LED[3],LOW); 204 | digitalWrite(LED[4],LOW); 205 | digitalWrite(LED[5],LOW); 206 | } 207 | --------------------------------------------------------------------------------