Go Down

Topic: Button Sequence question (Read 1 time) previous topic - next topic

Elduderino

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!

Elduderino

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

HazardsMind

#7
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.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
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Elduderino

#8
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);
  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;
      }
    }
  }

PaulS

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.

Go Up