Pages: [1] 2   Go Down
Author Topic: Button Sequence question  (Read 1432 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 98
Posts: 3560
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finite State Machine.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
« Last Edit: January 24, 2013, 05:03:19 pm by Jimmy60 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Queens, New York
Online Online
Faraday Member
**
Karma: 98
Posts: 3560
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: January 25, 2013, 09:11:49 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, hammered out something, It kind of works, am I at least on the right track?
Code:
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;
      }
    }
  }
« Last Edit: January 26, 2013, 02:40:12 pm by Elduderino » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49022
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 26, 2013, 02:39:08 pm by Elduderino » Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

For whom does the clock pulse? It pulses for you!

Queens, New York
Online Online
Faraday Member
**
Karma: 98
Posts: 3560
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49022
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1] 2   Go Up
Jump to: