daily pastebin goal
17%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Yall need jesus -Danny Kozbial
  3. */
  4.  
  5. #include <SPI.h>
  6. #include <MFRC522.h>
  7.  
  8.  
  9. // Libraries for OLED display
  10. #include <Adafruit_GFX.h>
  11. #include <Adafruit_SSD1306.h>
  12.  
  13. // DEFINES
  14. // Provides debugging information over serial connection if defined
  15. #define DEBUG
  16. // OLED display pin
  17. #define OLED_RESET 4
  18. // RFID sensor pins
  19. #define SS_PIN 10
  20. #define RST_PIN 9
  21. // Relay pin used to secure a lock when set to HIGH and release when LOW
  22. #define LOCK_PIN 8
  23. // Button pin
  24. #define BUTTON_PIN 7
  25.  
  26.  
  27. // CONSTANTS
  28. // Total count of authorized users
  29. const byte totalAuthorized = 4;
  30. // An array of "authorized" NFC tag IDs required to solve the puzzle
  31. const String authorizedIDs[totalAuthorized] = {"46126C89", "54C97889", "40807D89", "4E006989"};
  32. const byte priyaArray[4] = {78, 0, 105, 137};
  33. // An array of usernames corresponding to NFC tag IDs
  34. const String userNames[totalAuthorized] = {"Estella", "Mario", "Devin", "Priya"};
  35. // An array mapping of LEDs corresponding to NFC tag IDs
  36. const byte indicatorLights[totalAuthorized] = {6, 5, 2, 3};
  37.  
  38. // Total count of override tags
  39. const byte totalOverride = 2;
  40. // An array of "master unlock" NFC tag IDs that can override the puzzle and unlock the puzzle
  41. const String overrideIDs[totalOverride] = {"5C3851C3", "E3BB4583"};
  42.  
  43.  
  44. // GLOBALS
  45. // Initialise MFRC522 instance for RFID reader
  46. MFRC522 rfid(SS_PIN, RST_PIN);
  47. // Initialize OLED display
  48. Adafruit_SSD1306 display(OLED_RESET);
  49. // An array representing the state of whether each ID has been scanned
  50. bool lock[totalAuthorized] = { true, true, true, true };
  51. // Index of next indicator light to turn on.
  52. int lightIndex = 0;
  53. // Whether lock has been unlocked
  54. boolean unlocked = false;
  55. // Stores previous id
  56. String previousID = "";
  57.  
  58.  
  59.  
  60. void setup() {
  61.   #ifdef DEBUG
  62.   // Initialise serial communications channel with the PC
  63.   Serial.begin(9600);
  64.   Serial.println(F("Serial communication started"));
  65.   #endif
  66.  
  67.  
  68.   // initialize the pushbutton pin as an input:
  69.   pinMode(BUTTON_PIN, INPUT);
  70.  
  71.   // Set the lock pin as output and secure the lock
  72.   pinMode(LOCK_PIN, OUTPUT);
  73.   digitalWrite(LOCK_PIN, HIGH);
  74.   for(int i = 0; i < totalAuthorized; i++) {
  75.     pinMode(indicatorLights[i], OUTPUT);
  76.     digitalWrite(indicatorLights[i], LOW);
  77.   }
  78.  
  79.   // Initialize the SPI bus
  80.   SPI.begin();
  81. /*
  82.   // Initialise the display with the I2C addr 0x3D (for the 128x64)
  83.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  84.  
  85.   // Display text to display indicating lock status
  86.   display.clearDisplay();
  87.   display.display();
  88.   display.setTextColor(WHITE);
  89.   display.setTextSize(2);
  90.   display.setCursor(10,0);
  91.   display.print("SONIA ON");
  92.   display.display();
  93. */
  94.   rfid.PCD_Init();
  95.  
  96.   #ifdef DEBUG
  97.   Serial.println(F("--- END SETUP ---"));
  98.   #endif
  99. }
  100.  
  101.  
  102. void loop() {
  103.     delay(1000);
  104.     #ifdef DEBUG
  105.     Serial.println(F("Scan Started"));
  106.     #endif
  107.     if ( !rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial() ) {
  108.         #ifdef DEBUG
  109.         Serial.println("No Card");
  110.         #endif
  111.         return;
  112.     }
  113.  
  114.     for (int i = 0; i <
  115.     String cardID = dump_byte_array(rfid.uid.uidByte, rfid.uid.size);
  116.     #ifdef DEBUG
  117.     Serial.print("UID tag: " );
  118.     Serial.println(cardID);
  119.    
  120.     #endif
  121.     if (cardID == "") {
  122.         #ifdef DEBUG
  123.         Serial.print("Empty");
  124.         #endif
  125.       return;
  126.     }
  127.     if ( cardID == previousID ) {
  128.       return;
  129.     }
  130.  
  131.     previousID = cardID;
  132.  
  133.     //Override Check
  134.     for (int i = 0; i < totalOverride; i++) {
  135.       if ( overrideIDs[i] == cardID) {
  136.         #ifdef DEBUG
  137.         Serial.println(F("Override Detected"));
  138.         #endif
  139.         overrideCommand();
  140.         return;
  141.       }
  142.     }
  143.  
  144.  
  145.     for (int i = 0; i < totalAuthorized; i++) {
  146.       if ( authorizedIDs[i] == cardID) {
  147.         #ifdef DEBUG
  148.         Serial.println(F("Key Detected"));
  149.         #endif
  150.         keyCommand(i);
  151.       }
  152.     }
  153.  
  154.     if (unlocked) {
  155.       // Check if button is pressed.
  156.       int buttonState = digitalRead(BUTTON_PIN);
  157.       if (buttonState == HIGH) {
  158.         onButtonPress();
  159.       }
  160.     }
  161.  
  162.  
  163. }
  164.  
  165. void(* resetFunc) (void) = 0; //declare reset function @ address 0
  166.  
  167. String dump_byte_array(byte *buffer, byte bufferSize) {
  168.   String read_rfid = "";
  169.   for (byte i = 0; i < bufferSize; i++) {
  170.     Serial.println(i);
  171.     Serial.print(buffer[i]);
  172.     read_rfid.concat(buffer[i] < 0x10 ? "0" : "");
  173.     read_rfid.concat(String(buffer[i], DEC));
  174.   }
  175.   read_rfid.toUpperCase();
  176.   return read_rfid;
  177. }
  178.  
  179. void overrideCommand() {
  180.   printText("", "OVERRIDE", 2000);
  181.   #ifdef DEBUG
  182.   Serial.print(F("Override key scanned: "));
  183.   #endif  
  184.   onUnlock();
  185.   delay(10000);
  186.   resetFunc();
  187. }
  188.  
  189. void keyCommand(int i) {
  190.   lock[i] = false;
  191.   digitalWrite(lightIndex++, HIGH);
  192.   printText("Welcome", userNames[i], 2000);
  193.   #ifdef DEBUG
  194.   Serial.print(F("Found match: "));
  195.   Serial.println(userNames[i]);
  196.   #endif
  197.   // Check if all users have now been authorized
  198.   int authorizedCount = 0;
  199.   for (int i = 0; i < totalAuthorized; i++) {
  200.     if (!lock[i]) {
  201.       authorizedCount++;
  202.     }
  203.   }
  204.   #ifdef DEBUG
  205.   Serial.print(F("Total authorized: "));
  206.   Serial.println(authorizedCount);
  207.   #endif
  208.   if (authorizedCount == totalAuthorized) {
  209.     unlocked = true;
  210.     onUnlock();
  211.   }
  212. }
  213.  
  214.  
  215.  
  216. void onUnlock()
  217. {  
  218.   #ifdef DEBUG
  219.   // Print debugging message
  220.   Serial.println(F("Box unlocked."));
  221.   #endif
  222.  
  223.   // Release the lock
  224.   digitalWrite(LOCK_PIN, LOW);
  225.  
  226.   unlocked = true;
  227. }
  228.  
  229.  
  230.  
  231. void onButtonPress()
  232. {  
  233.   #ifdef DEBUG
  234.   // Print debugging message
  235.   Serial.println(F("Puzzle Solved!"));
  236.   #endif
  237.  
  238.   display.display();
  239.   display.setTextColor(BLACK);
  240.   display.setTextSize(2);
  241.   display.setCursor(10,0);
  242.   display.print("SONIA ON");
  243.   display.display();
  244.  
  245.   display.setTextColor(WHITE);
  246.   display.setTextSize(2);
  247.   display.setCursor(10,0);
  248.   display.print("SONIA OFF");
  249.   display.display();
  250. }
  251.  
  252. void printText(String line1, String line2, int mSec) {
  253.   display.display();
  254.   display.setTextColor(BLACK);
  255.   display.setTextSize(2);
  256.   display.setCursor(10,0);
  257.   display.print("SONIA ON");
  258.   display.display();
  259.  
  260.   display.setTextColor(WHITE);
  261.   display.setTextSize(2);
  262.   display.setCursor(10,0);
  263.   display.print(line1);
  264.   display.display();
  265.  
  266.   display.setTextColor(WHITE);
  267.   display.setTextSize(2);
  268.   display.setCursor(10,30);
  269.   display.print(line2);
  270.   display.display();
  271.  
  272.   delay(mSec);
  273.  
  274.   display.setTextColor(BLACK);
  275.   display.setTextSize(2);
  276.   display.setCursor(10,30);
  277.   display.print(line2);
  278.   display.display();
  279.  
  280.   display.setTextColor(BLACK);
  281.   display.setTextSize(2);
  282.   display.setCursor(10,0);
  283.   display.print(line1);
  284.   display.display();
  285. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top