Button Sequence question

Hey guys! I'm most definitely a noob and need help with this: I want to create a simple button sequence to input to unlock something. I'm not sure how to put it into the arduino language. For example, let's say I have five buttons named "1-5" and I want the user to have to press them in this order (1,1,1,4,2,5,5,3) How can I accomplish this in code? Is it an array? a string? Like I said, I'm green, so be gentle, I'd really appreciate the help.

First you may need to debounce the buttons, so look at that example. Then after that, it is just a matter of IF/ELSE statements. Once you are able to get the buttons to shows the correct numbers on the serial monitor, then you simply store them in an array (one at a time), and compare that array to another array with the correct sequence. If they don't match, you can send out an error message.

Finite State Machine.

Right, thanks for the response. I've already got the debounce down and the basics, it's the storing in an array and comparing that I'm totally lost about. Thanks again.

Think of it this way. You'll have one array that holds the combination and another that will be built from user input. When the user is done with input you can just compare the two arrays and use a flag to see how the comparison went. It's either right or wrong. Right?

byte combination[] = {1, 2, 3, 4, 5};  // a 5 number combination
byte userInput[5]; // same size, will hold user input

// to compare them we'll start with a flag set true
boolean flag = true;

// then a simple for loop can do the check
for (byte n = 0; n < 5 ; n++)
{
    if (userInput[n] != combination[n]) // any number that doesn't match means a miss
    {
        flag = false;
    }
}
// at this point flag will be true if it matched and false if it didn't

Does that help?

Yes that definitely helps! Thank you! I'm just starting out, so learning the more complex operations is tough, being self taught, but like the beatles say, I'll get by with a little help from my friends!

One more thing, how do you store each button press in the array to be able to compare it?

Look to see what button is pressed, (shown in serial monitor) then do something like Button_Combination[count] = (whatever button was pressed). Once a new button is pressed, the "count" is increased by 1, to store the next button.

This was the code to another persons project, you may be able to get something from it.

 void loop(){
  //lcd.begin(15,2);
  lcd.setCursor(2,0);
  lcd.print("Input Digits");

  char key = keypad.getKey();
if(key != NO_KEY) // Do nothing if no key is pressed, incorporated from PaulS's example.
  {
      if (key != '*') {
    Data[currentCommand++] = key;
    keyIn = atoi(Data); //convert array data to actual integer
    lcd.setCursor(6,1);
    lcd.print(keyIn);    //this should print the first digit in it's same spot and the 2nd and 3rd will follow like normal, when inputted.   
}

else {
     //keyIn = atoi(Data);
    //lcd.setCursor(6,1);
    //lcd.print(keyIn);
    //lcd.print("     ");      
    lcd.print(" OK");        //just to tell you * was pressed
    Serial.println(keyIn);
    while(currentCommand !=0){   // This can be used for any array size, 
    Data[currentCommand--] = 0; //clear for new data
        }
     } 
  }
}

http://arduino.cc/forum/index.php/topic,137352.30.html

Okay, hammered out something, It kind of works, am I at least on the right track?

const int greenled = 11;
const int redled = 8;
const int b1 = 2;
const int b2 = 3;
const int b3 = 4;

int pwcount;
byte combination[] = "1332";
byte userInput[4];

int buttonstate1 = 0;
int buttonstate2 = 0;
int buttonstate3 = 0;

