Go Down

Topic: Button Sequence question (Read 2272 times) previous topic - next topic

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.
My GitHub:


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.


Jan 24, 2013, 10:32 pm Last Edit: Jan 24, 2013, 11:03 pm by Jimmy60 Reason: 1
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?

Code: [Select]

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?


Jan 26, 2013, 02:58 am Last Edit: Jan 26, 2013, 03:11 am by HazardsMind Reason: 1
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.

Code: [Select]
void loop(){
  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.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.print("     ");     
    lcd.print(" OK");        //just to tell you * was pressed
    while(currentCommand !=0){   // This can be used for any array size,
    Data[currentCommand--] = 0; //clear for new data

My GitHub:

Jan 26, 2013, 08:16 pm Last Edit: Jan 26, 2013, 08:40 pm by Elduderino Reason: 1
Okay, hammered out something, It kind of works, am I at least on the right track?
Code: [Select]
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);

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

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

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

    else {
      if(userInput[n] != combination[n] && pwcount >=4){
        digitalWrite(greenled, LOW);
        digitalWrite(redled, HIGH);
        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.

Jan 26, 2013, 08:30 pm Last Edit: Jan 26, 2013, 08:39 pm by Elduderino Reason: 1
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!

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.
For whom does the clock pulse? It pulses for you!


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.
My GitHub:

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.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131