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;
}