void setup() {
  pinMode(greenled, OUTPUT);
  pinMode(redled, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  Serial.begin(9600);
}

void loop(){
  buttonstate1 = digitalRead(b1);
  buttonstate2 = digitalRead(b2);
  buttonstate3 = digitalRead(b3);

  if (buttonstate1 == HIGH){
    userInput[pwcount] = '1';
    pwcount++;
    delay(300);
    Serial.print('1');
  }
  if (buttonstate2 == HIGH){
    userInput[pwcount] = '2';
    pwcount++;
    delay(300);
    Serial.print('2');
  }
  if (buttonstate3 == HIGH){
    userInput[pwcount] = '3';
    pwcount++;
    delay(300);
    Serial.print('3');
  }
  for(byte n = 0; n <=4; n++){


    if (userInput[pwcount] == combination[n] && pwcount >=4){
      digitalWrite(redled, LOW);
      digitalWrite(greenled, HIGH);
      Serial.println("unlocked");
      pwcount = 0;

    }
    else {
      if(userInput[n] != combination[n] && pwcount >=4){
        digitalWrite(greenled, LOW);
        digitalWrite(redled, HIGH);
        Serial.println("Denied");
        pwcount = 0;
        n = 0;
      }
    }
  }

Does it make sense to check for a password match if the user has entered only one character?

Please use Tools + Auto Format before posting code. Properly indented code is much easier to read. Also, all those blank lines are not necessary.

Not really. I only need it to do a check once all characters in the code have been entered. Sorry about the format, new to forums. I'll try and fix it.

PaulS point is interesting: There are commercial door entry units out there that check the code automatically after the correct count is entered, not requiring a 'enter' button (usually A or B)- fine! - However they then bubble the code up the array and pop the new number in at the bottom. The code is checked again!

thus:
code 1234
user enters 91234752 and gains entry, whereas we would expect the buffer to hold 4752 and fail! (door access usually lasts 7 sec)

This strange behaviour is then compounded by timing-out after n wrong tries (without a visual warning, although a grunt is issued on a fail, lamp-change for success). The next user hits the pad with the correct code and is already bared, so they start going through all the codes they have ever used, anywhere, lottery numbers, granny's birthday etc. The unit comes back on line and hey presto, locks them out again!

Point being, ensure the keypad is helpful and your users understand what it is trying to tell them.

Your not denouncing your buttons, so you might be getting multiple 1's, 2's and 3's on the serial monitor. And if that is the case, then they will all go into your array and mess up your password.

Yeah this is just mainly a proof of concept code so that I can understand the interaction of arrays, the 300ms delay after each button press does a decent job of keeping the numbers from repeating(provided they're not held down). I'm not sure if it's good form, but it works for this test. In the future, on my final project, I'll have a proper debounce.

In the future, on my final project, I'll have a proper debounce.

I suspect that edge detection will prove even more useful. Detect when a switch is pressed now and was not pressed before. This, of course, requires that you record the state between iterations of loop(), in a static or global variable.

300 milliseconds is far longer than even a cheap switch will bounce, but is not longer than a use might press a switch, thinking that it counts as only one press.

Not familiar with edge detection, do you mean just a state change detection? If I used that, I would still need a slight delay on it, right? I appreciate all the help, I'm an artist not an engineer, sometimes it's like reading chinese! F.Y.I., the end goal is to build a box with a lid that looks like a Nintendo controller, and the box can only be opened by entering the Konami code, should be pretty awesome.

Not familiar with edge detection, do you mean just a state change detection?

Yes.

If I used that, I would still need a slight delay on it, right?

Maybe. Maybe not. It depends on what you do after you detect a switch press.

I appreciate all the help, I'm an artist not an engineer, sometimes it's like reading chinese! F.Y.I., the end goal is to build a box with a lid that looks like a Nintendo controller, and the box can only be opened by entering the Konami code, should be pretty awesome.

You probably want a small delay, then. About 10 milliseconds.

Edge detection is close to what it sounds like - it's looking for the "edge" of a change. The change may be high to low, or low to high.

Basically, if the current state is Low, we are looking for a High, and vis versa.

In your case, for a single button, assuming a button press will drive the port High, and you do not want a repeat key function

Wait for a change to High, you can then pause for a few mS, to allow your button to settle (all electrical contacts tend to bounce. For high speed electronics, this bounce appears very slow and can look like several open/close events, so we wait for a small amount of time for the bounce to stop)
You then look at the port for a change to zero to occur (finger off), no need to denounce if you are off to do something else.
We now have a full button press.

Awesome, thanks a ton