Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: January 24, 2013, 02:31:53 pm » |
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
Offline
Edison Member
Karma: 27
Posts: 1551
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: January 24, 2013, 02:41:43 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
California
Offline
Edison Member
Karma: 38
Posts: 1844
|
 |
« Reply #2 on: January 24, 2013, 02:44:19 pm » |
Finite State Machine.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #3 on: January 24, 2013, 03:36:25 pm » |
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
Full Member
Karma: 10
Posts: 222
When the going gets weird, the weird turn pro. - Hunter S. Thompson
|
 |
« Reply #4 on: January 24, 2013, 04:32:58 pm » |
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?
|
|
|
|
« Last Edit: January 24, 2013, 05:03:19 pm by Jimmy60 »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #5 on: January 24, 2013, 05:38:57 pm » |
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
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #6 on: January 25, 2013, 07:59:24 pm » |
One more thing, how do you store each button press in the array to be able to compare it?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 27
Posts: 1551
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #7 on: January 25, 2013, 08:58:56 pm » |
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
|
|
|
|
« Last Edit: January 25, 2013, 09:11:49 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #8 on: January 26, 2013, 02:16:19 pm » |
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; } } }
|
|
|
|
« Last Edit: January 26, 2013, 02:40:12 pm by Elduderino »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: January 26, 2013, 02:25:39 pm » |
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
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #10 on: January 26, 2013, 02:30:05 pm » |
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
Full Member
Karma: 5
Posts: 112
|
 |
« Reply #11 on: January 26, 2013, 02:52:42 pm » |
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
Offline
Edison Member
Karma: 27
Posts: 1551
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #12 on: January 26, 2013, 03:04:47 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #13 on: January 26, 2013, 03:12:05 pm » |
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
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: January 27, 2013, 08:18:43 am » |
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
|
|
|
|
|
|