« 9-11-2006 | Main | Beautifying the Blog »

Combination Lock (though 'lock' may be a bit of an overstatement...)

This is my first real breadboard project, and my first Arduino program - with modest results. Regrettably, I have not yet produced a stylish enclosure for it as I spent too much time fiddling with code which proved slightly beyond my present capabilities…

Problems include the use of the delay function and that the program does not generate an error when an incorrect button is pressed. As a result, simply pushing all the buttons will foil the lock. I think the best approach will be to poll all of the swithes using an array to store the digits (as Gian suggested). A few helpful 2nd year ITPers provided some helpful tips which I will attempt to develop as my programming abilities improve. Their comments are posted below.

It looks like this:

The program is as follows:

int switchPin1 = 2; // switch 1
int switchPin2 = 4; // switch 2
int switchPin3 = 3; // switch 3
int switchPin4 = 5; // switch 4
int greenLedPin = 6; // green led
int redLedPin = 7; // red led
int switchState = 0; // first switch state

void setup() {
pinMode(switchPin1, INPUT); // switch 1 set to input
pinMode(switchPin2, INPUT); // switch 2 set to input
pinMode(switchPin3, INPUT); // switch 3 set to input
pinMode(switchPin4, INPUT); // switch 4 set to input
pinMode(greenLedPin, OUTPUT); // green LED pin set to output
pinMode(redLedPin, OUTPUT); // red LED pin set to output
}

void loop() {
switchState = digitalRead(switchPin1); // monitor first switch in sequence

if (switchState == 1) { // when pressed check next switch in sequence
delay(900); // requires a rather precise human
if(digitalRead(switchPin2) == HIGH) {
delay(900);
if(digitalRead(switchPin3) == HIGH) {
delay(900);
if(digitalRead(switchPin4) == HIGH) {
digitalWrite(greenLedPin, HIGH); // green LED on
digitalWrite(redLedPin, LOW); // red LED off
delay(5000); //savor the victory
}
}
}
}
else {
digitalWrite(greenLedPin, LOW); // green LED off
digitalWrite(redLedPin, HIGH); // red LED on
}
}


Some feedback:

---- Thanks to Christopher Paretti

I would set up the correct sequence ahead of time.
So the combo might be 4231 or something.
If you don't hit 4 first, throw an error and reset.
If you hit 4, trigger a boolean to be true ( lock1 = 1 ), then check the other buttons against this variable.

So a button 2 press might say

int lock1 = 0;
int lock2 = 0;

// button press code here
if( lock1 == 1)
{
lock2 = 1;
else {
digitalWrite(errorLight, HIGH);
// and then reset all the booleans back to 0.
lock1 = 0;
lock2 = 0;
}

then button 3 might say

if ( lock1 == 1 && lock2 == 1)
// blah blah

---- Thanks to Gian Villamil

The way I coded mine (it's a simon game, but not so different) is like this:

I basically poll each switch in sequence and see if it is down. If a
switch is down, I save its number in an array, and move to the next
digit. When all the digits are entered, I compare against the
combination.

By doing the compare when the entire combo is entered, I defeat
attempts to guess the code by testing permutations.

---- Thanks to Andrew Schneider

Since "delay" is a blocking function, you might look into using
"millis":

http://www.arduino.cc/en/Reference/Millis

and an example from Tom that works as a non-blocking delay:

void loop() {
if (millis() - lastTimeVar > 500) {
//do something
lastTimeVar = millis();
}

TrackBack

TrackBack URL for this entry:
http://itp.nyu.edu/~scv201/cgi-bin/mt/mt-tb.cgi/3

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)