Button combination_Helen

I am Helen Hyungou Jo, and this is my first assignment for making button combination.

I used 4 buttons and 2 LED, and there is one unlock button combination. If user put the right button combination, the first LED is on. If user put the wrong combination, the second LED is on.

This is my code.
int RED = 13; // red LED connected to digital pin 7
int GREEN = 12; // green LED connected to digital pin 8

int Button1 = 5; // PushButton connected to pin 9
int Button2 = 4; // PushButton connected to pin 10
int Button3 = 3; // PushButton connected to pin 11
int Button4 = 2; // 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()
{
Serial.begin(9600);

pinMode(RED, OUTPUT); // set direction of RED led pin
pinMode(GREEN, OUTPUT); // set direction of GREEN led pin
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
}
void loop(){

B1Value = digitalRead(Button1); // TAKE A LOOK AT BUTTON 1
Serial.println(B1Value);
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
}
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
}
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
}
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
}
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
digitalWrite (GREEN,LOW);
}else{ // IF IT’S NOT THE SAME, THEN FLASH THE RED LED
digitalWrite (RED,HIGH);
delay (1000);
digitalWrite (RED,LOW);
}
}

}// END OF LOOP

bt_combination

This is a video that can show how it works.

Advertisements

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