clbwright
Published

LED Lock Box

A lock box using a combination of 6 colors and 5 numbers in each combination "dial" - of which there are 3.

IntermediateShowcase (no instructions)624
LED Lock Box

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Resistor 10k ohm
Resistor 10k ohm
×4
Resistor 100 ohm
Resistor 100 ohm
×2
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
Breadboard (generic)
Breadboard (generic)
×2
NeoPixel strip
NeoPixel strip
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×3
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×4

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Schematics

LED Lockbox circuit diagram

Code

LED Lockbox code

Arduino
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(15, PIN, NEO_GRB + NEO_KHZ800);

int aPot; // integer values for the potentiometer readings
int bPot;
int cPot;

int aPos; // integer values for the placement of each light, corresponding to an array of lights
int bPos;
int cPos;

uint32_t aColor = strip.Color(25, 0, 0); // this defines the color of the lights. We start with RGB, but it can be changed if the user wants to start the board with different lights, just make sure to adjust
uint32_t bColor = strip.Color(0, 25, 0);        // the counters in rows 29-31 to reflect each color, so red = 1, green = 2, blue = 3, yellow = 4, purple = 5, teal = 6
uint32_t cColor = strip.Color(0, 0, 25);

uint32_t aKey = strip.Color(25, 0, 0); // this defines the correct colors for each row of lights to unlock the box
uint32_t bKey = strip.Color(0, 25, 0);
uint32_t cKey = strip.Color(0, 0, 25);

int aNumKey = 4; // each number here will be one less than the number on the console, so 4 is the max. 
int bNumKey = 2; // these are the correct number values for the light positions for each row. You must get the proper position and color for each light to unlock the box
int cNumKey = 3;

int aCounter = 1;  // the value for the color number you are on. 1 is red, 2 is green, 3 - blue, 4 - yellow, 5 - purple, 6 - teal
int bCounter = 2;
int cCounter = 3;

void setup() {
  pinMode(13, INPUT); // define pins as inputs for buttons
  pinMode(12, INPUT);
  pinMode(8, INPUT);
  pinMode(7, INPUT);
  Serial.begin(9600);
  strip.begin(); // start the strip colors
}

void loop() {

  aPot = analogRead(A2); // read analog pins and assign them to potentiometer values
  bPot = analogRead(A1);
  cPot = analogRead(A0);
  if (digitalRead(8) == HIGH) {         // if the first button, or button A, is pressed, 
    aColor = setColor(8, aCounter);     // our color for aColor is set using our setColor function, using the pin, 8, and aCounter, our counter for which color is currently selected
    if (aCounter != 6) {  // if aCounter is not 6, we increment the counter
      aCounter++; // This will increment the counter from whatever it is to one higher. This way, when we get in a value of 0 from our setColor function, this will set it to 1. 
    }
    else {                // if it is 6, we need to re-set it to 1, because we do not want the light to roll over into the next light
      aCounter = 1;
    }
  }
  if (digitalRead(12) == HIGH) { // same but for button attached to 12 pin (b button)
    bColor = setColor(12, bCounter);
    if (bCounter != 6) {
      bCounter++;
    }
    else {
      bCounter = 1;
    }
  } 
  if (digitalRead(13) == HIGH) {// same but for button attached to 13 pin (c button)
    cColor = setColor(13, cCounter);
    if (cCounter != 6) {
      cCounter++;
    }
    else {
      cCounter = 1;
    }
  }
  aPos = map(aPot, 0, 1030, 0, 5); // Map a position integer to be the same as the potentiometer readings mapped from 0 to 5, because of how the potentiometer reads, we can never get to 5, so it will give 5 values (0-4)
  bPos = map(bPot, 0, 1030, 0, 5) + 5; // same for b position and c position
  cPos = map(cPot, 0, 1030, 0, 5) + 10;
  
  setPosition(aPos, aColor, 0, 5); // Set the position of the first light
  setPosition(bPos, bColor, 5, 10); // set position of the second light
  setPosition(cPos, cColor, 10, 15); // set position of the third light

  if (digitalRead(7) == HIGH && aColor == aKey && bColor == bKey && cColor == cKey && aPos == aNumKey && (bPos -5) == bNumKey && (cPos - 10) == cNumKey) { // turns on the Green LED if the code is correct
    digitalWrite(4, HIGH); // Turn on the Green LED
    digitalWrite(2, LOW); // Turn off the Red LED
  }
  else if(digitalRead(7) == HIGH && aColor != aKey || bColor != bKey || cColor != cKey || aPos != aNumKey || (bPos - 5) != bNumKey || (cPos - 10) != cNumKey){ // Turns on the Red LED if the code is incorrect
    digitalWrite(4, LOW); // Turn off the Green LED
    digitalWrite(2, HIGH); // Turn on the Red LED
  }

}

void setPosition(int potPosition, uint32_t stripColor, int startIndex, int endIndex) { // takes in the reading for the positon of the potentiometer, the color, start index for the row of lights, and the end index for the lights
  strip.setPixelColor(potPosition, stripColor); // set the position read by the potentiometer (which will be mapped to 0 through 4), to the color specified in parameters. The color of the light is changed in setColor
  for (int i = startIndex; i < endIndex; i++) { // sort through the rest of the lights in that row
    if (i != potPosition) {                     // as long as those lights are not in the same position as the light we just changed...
      strip.setPixelColor(i, strip.Color(0, 0, 0)); // turn them off. 
    }
  }
  strip.show(); // display our new configuration
}
uint32_t setColor(int pinNum, int counter) { // takes in values for what pin number the function will be reading from, and the counter value of that current color. Each color correlates to a number from 0-5 (6 colors)
  uint32_t color;
  if (digitalRead(pinNum) == HIGH) { // for a function, parameters need to be pin number, counter for that button, and color for that pin
    if (counter == 1) {
      color = strip.Color(0, 25, 0);
    }
    if (counter == 2) {
      color = strip.Color(0, 0, 25);
    }
    if (counter == 3) {
      color = strip.Color(25, 25, 0);
    }
    if (counter == 4) {
      color = strip.Color(25, 0, 25);
    }
    if (counter == 5) {
      color = strip.Color(0, 25, 25);
    }
    if (counter == 6) {
      counter = 0;              // this is set to 0, because our value will be incremented outside of this function. For example, line 50 increments this color from 0 to 1. 
      color = strip.Color(25, 0, 0);
    }
    delay(150); // This makes sure that we can delay for a bit to let the button be released. 
  }
  return color;
}

Credits

clbwright
1 project • 0 followers

Comments