   }
 
 
-  
-  if(!digitalRead(SLOT_SELECT)){// Select memory slot
-    while(!digitalRead(SLOT_SELECT));
-    memory_slot = (memory_slot < TOTAL_SLOTS - 1) ? memory_slot + 1 : 0;
-    #ifdef DEBUG
-    Serial.print("Slot selected: ");
-    Serial.println(memory_slot);
-    #endif
-    blink_n_times(memory_slot+1,STATUS_LED);
-  }
   if(!digitalRead(LOCK_UNLOCK)){// Select slot
     state = lock(!state);
   }
-}
-
-boolean save_delete_card(uint8_t *uid,int addr){
+  if(!digitalRead(RESET_MEMORY)){// Select slot
+    digitalWrite(STATUS_LED,HIGH);
+    delay(5000);
+    digitalWrite(STATUS_LED,LOW);
+    if(!digitalRead(RESET_MEMORY)){
+      clear_memory();
+      blink_n_times(4,STATUS_LED,100);
+      delay(1000);
+    }
+  }
+}
+
+int check_card(uint8_t *uid){
+  /* 
+   *  Checks if card is saved, returns the address of the card if it saved
+   *  or -1 if it is not found.
+   */
+  int i,j;
+  int memory_offset;
+
+  for(i=0;i<TOTAL_SLOTS;i++){
+    memory_offset = i*SLOT_SIZE;
+    for(j=0;j<SLOT_SIZE;j++){
+      if(EEPROM.read(memory_offset+j)!=uid[j]) break;
+    }
+    if(j==SLOT_SIZE){
+      return i;
+    }
+  }
+  return -1;
+}
+int save_card(uint8_t *uid){
+  /* 
+   *  Saves card to empty slot
+   */
+  uint8_t blank_card[] = {0,0,0,0};
+  int card_addr = check_card(uid);// Search card
+  int i, new_addr, memory_offset;
+  if(card_addr == -1){
+    new_addr = check_card(blank_card);// Search for a empty slot
+    if(new_addr != -1){
+      memory_offset = SLOT_SIZE*new_addr;
+      for(i=0;i<SLOT_SIZE;i++){
+        EEPROM.write(memory_offset+i,uid[i]);
+      }
+    }
+    return new_addr;
+  }
+  return -1;// Already saved
+
+}
+int delete_card(int addr){
+  /* 
+   *  Deletes card from memory slot (addr)
+   */
+  int memory_offset = SLOT_SIZE*addr;
+  int i;
+
+  for(i=0;i<SLOT_SIZE;i++){
+    EEPROM.write(memory_offset+i,0);
+  }
+}
+void clear_memory(void){
+  int i;
+  int total = TOTAL_SLOTS*SLOT_SIZE;
+  for(i=0;i<total;i++){
+    EEPROM.write(i,0);
+  }
+}
+void blink_n_times(int n,int led_pin,int blink_interval){
   /*
-   * If card doesn't exist in any slot, then it is saved in "addr", 
-   *   else it is deleted from memory.
-   */
-  int i;
-  int j;
-  int memory_offset = SLOT_SIZE*addr;
-  boolean save = 1;
-  
-  for(i=0;i<TOTAL_SLOTS;i++){ //Check if card is saved in any slot
-    if(check_card(uid,i)){
-      save = 0; //Delete if found
-    }
-  }
-  if(save){
-    for(i=0;i<SLOT_SIZE;i++){
-      EEPROM.write(memory_offset+i,uid[i]);
-    }
-    return(1);
-  }else{
-    for(i=0;i<TOTAL_SLOTS;i++){ //Delete card from all slots
-      if(check_card(uid,i)){
-        memory_offset = SLOT_SIZE*i;
-        for(j=0;j<SLOT_SIZE;j++){
-          EEPROM.write(memory_offset+j,0);
-        }
-      }
-    }
-    return(0);
-  }
-}
-boolean check_card(uint8_t *uid,int addr){
-  /* 
-   *  Checks if card (uid) is contained in the designed address (addr)
-   */
-  int i;
-  int memory_offset = SLOT_SIZE*addr;
-  for(i=0;i<SLOT_SIZE;i++){
-    if(EEPROM.read(memory_offset+i)!=uid[i]) return 0;
-  }
-  return 1;
-}
-void blink_n_times(int n,int led_pin){
-  /*
-   * Blink n times
+   * Blink led_pin n times for blink_interval
    */
   int i;
   for(i=0;i<n;i++){
     digitalWrite(led_pin,HIGH);
-    delay(250);
+    delay(blink_interval);
     digitalWrite(led_pin,LOW);
-    delay(250);
+    delay(blink_interval);
   }
 }
 



void setup(void) {
  pinMode(MEMORY_BUTTON,INPUT_PULLUP);
  pinMode(SLOT_SELECT,INPUT_PULLUP);
  pinMode(STATUS_LED,OUTPUT);
  pinMode(LOCKED,OUTPUT);
  pinMode(UNLOCKED,OUTPUT);

    files in https://github.com/devalfrz/arduino-lockitron-nfc).

    Buttons: (buttons are set to have a pullup resistor so they ar inverted)
      - LOCK_UNLOCK: This button will lock or unlock the system depending on its
          previous state.
          
      - SAVE_CARD: If it's pressed for 3 seconds while a card is read,
          it will either save the current card and blink 3 times or delete the
          card and blink one time for 2 seconds.
          
      - RESET_MEMORY: Keep this button pressed and this will clear all cards in
          memory.
          

*/
/**************************************************************************/

/**** INPUTS/OUTPUTS ****/
// Adafruit PN532 breakout board:
#define PN532_SCK  A4
#define PN532_MOSI A3
#define PN532_SS   A2
#define PN532_MISO A5
// Buttons
#define RESET_MEMORY 10
#define SAVE_CARD 9
#define STATUS_LED 13
#define UNLOCKED 12
#define LOCKED 11
