Combination Lock Game – Denah, Chris, and Saima

Here is a video of the game.

Image

 

Here is the code:

/* combo lock game
*
* using combo lock code, building on it
* when the four buttons are pressed in certain combinations, lights shine, get them all to win!
* denah,chris,saima
*
*/

int ORANGE = 3;
int PINK = 4;
int YELLOW = 6;
int BLUE = 5;
int RED = 7;
int GREEN = 8;

int Button1 = 9; // PushButton connected to pin 9
int Button2 = 10; // PushButton connected to pin 10
int Button3 = 11; // PushButton connected to pin 11
int Button4 = 12; // PushButton connected to pin 12
int debounce = 10; // Length of time to debounce switch in milliseconds
int B1Value; // used to feel current button state
int B2Value; // used to feel current button state
int B3Value; // used to feel current button state
int B4Value; // used to feel current button state

int Combo[4] = {1,2,3,4}; // THIS IS THE COMBINATION TO OPEN THE LOCK
int Guess[4]; // used to hold the combination guess
int Position = 0; // used to hold the position we’re on in the array

boolean LastB1Value = LOW; // used to remember last button state
boolean LastB2Value = LOW; // used to remember last button state
boolean LastB3Value = LOW; // used to remember last button state
boolean LastB4Value = LOW; // used to remember last button state

