Combining Variables

I am a super newb, I have looked around and it looks like no solution so I am looking for other ways to work this project. I want to setup an electromagnet door lock. My initial thought was to have a small board with 4 buttons on the board. Each button will have its own input and its own variable or value in array. Then I would like to enter a combination in the code say 1221. Using something like the debouce code to populate each variable and then have them all combined and checked against coded combination. So I would hit the first button once, the second button twice, third button twice, fourth button once. I hope this makes since. Thanks for your time. David

So what's your question? You could certainly do something like you're describing with an Arduino . . .

Do you think this would work - if so how can I combine count1, count2, count3, count4 into one variable ex 1221 so I can check that against a predetermined code.

Why do you need to combine them into one to compare them? Let's say you've got four int variables, presses1, presses2, presses3 and presses4. You can store the required code as four variables also, expected1, expected2, expected3 and expected4. Then just do four comparisons, ie if (expected1 == presses1 && expected2 == presses2 . . .)

Now an array might be a better way to store these guys than separate variables but that depends on your application and coding style.

Look at you go - good thought and very simple - thanks

Happy I could help.

This how I would do it:
(the actual implementation is left as an exercise;))

I would give each button an integral ID from 1 to 9 (this limits the programmatical identification of buttons to 9 distinct buttons).

Then you should have a target code or 'password'.

Say your code is 11321, then the user will need to press:
button 1
button 1
button 3
button 2
button 1

Walkthrough:
First you need some variables:

const byte CODE_LENGTH = 5;
const unsigned int code = 11321;

unsigned int currentCode = 0;
byte currentPress = 0;
byte currentId = 0;

First step will be to associate a button press with an integral value.

currentId = 0;

//using Button Library, requires an instance of Button called button1
if (button1.uniquePress()){
  currentId = 1;
}

//rest of the buttons
if (currentId){
  int factor = 0; //this should really be 10 ^ ( CODE_LENGTH - currentPress )
  currentCode += currentId * factor;
  currentPress++;
}

if (currentCode == code){
  //code found
} else if (currentPress>CODE_LENGTH){
  currentCode = 0;
  currentPress = 0;
}

something like that should do the trick :slight_smile:

That is just a little bit cooler than my 5 pages of code - I am going to check it out - thanks