4x4 Keypad Password + LCD

So I am making a password system. I don't want it to run void loop until the password has been correctly entered. my code seems to read the key presses but it isn't updating the integer "authorized" so I don't know what to do! Please help :wink:
here is my code:

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad

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


LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
int time;
int passpos = 1;
int digit1 = 1;
int digit2 = 2;
int digit3 = 3;
int digit4 = 4;
int curdig1 = 0;
int curdig2 = 0;
int curdig3 = 0;
int curdig4 = 0;
int authorized = 0;

void authorize()
{
  lcd.setCursor(6, 1);
  lcd.print("____");
  if (passpos == 1)
  {
    char key = keypad.waitForKey();
    if (digit1 = key)
    {
      curdig1 = key;
      lcd.setCursor(6, 1);
      lcd.print("#");
      passpos = 2;
    }
    else {
      lcd.print("#");
      passpos = 2;
    }

  }
  if (passpos == 2)
  {
    char key = keypad.waitForKey();
    if (digit2 = key)
    {
      curdig1 = key;
      lcd.setCursor(7, 1);
      lcd.print("#");
      passpos = 3;
    }
    else {
      lcd.print("#");
      passpos = 3;
    }

  }
  if (passpos == 3)
  {
    char key = keypad.waitForKey();
    if (digit3 = key)
    {
      curdig1 = key;
      lcd.setCursor(8, 1);
      lcd.print("#");
      passpos = 4;
    }
    else {
      lcd.print("#");
      passpos = 4;
    }

  }
  if (passpos == 4)
  {
    char key = keypad.waitForKey();
    if (digit4 = key)
    {
      curdig1 = key;
      lcd.setCursor(9, 1);
      lcd.print("#");
      passpos = 5;
    }
    else if (passpos == 5 && curdig1 == digit1 && curdig2 == digit2 && curdig3 == digit3 && curdig4 == digit4)
    {
      authorized = 1;
      lcd.clear();
    }
    else
    {
      authorize();
    }
  }
}
void setup()
{
  lcd.init();
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print("Password:");
  authorize();
  lcd.print(authorized);
}
void loop()
{
  lcd.setCursor(7, 1);
  char  key = keypad.getKey();

  if (key) {
    lcd.print(key);
  }
}

At the very least, you probably want == (test for equality) not = (assignment) in many places.

At a glance, the logic for getting out when passpos becomes 5 don’t look right.

Trace it by hand, that is “play computer” and walk yourself through it to see… it’s doing what you wrote, maybe not what you think.

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.