void setup()
{
pinMode(RED, OUTPUT); // set direction of RED led pin
pinMode(GREEN, OUTPUT); // set direction of GREEN led pin
pinMode(YELLOW, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(PINK, OUTPUT);
pinMode(ORANGE, OUTPUT);
pinMode(Button1, INPUT); // set direction of button 1
pinMode(Button2, INPUT); // set direction of button 2
pinMode(Button3, INPUT); // set direction of button 3
pinMode(Button4, INPUT); // set direction of button 4
Serial.begin(9600);
}

void loop(){

B1Value = digitalRead(Button1); // TAKE A LOOK AT BUTTON 1
if (B1Value != LastB1Value){ // IF BUTTON 1 HAS CHANGED,
delay(debounce); // WAIT FOR DEBOUNCE TIME THEN
B1Value = digitalRead(Button1); // TAKE A LOOK AT BUTTON 1
if (B1Value != LastB1Value){ // IF THIS IS A REAL INPUT THEN
if (B1Value == HIGH){ //IF THE BUTTON HAS JUST BEEN PRESSED (LOW TO HIGH TRANSITION)
Guess[Position] = 1; //PUT THE VALUE OF THE BUTTON INTO THE CORRECT POSITION IN THE ARRAY
Position++; //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
Serial.println(Position); //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
}
LastB1Value = B1Value; //KEEP TRACK OF THE BUTTON STATE TO COMPARE NEXT TIME WE LOOP
}
}

B2Value = digitalRead(Button2);
if (B2Value != LastB2Value){
delay(debounce);
B2Value = digitalRead(Button2);
if (B2Value != LastB2Value){
if (B2Value == HIGH){ //IF THE BUTTON HAS JUST BEEN PRESSED
Guess[Position] = 2; //PUT THE VALUE OF THE BUTTON INTO THE CORRECT POSITION IN THE ARRAY
Position++; //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
Serial.println(Position); //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
}
LastB2Value = B2Value; //KEEP TRACK OF THE BUTTON STATE TO COMPARE NEXT TIME WE LOOP
}
}

B3Value = digitalRead(Button3);
if (B3Value != LastB3Value){
delay(debounce);
B3Value = digitalRead(Button3);
if (B3Value != LastB3Value){
if (B3Value == HIGH){ //IF THE BUTTON HAS JUST BEEN PRESSED
Guess[Position] = 3; //PUT THE VALUE OF THE BUTTON INTO THE CORRECT POSITION IN THE ARRAY
Position++; //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
Serial.println(Position); //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
}
LastB3Value = B3Value; //KEEP TRACK OF THE BUTTON STATE TO COMPARE NEXT TIME WE LOOP
}
}

B4Value = digitalRead(Button4);
if (B4Value != LastB4Value){
delay(debounce);
B4Value = digitalRead(Button4);
if (B4Value != LastB4Value){
if (B4Value == HIGH){ //IF THE BUTTON HAS JUST BEEN PRESSED
Guess[Position] = 4; //PUT THE VALUE OF THE BUTTON INTO THE CORRECT POSITION IN THE ARRAY
Position++; //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
Serial.println(Position); //INCRIMENT THE POSITION COUNTER TO RECORD THE NEXT BUTTON PRESS
}
LastB4Value = B4Value; //KEEP TRACK OF THE BUTTON STATE TO COMPARE NEXT TIME WE LOOP
}
}

if (Position == 4){ //CHECK TO SEE IF WE’VE GOT 4 PRESSES, IF SO THEN-
Position = 0; //RESET THE POSITION COUNTER FOR THE NEXT GO-AROUND
if (Combo[0]==Guess[0] && Combo[1]==Guess[1] && Combo[2]==Guess[2] && Combo[3]==Guess[3]){
digitalWrite (GREEN,HIGH); // THEN TEST THE GUESS AGAINST THE STORED COMBO
delay (1000); // IF IT’S THE SAME, THEN FLASH THE GREEN LED

}else{
if (Combo[3]==Guess[0] && Combo[2]==Guess[1] && Combo[1]==Guess[2] && Combo[0]==Guess[3]){
digitalWrite (RED,HIGH); // THEN TEST THE GUESS AGAINST THE STORED COMBO
delay (1000); // IF IT’S THE SAME, THEN FLASH THE GREEN LED
}else{
if (Combo[3]==Guess[0] && Combo[2]==Guess[1] && Combo[3]==Guess[2] && Combo[2]==Guess[3]){
digitalWrite (YELLOW,HIGH); // THEN TEST THE GUESS AGAINST THE STORED COMBO
delay (1000); // IF IT’S THE SAME, THEN FLASH THE GREEN LED
}else{
if (Combo[0]==Guess[0] && Combo[1]==Guess[1] && Combo[0]==Guess[2] && Combo[1]==Guess[3]){
digitalWrite (BLUE,HIGH); // THEN TEST THE GUESS AGAINST THE STORED COMBO
}else{
if (Combo[1]==Guess[0] && Combo[2]==Guess[1] && Combo[1]==Guess[2] && Combo[2]==Guess[3]){
digitalWrite (PINK, HIGH); // THEN TEST THE GUESS AGAINST THE STORED COMBO
delay(1000);
}else{
if (Combo[3]==Guess[0] && Combo[0]==Guess[1] && Combo[3]==Guess[2] && Combo[0]==Guess[3]){
digitalWrite (ORANGE, HIGH); // THEN TEST THE GUESS AGAINST THE STORED COMBO
delay(1000);
}
}
}
}
}
}
}
}// END OF LOOP

 

And here is the pseudo code: 

orange led connected to pin 3

pink led connected to pin 4

yellow led connected to pin 6

blue led connected to pin 5

red led connected to pin 7

green led connected to pin 8

 

button 1 is connected to pin 9

button 2 connected to pin 10

button 3 connected to pin 11

button 4 connected to pin 12

time to debounce switch is 10 sec (10,000 millis)

used to feel current button state

used to feel current button state

used to feel current button state

used to feel current button state

 

combination to open the lock is 1,2,3,4

used to hold the combination guess

used to hold the position we’re on in the array

 

used to remember last button state

used to remember last button state

used to remember last button state

used to remember last button state

 

void setup

take a look at button 1

if button 1 has changed,

wait for debounce time

take a look at button 1

if this is a real input then

if the button has just been pressed

put the value of the button into the correct position in the array

increment the position counter to record the next button press

increment the position counter to record the next button press

 

keep track of the button state to compare next time we loop

 

 

 

take a look at button 2

if button 2 has changed,

wait for debounce time

take a look at button 2

if this is a real input then

if the button has just been pressed

put the value of the button into the correct position in the array

increment the position counter to record the next button press

increment the position counter to record the next button press

 

keep track of the button state to compare next time we loop

 

 

 

take a look at button 3

if button 3 has changed,

wait for debounce time

take a look at button 3

if this is a real input then

if the button has just been pressed

put the value of the button into the correct position in the array

increment the position counter to record the next button press

increment the position counter to record the next button press

 

keep track of the button state to compare next time we loop

 

 

 

take a look at button 4

if button 4 has changed,

wait for debounce time

take a look at button 4

if this is a real input then

if the button has just been pressed

put the value of the button into the correct position in the array

increment the position counter to record the next button press

increment the position counter to record the next button press

 

keep track of the button state to compare next time we loop

 

 

 

check to see if we’ve got 4 presses, if so then

reset the position counter for the next go-around

if the guess is 0,1,2,3

then flash the green led

wait 10 sec

 

or else 

if the combination guess is 3,2,1,0

then flash the RED led

wait 10 sec

or else

if the combination guess is 3,2,3,2

then flash the YELLOW led

wait 10 sec

or else

if the combination guess is 0,1,0,1

then flash the BLUE led

or else

if the combination guess is 1,2,1,2

then flash the PINK led

wait 10 sec

or else

if the combination guess is 3,0,3,0

then flash the ORANGE led

wait 10 sec

 

 

 

 

 

 

 

end of loop

 

Advertisements

One thought on “Combination Lock Game – Denah, Chris, and Saima

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s