Components and supplies
Breadboard (generic)
Resistor 330 ohm
Servos (Tower Pro MG996R)
Capacitor 100 µF
Jumper wires (generic)
Pushbutton switch 12mm
Standard LCD - 16x2 White on Blue
9V battery (generic)
9V Battery Clip
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
1/* 2Made by Marco Fusco 3May 20, 2016 4Contact me at: marcofusco111@gmail.com 5*/ 6 7 8#include <LiquidCrystal.h> 9#include <EEPROM.h> 10#include <Servo.h> 11 12int address = 0; 13static unsigned long SaveTimer; 14static unsigned long SaveDelay = (30 * 1000); 15 16 17char CODE[10] = "1234E"; 18char Str[10]; 19char CodeLength = 4; 20int Pos = 0; 21bool Unlocked; 22static unsigned long DisplayTimer; 23static unsigned long DisplayDelay = 200; 24 25LiquidCrystal lcd(12, 11, 9, 8, 7, 6); 26 27 28int buttonPin1 = 2; 29int buttonPin2 = 3; 30int buttonPin3 = 4; 31int buttonPin4 = 5; 32 33int enterbutton = 10; 34int clearlockbutton = 13; 35 36Servo myServo; 37 38void setup() { 39 40 myServo.attach(A1); 41 42 int EEPROMCodeOK = true; 43 for (Pos = 0; Pos <= (CodeLength); Pos++) { 44 Str[Pos] = EEPROM.read(Pos); 45 if (!(strrchr("1123456789", Str[Pos]))) { 46 // not a valid code 47 EEPROMCodeOK = false; 48 } 49 } 50 Pos++; 51 Str[Pos] = EEPROM.read(Pos); 52 if (Str[CodeLength + 1] != 'E') EEPROMCodeOK = false; 53 if (EEPROMCodeOK) { 54 Str[CodeLength + 2] = '\\0'; 55 strncpy(CODE, Str, CodeLength + 1); 56 } 57 ClearCode(); 58 59 pinMode(buttonPin1, INPUT_PULLUP); 60 pinMode(buttonPin2, INPUT_PULLUP); 61 pinMode(buttonPin3, INPUT_PULLUP); 62 pinMode(buttonPin4, INPUT_PULLUP); 63 64 pinMode(enterbutton, INPUT_PULLUP); 65 pinMode(clearlockbutton, INPUT_PULLUP); 66 67 lcd.begin(16, 2); 68 lcd.setCursor(0, 0); 69 lcd.print("Hello."); 70 delay(2000); 71 lcd.clear(); 72 lcd.setCursor(0, 0); 73 lcd.print("Password:"); 74 75 DisplayTimer = millis() + 200; 76} 77 78 79void loop() { 80 81 Lock(); 82 83 Pos = constrain(Pos, 0, CodeLength); 84 85 int buttonState1 = digitalRead(buttonPin1); 86 int buttonState2 = digitalRead(buttonPin2); 87 int buttonState3 = digitalRead(buttonPin3); 88 int buttonState4 = digitalRead(buttonPin4); 89 90 int clButtonState = digitalRead(clearlockbutton); 91 int enterButtonState = digitalRead(enterbutton); 92 93 lcd.setCursor(9, 0); 94 95 if (buttonState1 == LOW) { 96 Str[Pos] = '1'; 97 Pos++; 98 Str[Pos] = '\\0'; 99 delay(250); 100 while (digitalRead(buttonPin1) == LOW); 101 102 } 103 104 else if (buttonState2 == LOW) { 105 Str[Pos] = '2'; 106 Pos++; 107 Str[Pos] = '\\0'; 108 delay(250); 109 while (digitalRead(buttonPin2) == LOW); 110 111 } 112 113 else if (buttonState3 == LOW) { 114 Str[Pos] = '3'; 115 Pos++; 116 Str[Pos] = '\\0'; 117 delay(250); 118 while (digitalRead(buttonPin3) == LOW); 119 } 120 121 else if (buttonState4 == LOW) { 122 Str[Pos] = '4'; 123 Pos++; 124 Str[Pos] = '\\0'; 125 delay(250); 126 while (digitalRead(buttonPin4) == LOW); 127 128 } 129 else if (enterButtonState == LOW) { 130 Str[Pos] = 'E'; 131 Pos++; 132 Str[Pos] = '\\0'; 133 delay(250); 134 while (digitalRead(buttonPin1) == LOW); 135 if (strcmp (Str,CODE) == 0) { 136 Unlocked = true; 137 lcd.setCursor(0, 0); 138 lcd.print(" Access Granted"); 139 delay(2000); 140 lcd.clear(); 141 lcd.print(" Unlocked"); 142 } 143 else if (SaveTimer > millis() && (Pos + 1) == CodeLength) { 144 145 strcpy(CODE, Str); 146 for (Pos = 0; Pos <= (CodeLength + 1); Pos++) { 147 EEPROM.write(Pos, Str[Pos]); 148 } 149 lcd.setCursor(0, 0); 150 lcd.print("Saving Code:"); 151 lcd.setCursor(0, 1); 152 lcd.print(Str); 153 154 Unlocked = true; 155 } 156 157 else { 158 159 lcd.clear(); 160 lcd.print(" Access Denied."); 161 delay(2000); 162 lcd.clear(); 163 lcd.print("Password:"); 164 165 } 166 167 while (Unlocked) { 168 Unlock(); 169 if (digitalRead(clearlockbutton) == LOW) { 170 delay(200); 171 lcd.clear(); 172 lcd.print(" Locked"); 173 delay(2000); 174 lcd.clear(); 175 Unlocked = false; 176 SaveTimer = millis() + 30000; 177 } 178 } 179 180 ClearCode(); 181 182 183 } 184 185 else if (clButtonState == LOW) { 186 delay(500); 187 188 while (clearlockbutton == LOW); 189 if ((millis() - SaveTimer) > 4500) { 190 191 } 192 193 ClearCode(); 194 195 } 196 197 if ( (long)( millis() - DisplayTimer ) >= 0) { 198 DisplayTimer += DisplayDelay; 199 lcd.setCursor(9, 0); 200 lcd.print(Str); 201 lcd.print(" "); 202 203 } 204} 205 206void ClearCode() { 207 208 Pos = 0; 209 Str[Pos] = '\\0'; 210 lcd.setCursor(0, 0); 211 lcd.print("Password:"); 212 lcd.setCursor(0, 1); 213 lcd.print(" "); 214 215} 216 217void Unlock() { 218 219 myServo.write(150); 220 221} 222 223void Lock() { 224 225 myServo.write(50); 226 227} 228 229
Code
arduino
1/* 2Made by Marco Fusco 3May 20, 2016 4Contact me at: marcofusco111@gmail.com 5*/ 6 7 8#include <LiquidCrystal.h> 9#include <EEPROM.h> 10#include <Servo.h> 11 12int address = 0; 13static unsigned long SaveTimer; 14static unsigned long SaveDelay = (30 * 1000); 15 16 17char CODE[10] = "1234E"; 18char Str[10]; 19char CodeLength = 4; 20int Pos = 0; 21bool Unlocked; 22static unsigned long DisplayTimer; 23static unsigned long DisplayDelay = 200; 24 25LiquidCrystal lcd(12, 11, 9, 8, 7, 6); 26 27 28int buttonPin1 = 2; 29int buttonPin2 = 3; 30int buttonPin3 = 4; 31int buttonPin4 = 5; 32 33int enterbutton = 10; 34int clearlockbutton = 13; 35 36Servo myServo; 37 38void setup() { 39 40 myServo.attach(A1); 41 42 int EEPROMCodeOK = true; 43 for (Pos = 0; Pos <= (CodeLength); Pos++) { 44 Str[Pos] = EEPROM.read(Pos); 45 if (!(strrchr("1123456789", Str[Pos]))) { 46 // not a valid code 47 EEPROMCodeOK = false; 48 } 49 } 50 Pos++; 51 Str[Pos] = EEPROM.read(Pos); 52 if (Str[CodeLength + 1] != 'E') EEPROMCodeOK = false; 53 if (EEPROMCodeOK) { 54 Str[CodeLength + 2] = '\\0'; 55 strncpy(CODE, Str, CodeLength + 1); 56 } 57 ClearCode(); 58 59 pinMode(buttonPin1, INPUT_PULLUP); 60 pinMode(buttonPin2, INPUT_PULLUP); 61 pinMode(buttonPin3, INPUT_PULLUP); 62 pinMode(buttonPin4, INPUT_PULLUP); 63 64 pinMode(enterbutton, INPUT_PULLUP); 65 pinMode(clearlockbutton, INPUT_PULLUP); 66 67 lcd.begin(16, 2); 68 lcd.setCursor(0, 0); 69 lcd.print("Hello."); 70 delay(2000); 71 lcd.clear(); 72 lcd.setCursor(0, 0); 73 lcd.print("Password:"); 74 75 DisplayTimer = millis() + 200; 76} 77 78 79void loop() { 80 81 Lock(); 82 83 Pos = constrain(Pos, 0, CodeLength); 84 85 int buttonState1 = digitalRead(buttonPin1); 86 int buttonState2 = digitalRead(buttonPin2); 87 int buttonState3 = digitalRead(buttonPin3); 88 int buttonState4 = digitalRead(buttonPin4); 89 90 int clButtonState = digitalRead(clearlockbutton); 91 int enterButtonState = digitalRead(enterbutton); 92 93 lcd.setCursor(9, 0); 94 95 if (buttonState1 == LOW) { 96 Str[Pos] = '1'; 97 Pos++; 98 Str[Pos] = '\\0'; 99 delay(250); 100 while (digitalRead(buttonPin1) == LOW); 101 102 } 103 104 else if (buttonState2 == LOW) { 105 Str[Pos] = '2'; 106 Pos++; 107 Str[Pos] = '\\0'; 108 delay(250); 109 while (digitalRead(buttonPin2) == LOW); 110 111 } 112 113 else if (buttonState3 == LOW) { 114 Str[Pos] = '3'; 115 Pos++; 116 Str[Pos] = '\\0'; 117 delay(250); 118 while (digitalRead(buttonPin3) == LOW); 119 } 120 121 else if (buttonState4 == LOW) { 122 Str[Pos] = '4'; 123 Pos++; 124 Str[Pos] = '\\0'; 125 delay(250); 126 while (digitalRead(buttonPin4) == LOW); 127 128 } 129 else if (enterButtonState == LOW) { 130 Str[Pos] = 'E'; 131 Pos++; 132 Str[Pos] = '\\0'; 133 delay(250); 134 while (digitalRead(buttonPin1) == LOW); 135 if (strcmp (Str,CODE) == 0) { 136 Unlocked = true; 137 lcd.setCursor(0, 0); 138 lcd.print(" Access Granted"); 139 delay(2000); 140 lcd.clear(); 141 lcd.print(" Unlocked"); 142 } 143 else if (SaveTimer > millis() && (Pos + 1) == CodeLength) { 144 145 strcpy(CODE, Str); 146 for (Pos = 0; Pos <= (CodeLength + 1); Pos++) { 147 EEPROM.write(Pos, Str[Pos]); 148 } 149 lcd.setCursor(0, 0); 150 lcd.print("Saving Code:"); 151 lcd.setCursor(0, 1); 152 lcd.print(Str); 153 154 Unlocked = true; 155 } 156 157 else { 158 159 lcd.clear(); 160 lcd.print(" Access Denied."); 161 delay(2000); 162 lcd.clear(); 163 lcd.print("Password:"); 164 165 } 166 167 while (Unlocked) { 168 Unlock(); 169 if (digitalRead(clearlockbutton) == LOW) { 170 delay(200); 171 lcd.clear(); 172 lcd.print(" Locked"); 173 delay(2000); 174 lcd.clear(); 175 Unlocked = false; 176 SaveTimer = millis() + 30000; 177 } 178 } 179 180 ClearCode(); 181 182 183 } 184 185 else if (clButtonState == LOW) { 186 delay(500); 187 188 while (clearlockbutton == LOW); 189 if ((millis() - SaveTimer) > 4500) { 190 191 } 192 193 ClearCode(); 194 195 } 196 197 if ( (long)( millis() - DisplayTimer ) >= 0) { 198 DisplayTimer += DisplayDelay; 199 lcd.setCursor(9, 0); 200 lcd.print(Str); 201 lcd.print(" "); 202 203 } 204} 205 206void ClearCode() { 207 208 Pos = 0; 209 Str[Pos] = '\\0'; 210 lcd.setCursor(0, 0); 211 lcd.print("Password:"); 212 lcd.setCursor(0, 1); 213 lcd.print(" "); 214 215} 216 217void Unlock() { 218 219 myServo.write(150); 220 221} 222 223void Lock() { 224 225 myServo.write(50); 226 227} 228 229
Downloadable files
Diagram
Diagram
Diagram
Diagram
Comments
Only logged in users can leave comments
chummer1010
0 Followers
•0 Projects
Table of contents
Intro
68
0