access system with keypad and pir sensor

Hey guys,I'm a beginner with arduino
Can someone please explain whats wrong with my code and how can i make it better?

#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Password_Lenght 5
LiquidCrystal_I2C lcd(0x27, 16, 2);
char Data[Password_Lenght];
char Master[Password_Lenght] = "1234";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
int redpin=10;
int greenpin=11;
int ledalb = 13;
int PIR = 2;
const byte ROWS = 4;
const byte COLS = 4;
const int buzzer = 6;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,12}; //connect to the column pinouts of the keypad
Keypad customKeypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of
class NewKeypad
void setup()
{
lcd.begin();// initialize the lcd
lcd.backlight();
pinMode(redpin,OUTPUT);
pinMode(greenpin,OUTPUT);
pinMode(ledalb, OUTPUT); // declare LED as output
pinMode(PIR, INPUT);
Serial.begin(9600);
digitalWrite(PIR, LOW);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print(" Introd. Parola");
customKey = customKeypad.getKey();
if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
{
tone(buzzer,1000);
delay(100);
noTone(buzzer);
Data[data_count] = customKey; // store char into data array
lcd.setCursor(data_count,1); // move cursor to show each new char
lcd.print("*"); // print char at said cursor
data_count++;
}
if(data_count == Password_Lenght-1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Parola este ");
if(!strcmp(Data, Master)){ // equal to (strcmp(Data, Master) == 0)
tone(buzzer, 1000);
noTone(buzzer);
lcd.setCursor(4,1);
lcd.print("corecta");
digitalWrite(greenpin, HIGH);
digitalWrite(redpin, LOW);
delay(500);
}
else{
lcd.setCursor(4,1);
lcd.print("gresita");
tone(buzzer, 1000);
delay(500);
noTone(buzzer);
delay(200);
tone(buzzer, 500);
delay(200);
noTone(buzzer);
digitalWrite(redpin, HIGH);
digitalWrite(greenpin, LOW);
}
delay(1000);// added 1 second delay to make sure the password is completely shown on screen before
it gets cleared.
lcd.clear();
clearData();
};
if (digitalRead(PIR) == HIGH) { // check if the sensor is HIGH
digitalWrite(ledalb, HIGH); // turn LED ON
Serial.println("Motion detected!");
delay(500); // delay 100 milliseconds
}
else if(digitalRead(PIR) == LOW){
digitalWrite(ledalb, LOW); // turn LED OFF
Serial.println("Motion stopped!");
delay(100); // delay 100 milliseconds
}
}
void clearData()
{
while(data_count !=0)
{ // This can be used for any array size,
Data[data_count--] = 0; //clear array for new data
}
return;
}

Keypad customKeypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of
class NewKeypad
void setup()

delay(1000);// added 1 second delay to make sure the password is completely shown on screen before
it gets cleared.
lcd.clear();

The compiler thinks that the "class NewKeypad" line and the line "it gets cleared" are a new lines of code with bad syntax. Eliminate the new line before those lines to fix.

no matching function for call to 'LiquidCrystal_I2C::begin()'

There are several versions of the LiquidCrystal_I2C library. They are not the same. You have tried to mix them. The version that you have included does not use the begin() function, it does use the init() function.

Here is the code fixed so that it compiles with no errors or warinings and formatted with the IDE autoformat tool.

#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Password_Lenght 5
LiquidCrystal_I2C lcd(0x27, 16, 2);
char Data[Password_Lenght];
char Master[Password_Lenght] = "1234";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
int redpin = 10;
int greenpin = 11;
int ledalb = 13;
int PIR = 2;
const byte ROWS = 4;
const byte COLS = 4;
const int buzzer = 6;
char keys[ROWS][COLS] =
{
   {'1', '2', '3', 'A'},
   {'4', '5', '6', 'B'},
   {'7', '8', '9', 'C'},
   {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 12}; //connect to the column pinouts of the keypad
Keypad customKeypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad

void setup()
{
   lcd.init();// initialize the lcd
   lcd.backlight();
   pinMode(redpin, OUTPUT);
   pinMode(greenpin, OUTPUT);
   pinMode(ledalb, OUTPUT); // declare LED as output
   pinMode(PIR, INPUT);
   Serial.begin(9600);
   digitalWrite(PIR, LOW);
}
void loop()
{
   lcd.setCursor(0, 0);
   lcd.print(" Introd. Parola");
   customKey = customKeypad.getKey();
   if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
   {
      tone(buzzer, 1000);
      delay(100);
      noTone(buzzer);
      Data[data_count] = customKey; // store char into data array
      lcd.setCursor(data_count, 1); // move cursor to show each new char
      lcd.print("*"); // print char at said cursor
      data_count++;
   }
   if (data_count == Password_Lenght - 1)
   {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" Parola este ");
      if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0)
      {
         tone(buzzer, 1000);
         noTone(buzzer);
         lcd.setCursor(4, 1);
         lcd.print("corecta");
         digitalWrite(greenpin, HIGH);
         digitalWrite(redpin, LOW);
         delay(500);
      }
      else
      {
         lcd.setCursor(4, 1);
         lcd.print("gresita");
         tone(buzzer, 1000);
         delay(500);
         noTone(buzzer);
         delay(200);
         tone(buzzer, 500);
         delay(200);
         noTone(buzzer);
         digitalWrite(redpin, HIGH);
         digitalWrite(greenpin, LOW);
      }
      delay(1000);// added 1 second delay to make sure the password is completely shown on screen before it gets cleared.
      lcd.clear();
      clearData();
   };
   if (digitalRead(PIR) == HIGH)   // check if the sensor is HIGH
   {
      digitalWrite(ledalb, HIGH); // turn LED ON
      Serial.println("Motion detected!");
      delay(500); // delay 100 milliseconds
   }
   else if (digitalRead(PIR) == LOW)
   {
      digitalWrite(ledalb, LOW); // turn LED OFF
      Serial.println("Motion stopped!");
      delay(100); // delay 100 milliseconds
   }
}

void clearData()
{
   while (data_count != 0)
   {
      // This can be used for any array size,
      Data[data_count--] = 0; //clear array for new data
   }
   return;
}

I use the hd44780 library for my I2C LCDs. It is the best and fastest library currently available. It can be installed through the library manager.

The code does not compile. That's wrong.

Look at this:

   }
    delay(1000);// added 1 second delay to make sure the password is completely shown on screen before
    it gets cleared.
    lcd.clear();
    clearData();

The second quoted line starts: "it gets cleared"..... That's the fault.

By the way, learn to use Autoformat, Ctrl T! Many common errors will be obvious then, reading the code.

Thanks for answering ,the code compiled successfully and everything works fine now!

No, You can't. You have declared pin 6 as a member in array ROWS.
Take some time to sort things out. No quick fix that only cause new trouble.

yeah it seems like i cant ...

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