Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Yall need jesus -Danny Kozbial
- */
- #include <SPI.h>
- #include <MFRC522.h>
- // Libraries for OLED display
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- // DEFINES
- // Provides debugging information over serial connection if defined
- #define DEBUG
- // OLED display pin
- #define OLED_RESET 4
- // RFID sensor pins
- #define SS_PIN 10
- #define RST_PIN 9
- // Relay pin used to secure a lock when set to HIGH and release when LOW
- #define LOCK_PIN 8
- // Button pin
- #define BUTTON_PIN 7
- // CONSTANTS
- // Total count of authorized users
- const byte totalAuthorized = 4;
- // An array of "authorized" NFC tag IDs required to solve the puzzle
- const String authorizedIDs[totalAuthorized] = {"46126C89", "54C97889", "40807D89", "4E006989"};
- const byte priyaArray[4] = {78, 0, 105, 137};
- // An array of usernames corresponding to NFC tag IDs
- const String userNames[totalAuthorized] = {"Estella", "Mario", "Devin", "Priya"};
- // An array mapping of LEDs corresponding to NFC tag IDs
- const byte indicatorLights[totalAuthorized] = {6, 5, 2, 3};
- // Total count of override tags
- const byte totalOverride = 2;
- // An array of "master unlock" NFC tag IDs that can override the puzzle and unlock the puzzle
- const String overrideIDs[totalOverride] = {"5C3851C3", "E3BB4583"};
- // GLOBALS
- // Initialise MFRC522 instance for RFID reader
- MFRC522 rfid(SS_PIN, RST_PIN);
- // Initialize OLED display
- Adafruit_SSD1306 display(OLED_RESET);
- // An array representing the state of whether each ID has been scanned
- bool lock[totalAuthorized] = { true, true, true, true };
- // Index of next indicator light to turn on.
- int lightIndex = 0;
- // Whether lock has been unlocked
- boolean unlocked = false;
- // Stores previous id
- String previousID = "";
- void setup() {
- #ifdef DEBUG
- // Initialise serial communications channel with the PC
- Serial.begin(9600);
- Serial.println(F("Serial communication started"));
- #endif
- // initialize the pushbutton pin as an input:
- pinMode(BUTTON_PIN, INPUT);
- // Set the lock pin as output and secure the lock
- pinMode(LOCK_PIN, OUTPUT);
- digitalWrite(LOCK_PIN, HIGH);
- for(int i = 0; i < totalAuthorized; i++) {
- pinMode(indicatorLights[i], OUTPUT);
- digitalWrite(indicatorLights[i], LOW);
- }
- // Initialize the SPI bus
- SPI.begin();
- /*
- // Initialise the display with the I2C addr 0x3D (for the 128x64)
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- // Display text to display indicating lock status
- display.clearDisplay();
- display.display();
- display.setTextColor(WHITE);
- display.setTextSize(2);
- display.setCursor(10,0);
- display.print("SONIA ON");
- display.display();
- */
- rfid.PCD_Init();
- #ifdef DEBUG
- Serial.println(F("--- END SETUP ---"));
- #endif
- }
- void loop() {
- delay(1000);
- #ifdef DEBUG
- Serial.println(F("Scan Started"));
- #endif
- if ( !rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial() ) {
- #ifdef DEBUG
- Serial.println("No Card");
- #endif
- return;
- }
- for (int i = 0; i <
- String cardID = dump_byte_array(rfid.uid.uidByte, rfid.uid.size);
- #ifdef DEBUG
- Serial.print("UID tag: " );
- Serial.println(cardID);
- #endif
- if (cardID == "") {
- #ifdef DEBUG
- Serial.print("Empty");
- #endif
- return;
- }
- if ( cardID == previousID ) {
- return;
- }
- previousID = cardID;
- //Override Check
- for (int i = 0; i < totalOverride; i++) {
- if ( overrideIDs[i] == cardID) {
- #ifdef DEBUG
- Serial.println(F("Override Detected"));
- #endif
- overrideCommand();
- return;
- }
- }
- for (int i = 0; i < totalAuthorized; i++) {
- if ( authorizedIDs[i] == cardID) {
- #ifdef DEBUG
- Serial.println(F("Key Detected"));
- #endif
- keyCommand(i);
- }
- }
- if (unlocked) {
- // Check if button is pressed.
- int buttonState = digitalRead(BUTTON_PIN);
- if (buttonState == HIGH) {
- onButtonPress();
- }
- }
- }
- void(* resetFunc) (void) = 0; //declare reset function @ address 0
- String dump_byte_array(byte *buffer, byte bufferSize) {
- String read_rfid = "";
- for (byte i = 0; i < bufferSize; i++) {
- Serial.println(i);
- Serial.print(buffer[i]);
- read_rfid.concat(buffer[i] < 0x10 ? "0" : "");
- read_rfid.concat(String(buffer[i], DEC));
- }
- read_rfid.toUpperCase();
- return read_rfid;
- }
- void overrideCommand() {
- printText("", "OVERRIDE", 2000);
- #ifdef DEBUG
- Serial.print(F("Override key scanned: "));
- #endif
- onUnlock();
- delay(10000);
- resetFunc();
- }
- void keyCommand(int i) {
- lock[i] = false;
- digitalWrite(lightIndex++, HIGH);
- printText("Welcome", userNames[i], 2000);
- #ifdef DEBUG
- Serial.print(F("Found match: "));
- Serial.println(userNames[i]);
- #endif
- // Check if all users have now been authorized
- int authorizedCount = 0;
- for (int i = 0; i < totalAuthorized; i++) {
- if (!lock[i]) {
- authorizedCount++;
- }
- }
- #ifdef DEBUG
- Serial.print(F("Total authorized: "));
- Serial.println(authorizedCount);
- #endif
- if (authorizedCount == totalAuthorized) {
- unlocked = true;
- onUnlock();
- }
- }
- void onUnlock()
- {
- #ifdef DEBUG
- // Print debugging message
- Serial.println(F("Box unlocked."));
- #endif
- // Release the lock
- digitalWrite(LOCK_PIN, LOW);
- unlocked = true;
- }
- void onButtonPress()
- {
- #ifdef DEBUG
- // Print debugging message
- Serial.println(F("Puzzle Solved!"));
- #endif
- display.display();
- display.setTextColor(BLACK);
- display.setTextSize(2);
- display.setCursor(10,0);
- display.print("SONIA ON");
- display.display();
- display.setTextColor(WHITE);
- display.setTextSize(2);
- display.setCursor(10,0);
- display.print("SONIA OFF");
- display.display();
- }
- void printText(String line1, String line2, int mSec) {
- display.display();
- display.setTextColor(BLACK);
- display.setTextSize(2);
- display.setCursor(10,0);
- display.print("SONIA ON");
- display.display();
- display.setTextColor(WHITE);
- display.setTextSize(2);
- display.setCursor(10,0);
- display.print(line1);
- display.display();
- display.setTextColor(WHITE);
- display.setTextSize(2);
- display.setCursor(10,30);
- display.print(line2);
- display.display();
- delay(mSec);
- display.setTextColor(BLACK);
- display.setTextSize(2);
- display.setCursor(10,30);
- display.print(line2);
- display.display();
- display.setTextColor(BLACK);
- display.setTextSize(2);
- display.setCursor(10,0);
- display.print(line1);
- display.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement