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.

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?

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

// then a simple for loop can do the checkfor (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

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!

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.

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!

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.

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.

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.