Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <EEPROM.h>
- struct Key
- {
- int row, col;
- };
- // Keyboard pins
- int KeyBoard7 = 4;
- int KeyBoard6 = 5;
- int KeyBoard5 = 6;
- int KeyBoard4 = 7;
- int KeyBoard3 = 12;
- int KeyBoard2 = 8;
- int KeyBoard1 = 9;
- //Misc Pins
- int setButton = A1;
- int ledGreen = A3;
- int ledYellow = A5;
- int ledRed = A4;
- int buzzerPin = A2;
- int solenoidPin = A0;
- //Keyboard inits
- int c, r;
- int row[] = {KeyBoard2, KeyBoard3, KeyBoard5, KeyBoard7};
- int col[] = {KeyBoard1, KeyBoard4, KeyBoard6};
- int code[4];
- int KeyMap[4][3] = {
- {1, 2, 3},
- {4, 5, 6},
- {7, 8, 9},
- {11, 0, 12}};
- int C = 11;
- int E = 12;
- //Load Code From EEPROM
- void LoadCode()
- {
- Serial.println("Loading Code");
- for (int i = 0; i < 4; i++)
- {
- code[i] = EEPROM.read(i);
- Serial.print(code[i]);
- }
- Serial.println();
- }
- //Setup
- void setup()
- {
- // Serial
- Serial.begin(9600);
- // Keypad pins
- pinMode(KeyBoard6, INPUT_PULLUP);
- pinMode(KeyBoard4, INPUT_PULLUP);
- pinMode(KeyBoard1, INPUT_PULLUP);
- pinMode(KeyBoard7, OUTPUT);
- pinMode(KeyBoard5, OUTPUT);
- pinMode(KeyBoard3, OUTPUT);
- pinMode(KeyBoard2, OUTPUT);
- for (r = 0; r < 4; r++)
- {
- digitalWrite(row[r], HIGH);
- }
- //Set LEdpins
- pinMode(ledGreen, OUTPUT);
- pinMode(ledYellow, OUTPUT);
- pinMode(ledRed, OUTPUT);
- //Set pin
- pinMode(setButton, INPUT_PULLUP);
- pinMode(solenoidPin, OUTPUT);
- digitalWrite(ledGreen, 0);
- digitalWrite(ledYellow, 0);
- digitalWrite(ledRed, 0);
- //Load code
- LoadCode();
- }
- //Reads Key Coords from Keypad
- Key keyPad()
- {
- for (r = 0; r < 4; r++)
- {
- digitalWrite(row[r], LOW);
- for (c = 0; c < 3; c++)
- {
- if (digitalRead(col[c]) == LOW)
- {
- digitalWrite(row[r], HIGH);
- return Key{r, c};
- }
- }
- digitalWrite(row[r], HIGH);
- }
- return Key{9, 9};
- }
- //Reads Key Coords to 4 integers
- void readKeyPad(int keyInput[])
- {
- for (int i = 0; i < 4; i++)
- {
- keyInput[i] = 0;
- }
- Serial.println("Reading Code");
- Key key = keyPad();
- int wait = 1;
- int i1 = 0;
- while (i1 < 4)
- {
- key = keyPad();
- if (key.row != 9 && key.col != 9)
- {
- keyInput[i1] = KeyMap[key.row][key.col];
- if (keyInput[i1] == C){
- digitalWrite(ledRed, 1);
- delay(100);
- digitalWrite(ledRed, 0);
- break;
- }
- if (keyInput[i1] == E && wait == 0)
- {
- wait = 1;
- }
- if (wait == 0)
- {
- wait = 1;
- digitalWrite(ledYellow, 1);
- Serial.print(i1);
- Serial.print(" ");
- Serial.print(keyInput[i1]);
- Serial.print(" ");
- Serial.print(wait);
- Serial.println();
- tone(buzzerPin,750);
- i1++;
- }
- }
- else if (wait == 1)
- {
- noTone(buzzerPin);
- delay(10);
- wait = 0;
- digitalWrite(ledYellow, 0);
- }
- }
- digitalWrite(ledYellow, 0);
- for (int i = 0; i < 4; i++)
- {
- Serial.print(keyInput[i]);
- }
- Serial.println();
- noTone(buzzerPin);
- }
- //Changes Code[] to keyInput and save it to EEPROM
- void changeCode()
- {
- Serial.println("Changing Code");
- int keyInput[4];
- readKeyPad(keyInput);
- for (int i = 0; i < 4; i++)
- {
- EEPROM.write(i, keyInput[i]);
- Serial.print(keyInput[i]);
- }
- Serial.println();
- LoadCode();
- }
- //Compares keyInput with Code[]
- int checkCode()
- {
- int keyInput[4];
- readKeyPad(keyInput);
- int correctCode = 1;
- for (int i = 0; i < 4; i++)
- {
- if (keyInput[i] != code[i])
- {
- correctCode = 0;
- }
- }
- if (correctCode)
- {
- Serial.println("Code is Correct");
- return 1;
- }
- else
- {
- Serial.println("Code is Incorrect");
- return 0;
- }
- }
- //Unlocks Safe With Buzzer sounds
- void unlockSafe(int duration)
- {
- //Serial.println("00");
- digitalWrite(solenoidPin, HIGH);
- delay(duration);
- digitalWrite(solenoidPin, LOW);
- }
- //Main Loop
- void loop()
- {
- Key key = keyPad();
- int keyChar;
- if (key.row != 9 && key.col != 9)
- {
- //Serial.println(KeyMap[key.row][key.col]);
- keyChar = KeyMap[key.row][key.col];
- }
- if (keyChar == E)
- {
- //tone(pin, frequency, duration)
- if (checkCode())
- {
- digitalWrite(ledGreen, 1);
- tone(buzzerPin, 1000);
- unlockSafe(2000);
- digitalWrite(ledGreen, 0);
- noTone(buzzerPin);
- }
- else
- {
- digitalWrite(ledRed, 1);
- tone(buzzerPin, 100);
- delay(100);
- digitalWrite(ledRed, 0);
- delay(100);
- digitalWrite(ledRed, 1);
- delay(100);
- digitalWrite(ledRed, 0);
- tone(buzzerPin, 100);
- delay(100);
- digitalWrite(ledRed, 1);
- delay(100);
- digitalWrite(ledRed, 0);
- delay(100);
- digitalWrite(ledRed, 1);
- delay(100);
- digitalWrite(ledRed, 0);
- delay(100);
- digitalWrite(ledRed, 1);
- delay(100);
- digitalWrite(ledRed, 0);
- delay(100);
- digitalWrite(ledRed, 1);
- delay(100);
- digitalWrite(ledRed, 0);
- noTone(buzzerPin);
- }
- }
- if (!digitalRead(setButton))
- {
- changeCode();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement