Password Vault Coding

Password Vault Logic

The Password Vault sketch does three basic things:

  1. When turned on, it remains locked until you enter the unlock code
  2. Allows you to select one of your ten saved passwords
  3. "Types" your password over USB into a computer

Arduino Sketch

This is the complete Password Vault sketch. You can download it from the link below. 

Download: file
// ---------------------------------------------------------------------------
//Circuit Playground Password Vault
// John Park
// for Adafruit Industries
//
//Password Vault stores up to ten passwords, uses a code sequence to unlock, 
//can type your long passwords for you!
//
//    Plug into any computer with a USB cable
//    Flip the slide swith and then tap your secret code sequence on the 
//    capacitive pads
//    If wrong code is entered, buzzer will sound and red LED will blink.
//    When code is sucessfully entered, the NeoPixels will light green.
//    Now, you can choose one of up to ten stored passwords with the right 
//    button, you'll see a corresponing NeoPixel light up for your selection
//    Press the left button and your password will be typed by the Circuit 
//    Playground over USB into the password field (be sure to click there with 
//    your mouse first so the field has focus)
//
// For use with the Adafruit Circuit Playground.
//
// MIT license, check LICENSE for more information
//
//
// ---------------------------------------------------------------------------

#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>
#include <Keyboard.h>

//*******************************************************
//Create your own secret six-digit unlock sequence here.
//The sequence can be any combination of six touches 
//on capacitive pads #1, #3, #10, & #12
//*******************************************************
//Alter the line below to change the sequence
int codeSeq[] = { 1, 3, 10, 12, 10, 10}; 

//This array is the placeholder that will change as the code is entered,
//when codeEntry[] matches codeSeq[], sucess
int codeEntry[] = { 0, 0, 0, 0, 0, 0};

//*******************************************************
//Enter your passwords here,
//up to ten of them:
//*******************************************************

char* PASSWDS[] = {
  "..R45p83rrY.." ,
  "p3Nc1l.6{fG3$;" ,
  "[email protected]/&" ,
  "P0intS^[F4f8?" ,
  "DoUBLeA87*+F]" ,
  "?F3ErPsS.C.M.O.D.S." ,
  "mK$Bv7M;?gyFu2" ,
  "[email protected](WV[vq6N" ,
  "HorseStapleBattery" ,
  "bLk3y3d4p1R4t3,)" } ;

//-----------------------------------------------------------------------------
//Global Variables
bool codePrompt = 0;  //readiness to enter code
bool codeBegun = 0; //code entry has begun
bool codeCompare = 1;//compare code entry and correct code
bool passPrompt = 0; //ready to select password slot
bool passPrep=0; //password entry state prep
int passSlot = 0; //current password slot choice
bool passKeyed = 0; //password has been keyed in over USB
int n = 0; //code entry array position


void setup() {
  Keyboard.begin(); //allows Circuit Playground to act as USB HID device
  CircuitPlayground.begin();
  CircuitPlayground.playTone(932,200);  //startup beep (A# just like an
  //Apple IIe)
  int i=0; //flash the red LED
  for (i=0;i<2;i++) {
    CircuitPlayground.redLED(true);
    delay(200);
    CircuitPlayground.redLED(false);
    delay(200); 
  }
  CircuitPlayground.setBrightness(15);   //fairly dim pixel brightness
  //setting -- goes up to a blinding 255!
  CircuitPlayground.clearPixels(); //clean up if any were previously on
}


void loop() {
  
  //-------------------------------------------------------------------------
  //Slider check.
  //read slider to see if it is time to ender code
  if(codeBegun==0){
    bool previousSliderState =  CircuitPlayground.slideSwitch(); //get the
    //initial state of the slider switch as a reference to compare when it changes
    delay(20); //wait a moment  
    bool sliderState      = CircuitPlayground.slideSwitch(); //check slider
    //again
  
    if ((sliderState == previousSliderState)&&(codeBegun == 0)) {
      return; //no change, do nothing
    }
    else if ((sliderState != previousSliderState)&&(codeBegun == 0)) { //the
      // slider was moved
      //light the ring of NeoPixels
      int i;
      for(i=0;i<10;i++){
        CircuitPlayground.setPixelColor(i, 255, 0, 0);
        delay(100);
      } 
      codeBegun = 1;//flip this so code entry will be accepted
      previousSliderState=sliderState; //set these the same again so they
      //can detect future switches
      codePrompt = 1; //flip this so the code prompt will occur
    } 
  }
  //-------------------------------------------------------------------------
  //Code entry.
  //Allow unlock code to be entered using capacitive pads
  if ((codePrompt == 0)&&(passPrompt==0)) { //do nothing until code prompt flips
    return;
  }
  else if ((codePrompt == 1)&&(passPrompt==0)) {//check the cap switches for entry
    int cap1=CircuitPlayground.readCap(1);
    int cap3=CircuitPlayground.readCap(3);
    int cap10=CircuitPlayground.readCap(10);
    int cap12=CircuitPlayground.readCap(12);
    
    //check the cap switches
    if (cap1<25 && cap3<25 && cap10<25 && cap12<25) {
      return;
    }
    else if (cap1>25) {
      codeEntry[n]=1;//add code entry to the array
      //light a pixel yellow
      CircuitPlayground.setPixelColor(n,255,255,0);
      n++;
      delay(400);
    }   
    else if (cap3>25) {
      codeEntry[n]=3;//add code entry to the array
      //light a pixel yellow
      CircuitPlayground.setPixelColor(n,255,255,0);
      n++;
      delay(400);
    }
    else if (cap10>25) {
      codeEntry[n]=10;//add code entry to the array
      //light a pixel yellow
      CircuitPlayground.setPixelColor(n,255,255,0);
      n++;
      delay(400);
      }   
    else if (cap12>25) {
      codeEntry[n]=12;//add code entry to the array
      //light a pixel yellow
      CircuitPlayground.setPixelColor(n,255,255,0);
      n++;
      delay(400);
    }
  }

  //-------------------------------------------------------------------------
  //Code check.
  //Six digits have been entered, test if they're the correct ones
  if ((n<6)&&(passPrompt==0) ) {
    return;
  }
  else if((n>=6)&&(passPrompt==0)) {
    //memcmp compares two arrays
    codeCompare = memcmp(codeSeq, codeEntry, sizeof(codeSeq)); //0 = match
    if (codeCompare==0) {
      CircuitPlayground.playTone(632,100);  //beep
      delay(200);
      CircuitPlayground.playTone(632,100);  //beep
      //light up green
      int i;
      for (i=0;i<10;i++) {
        CircuitPlayground.setPixelColor(i, 0, 255, 0);
        delay(100);
        codePrompt=0;//to stop taking entries
        passPrompt = 1; //ready to pick password slot
      }
    }
    else {
      CircuitPlayground.playTone(232,400);  //beep
      n=0;//reset counter for entry
      //reset pixels to red
      int i;
      for (i=0;i<10;i++) {
        CircuitPlayground.setPixelColor(i, 255, 0, 0);
        delay(100);
      } 
    }
  }

  //-------------------------------------------------------------------------
  //Password selection prompt.
  //Ready for button input
  if (passPrompt == 0) { //not ready to pick password slot
    return;
  }
  else if ((passPrompt==1)&&(passKeyed==0)&&(passPrep==0)){ //ready to pick a password slot
    if (passPrep==0){
          CircuitPlayground.setPixelColor(passSlot, 255, 255, 255); //light slot 0
          passPrep=1 ; //flip this so we move on
      }
    }

    else {
      //-----------------------------------------------------------------
      // Check for button presses.
      // Detect if the left or right button is released by taking two
      // readings with a small delay in between.  If the button changes
      // state from pressed -> released then we can update indicators.
      bool leftFirst = CircuitPlayground.leftButton();
      bool rightFirst = CircuitPlayground.rightButton();
      delay(10);
      bool leftSecond = CircuitPlayground.leftButton();
      bool rightSecond = CircuitPlayground.rightButton();

      //-----------------------------------------------------------------
      //Right button selects password slot.
      if (rightFirst && !rightSecond) {
        passSlot++; //increments the password slot
        CircuitPlayground.setPixelColor(passSlot-1, 0, 255, 0); //fill in the previous pixel to green again
        CircuitPlayground.setPixelColor(passSlot, 255, 255, 255);
        
        if (passSlot > 9) { //rolls over from tenth slot to first
          passSlot=0;
          CircuitPlayground.setPixelColor(passSlot,255,255,255);
        }
      }

      //-----------------------------------------------------------------
      //Left button prints the password over USB.
      if (leftFirst && !leftSecond){
        CircuitPlayground.playTone(432,400);
        delay(100);
        CircuitPlayground.playTone(500,300);
        CircuitPlayground.setPixelColor(passSlot, 0, 0, 255);
        Keyboard.print(PASSWDS[passSlot]) ;
        passKeyed = 1;//password has been printed over USB
      }
    }
}

Download the Code

You can press the button below to download the code. Unzip and move the directory CircuitPlaygroundClassScheduler to your Arduino sketch directory. 

Adjust the code

Before uploading the code to your Circuit Playground, you'll want to change a the unlock sequence and the actual passwords you want to store and use. Open the CircuitPlaygroundPasswordVault.ino file in the Arduino IDE, and then make changes to these two sections shown below.

Download: file
//*******************************************************
//Create your own secret six-digit unlock sequence here.
//The sequence can be any combination of six touches 
//on capacitive pads #1, #3, #10, & #12
//*******************************************************
//Alter the line below to change the sequence
int codeSeq[] = { 1, 3, 10, 12, 10, 10}; 
Download: file
//*******************************************************
//Enter your passwords here,
//up to ten of them:
//*******************************************************

char* PASSWDS[] = {
  "..R45p83rrY.." ,
  "p3Nc1l.6{fG3$;" ,
  "[email protected]/&" ,
  "P0intS^[F4f8?" ,
  "DoUBLeA87*+F]" ,
  "?F3ErPsS.C.M.O.D.S." ,
  "mK$Bv7M;?gyFu2" ,
  "[email protected](WV[vq6N" ,
  "Tt.fV2Vf.D[8sb" ,
  "bLk3y3d4p1R4t3,)" } ;

Save It

Once you've adjusted those sections, save the sketch. In the next section, you'll upload it to the Circuit Playground.

Upload Code to the Board

Plug your Circuit Playground into your computer with the USB cable. 

In the Arduino IDE, select the Adafruit Circuit Playground item in the menu Tools > Board.

Then, select the proper port in Tools > Ports.

Press the reset button twice on the Circuit Playground so that the red #13 LED begins pulsing, indicating it is ready to be programmed.

In the Arduino IDE, click Sketch > Upload

The board will blink, and after a few second the new code will be uploaded to the board.

The board will blink the red #13 LED twice and beep when the sketch starts.

You'll build the enclosure in the next section.

This guide was first published on Sep 01, 2016. It was last updated on Sep 01, 2016. This page (Password Vault Coding) was last updated on Nov 14, 2019.