Description
Use Arduino and Processing to create a virtual combination lock
Components Used
3 Light Emitting Diodes (LED)
3 Resistors
1 Potentiometer
Arduino Code
/*
** Joshua Gomez - Lab 4
**
** This code will attempt to make a POT into a dial for a combination lock
** The LEDs will be used as hints that the correct position has been reached
** The combination is 50-15-30
*/
int potPin = 1; // select the input pin for the potentiometer
int ledPin1 = 9; // select the pins for the LEDs
int ledPin2 = 11;
int ledPin3 = 13;
int input = 0; // variable to store the value coming from the sensor
boolean step1 = false; // variables to keep track of progress
boolean step2 = false;
int val1 = 50; // combo lock settings
int val2 = 15;
int val3 = 30;
void setup() {
pinMode(ledPin1, OUTPUT); // declare the ledPins as an OUTPUT
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);
}
void loop() {
input = analogRead(potPin); // read the value from the sensor between 0-1024
input = input/17; // scale value down to combo lock's range: 0-60
Serial.println(input); // send value to Processing
if (step1==false && input==val1)
{
digitalWrite(ledPin1, HIGH); // if the first number is reached light the first LED
step1 = true;
}
if (step1==true && step2==false && input==val2)
{
digitalWrite(ledPin2, HIGH);
step2 = true;
digitalWrite(ledPin1, LOW);
}
if ( step2==true && input==val3)
{
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin2, LOW);
}
}
Processing Code
/*
* Joshua Gomez - Lab 4
*
* This program will display the face of a combination lock
* The input from the serial port will determine the position of the dial
* When the lock opens and a fun image appears
*/
import processing.serial.*;
Serial port; // Create object from Serial class
String portname = "/dev/ttyUSB0";
int cr = 13; // ASCII carriage return
int lf = 10; // ASCII line feed
String buf = ""; // input buffer
int position = 0; // position of the dial
int numPos = 12; // number of positions on the dial (I use 12 instead of 60 to make it easier)
PImage[] lock = new PImage[numPos + 1]; // array for the images of the combo lock, plus the final image
boolean step1 = false; // variables to keep track of progress
boolean step2 = false;
int val1 = 50; // set up the lock's combination
int val2 = 15;
int val3 = 30;
int prev = 0; // helps keep track of rotation
/****************************************************************************************/
void setup()
{
size(162, 163);
frameRate(10);
lock[0] = loadImage("combo_lock_0.jpg"); //load the animation frames of the lock
lock[1] = loadImage("combo_lock_1.jpg");
lock[2] = loadImage("combo_lock_2.jpg");
lock[3] = loadImage("combo_lock_3.jpg");
lock[4] = loadImage("combo_lock_4.jpg");
lock[5] = loadImage("combo_lock_5.jpg");
lock[6] = loadImage("combo_lock_6.jpg");
lock[7] = loadImage("combo_lock_7.jpg");
lock[8] = loadImage("combo_lock_8.jpg");
lock[9] = loadImage("combo_lock_9.jpg");
lock[10] = loadImage("combo_lock_10.jpg");
lock[11] = loadImage("combo_lock_11.jpg");
lock[12] = loadImage("happy.jpg");
image(lock[0], 0, 0); // load the first image
port = new Serial(this, portname, 9600);
}
void draw()
{
}
void SerialEvent(Serial p)
{
int c = port.read();
println("test");
if (c != lf && c != cr)
{
buf += char(c);
}
if (c == lf)
{
String input = buf;
println(input);
position = int(input);
int frame = position/5;
image(lock[frame], 0, 0);
if(step1==false && position==val1 && prev<=position)
step1=true;
if(step1==true && step2==false && position==val2 && prev>=position)
step2=true;
if(step2==true && position==val3 && prev<=position)
{
image(lock[12], 0, 0); // when step 3 is reached show the happy face
step1=false; // reset the lock
step2=false;
}
prev = position;
buf = "";
}
}
Item
ComboLock Arduino
Combo Lock Screenshot
Combo Loc
Happy Face
Part II
For the mechanical part of the lab, I searched the house for an alternate way of pressing the FSR. I settled on using my girlfriend's curling iron (not plugged in of course) with some packing bubbles between the FSR and the iron to distribute the force. Here is a picture (bubbles not included so you can see the FSR).
FSR depresser/ force distributor