Please correct my code

#include <Wire.h>
#include <Keypad.h>
// Length of password + 1 for null character
#define Password_Length 8
// Character to hold password input
char Data[Password_Length];
// Password
char Master[Password_Length] = "123A456";

// Pin connected to lock relay input
int lockOutput = 13;

// Counter for character entries
byte data_count = 0;

char customKey;

// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;

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

// Connections to Arduino
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
// Setup serial monitor
Serial.begin(9600);
}

void loop() {
// Get key value if pressed
char customKey = customKeypad.getKey();
Serial.print("Enter password");

if (customKey) {
// Print key value to serial monitor
Serial.println(customKey);
Serial.println(Data[data_count]);
data_count++;
}
if (data_count == Password_Length - 1) {

if (!strcmp(Data, Master)) {
  // Password is correct
  Serial.println("Correct");
  // Turn on relay for 5 seconds
  digitalWrite(lockOutput, HIGH);
  delay(5000);
  digitalWrite(lockOutput, LOW);
}
else {
  // Password is incorrect
  Serial.println("Incorrect");
  delay(1000);
}

}

void clearData()

{ while (data_count != 0) {
Data[data_count--] = 0;
}
}
return;

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

What do you think is wrong with your code ?

Does it compile ?
What does it do ?
What should it do ?

You don't appear to be putting anything into the array Data

actually i am converting a code from lcd to serial so i changed lcd to serial but it does not compile

So post the error messages and the code that produces them.

original code is

// Include Arduino Wire library for I2C
#include <Wire.h>
// Include LCD display library for I2C
#include <LiquidCrystal_I2C.h>
// Include Keypad library
#include <Keypad.h>

// Length of password + 1 for null character
#define Password_Length 5
// Character to hold password input
char Data[Password_Length];
// Password
char Master[Password_Length] = "1409";

// Pin connected to lock relay input
int lockOutput = 13;

// Counter for character entries
byte data_count = 0;

// Character to hold key input
char customKey;

// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;

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

// Connections to Arduino
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// Create LCD object
LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup() {
// Setup LCD with backlight and initialize
lcd.backlight();

lcd.init();

// Set lockOutput as an OUTPUT pin
pinMode(lockOutput, OUTPUT);
}

void loop() {

// Initialize LCD and print
lcd.setCursor(0, 0);
lcd.print("Enter Password:");

// Look for keypress
customKey = customKeypad.getKey();
if (customKey) {
// Enter keypress into array and increment counter
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}

// See if we have reached the password length
if (data_count == Password_Length - 1) {
lcd.clear();

if (!strcmp(Data, Master)) {
  // Password is correct
  lcd.print("Correct");
  // Turn on relay for 5 seconds
  digitalWrite(lockOutput, HIGH);
  delay(5000);
  digitalWrite(lockOutput, LOW);
}
else {
  // Password is incorrect
  lcd.print("Incorrect");
  delay(1000);
}

// Clear data and LCD display
lcd.clear();
clearData();

}
}

void clearData() {
// Go through array and clear data
while (data_count != 0) {
Data[data_count--] = 0;
}
return;
}

i want to convert it inito serial display from lcd

How about replacing the "lcd.print"s with "Serial.print"s ?

Please remember to use code tags when posting code.

Not posted in code tags

i am new to the platform so i dont know how to

giving some random error

You don't now how to replace the prints, or how to use code tags?

No.
Simply no.

i am new to this i know only basic codes so i tried to do that replacing but it gave some of the other error you can check the first sent code

If you don't know how to use code tags when posting code here then you did not read the link that I posted in reply #2, or if you did then you decided to ignore the advice

I can check what?

Why don't you simply post the error messages you are seeing?

You're missing a '}' at the end of loop().

You have a "return;" outside of any function at the end of your sketch.

After fixing those two syntax errors it compiles.

You are missing the line:
Data[data_count] = customKey;
before
data_count++;

You are missing the line:
clearData();
after the line
Serial.println("Incorrect");

1 Like

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