Arduino software (Exiting from one loop and get ready for another

2.7k views Asked by At

I am working on a keypad password lock system based on Arduino UNO. I have used the password library from Arduino's website.

What I am trying to accomplish is to set two passwords namely pass1 and pass 2.

The condition is that this circuit is attached to a gate, if user 1 lets say knows pass1, after entering the pass the gate will unlock for 10 seconds and the pass1 becomes invalid i.e. can not be used in future. However if user 2 comes and knows pass2, after entering the pass2, the gate will open for 10 seconds and then pass2 becomes invalid, in a nutshell a password can be used only once. I am attaching the code below. The problem with this code is that when pass1 is executed, the system does not take pass2 until unless the system is reset.

  #include <Password.h>

  #include <LiquidCrystal.h>
  #include <Password.h> 
  #include <Keypad.h> 
  LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
  Password password1 = Password( "1234" );
  Password password2 = Password( "4567" );
  int ledpin = A5;
  int a=0;
  const byte ROWS = 4; // Four rows
  const byte COLS = 4; // columns

   char keys[ROWS][COLS] = {
   {'1','2','3','A'},
   {'4','5','6','B'},
   {'7','8','9','C'},
   {'*','0','#','D'}
   };

  byte rowPins[ROWS] = { A0, A1, A2, A3 };/
  byte colPins[COLS] = { 8, 9, 10, 11 };



  Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS   );

  void setup(){

  lcd.begin(16,2);
  keypad.addEventListener(keypadEvent); //add an event listener for this   keypad
    }

    void loop(){
     keypad.getKey();
    }


    void keypadEvent(KeypadEvent eKey){
    switch (keypad.getState()){
    case PRESSED:

    lcd.println(eKey);
    switch (eKey){
    case '*':
    checkPassword();
    break;
    case '#':
    password1.reset();

    password2.reset();
    break;
    default:
    password1.append(eKey);
    password2.append(eKey);

    }       return ; 
    }
    }


    void checkPassword(){

    if (password1.evaluate()){
    lcd.setCursor(0,1);
    lcd.println("code1 OK");
    delay (2000);
    digitalWrite(ledpin, HIGH);
    delay(10000);
    digitalWrite(ledpin,LOW);
    password1.append('1');
    lcd.clear();

    return;


    }else if (password2.evaluate()){ 

    lcd.setCursor(0,1);
    lcd.println("code2 OK");
    delay (2000);
    digitalWrite(ledpin,HIGH);
    delay(10000);
    digitalWrite(ledpin,LOW);
    password2.append('2');
    lcd.clear();
    return; 




   }else {
   lcd.setCursor(0,1);
   lcd.println("Wrong");
   delay(2000);
   lcd.clear();
   lcd.print("Wait for 5 Sec");
   delay(5000);
   lcd.clear();
   return;



  //add code to run if it did not work
  }return;
  }
1

There are 1 answers

2
Mike Armstrong On

Wow that code is a bit of a mess (no offence).

What you're trying to do is as simple as setting a global bool; try creating a bool that you'll access and set to false once the password has been entered. Then a condition to check if the password is used again, if (String x = password1 && bool y = false) { // Then allow or disallow }

This way you can work within your main loop and never miss an incoming char.