analog kepad config and LCD I2C code development

Hello,

I’m working on this project for quite a time.

I finished the hardware configuration and working on the code development.

I know how to display messages and perform other simple actions.

The issue I have now is that I want to enter password, and then if the password is correct then you continue. Otherwise, it display wrong password.

This is the code.

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <AnalogVoltageRead.h>

#define analogPin A0
AnalogVoltageRead AVRD(analogPin);
LiquidCrystal_I2C lcd(0x27,2, 1, 0, 4, 5, 6, 7);

char Password_Array[4];
char Master[]= "123";
byte data = 0;
char targetkey;

byte ok = 13;
byte no = 12;

void setup(){
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  
  pinMode(ok,OUTPUT);
  pinMode(no,OUTPUT);
}

void loop(){
  lcd.setCursor(2,0);
  lcd.print("** Welcome **");
  delay(1000);
  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print("Enter Password");

   targetkey = AVRD.readKey();
   
    while(targetkey){

  //if(targetkey != KEY_NOT_PRESSED)
    if(targetkey){
      
      Password_Array[data] = targetkey;
      lcd.setCursor(data,1);
      lcd.print(Password_Array[data]);
      data++;
    Serial.println(targetkey);
    }}
// !Key NO_KEY}

if(data==3)
{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Password is ");

     if(!strcmp(Password_Array, Master)){
      lcd.print("Good");
      digitalWrite(ok,HIGH);
      delay(1000);
      lcd.clear();
      cleardata();}
    else{
      lcd.print("Bad");
      digitalWrite(no,HIGH);
    delay(1000);
    lcd.clear();
    cleardata();  
}}}
  
    

void cleardata()
{
  while(data!=0)
  {   // This can be used for any array size, 
    Password_Array[data--] = 0; //clear array for new data
    digitalWrite(ok,LOW);
    digitalWrite(no,LOW);
  }
  //return;
}

And the problem is?

There are two things that I would change; first of all the name data. It confused me as I thought it was a value that came in from somewhere (e.g. serial port, reading of pin). It’s better to change the name so it reflects the purpose; e.g. keycount. This will not solve your problem though (whatever it is).

The other thing to change would be initialisation of your password array (and the possible cause of your problem, whatever it is). Your using a strcmp() which expects a null terminated character string. It’s not said that char Password_Array[4]; sets all elements to ‘\0’; it usually contains garbage.

In your setup(), you can add a for loop to initialise all characters to ‘\0’ or use [memset() as shown below.

void setup()
{
  ...
  ...
  memset(Password_Array, 0, sizeof(Password_Array));
  ...
  ...
}

Reference for sizeof: [sizeof operator

If you need more help, please let us know what your exact problem is.](sizeof operator - cppreference.com)](http://linux.die.net/man/3/memset)

Same topic on other forums:

http://www.avrfreaks.net/forum/analog-k … evelopment

http://forum.arduino.cc/index.php?topic=365544.0

Yeah, and inform us what that hardware actually is and how it is supposed to work. You included a keypad library, but the password is based on an analog read. Or some kind of AnalogVoltageRead class from an unknown library. Google has no idea what it is or does.