There don't seem to be any tutorials on how to use a 3x4 keypad with lcd and password. I have tried copying and pasting code together and that is not working. I am new to this, so I cant come up with my own code. I don't know what to do.
char Master_Password[4] = {'1','3','4','2'};
char Data[4];
char key = keypad.getKey();
if(key != NO_KEY) // Do nothing if no key is pressed, incorporated from PaulS's example.
{
if(currentCommand != 4) {
Data[currentCommand++] = key;
lcd.setCursor(6 + currentCommand,1);
lcd.print(Data[currentCommand]);
}
else {
for(int i=0; i < currentCommand; i++)
{
if(Master_Password[i] != Data[i])
{
//not valid message
}
}
//valid message
}
while(currentCommand !=0){ // This can be used for any array size,
Data[currentCommand--] = 0; //clear for new data
}
}
I doubt this will compile, but its just an example of how to do it. You need to fill in the rest.
I don't have enough skill to fill in the setup and what not. I will mess with it to see what I can do though.
I now have an idea. How can i control the lcd to display one digit after the next, instead of replacing the other, cause then i could do something like this,
if(key) // Check for a valid key.
{
switch (key)
{
case '1':
lcd.print("1");
break;
case '2':
lcd.print("2");
break;
}
}
but for every number.
The answer was already given to you. Actually quite a few times.
Data[currentCommand++] = key;
lcd.setCursor(currentCommand,1);// set the cursor to display each character as you enter it
lcd.print(Data[currentCommand]); // displays character at set cursor
Sorry. If I wanted to add that to this code
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char* secretCode = "235711";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {6,7,8,9}; //connect to the row pinouts of the keypad
byte colPins[cols] = {10,A4,A5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys),
rowPins, colPins,
rows, cols);
int redPin = 8;
int greenPin = 9;
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Enter Password");
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,1);
if (key== '1'){
lcd.print("1");
}
if (key== '2'){
lcd.print("2");
}
if (key== '3'){
lcd.print("3");
}
if (key == '*' || key == '#') {
position = 0;
setLocked(true);
}
if (key == secretCode[position]) {
position++;
}
if (position == 6) {
setLocked(false);
}
delay(50);
}
void setLocked(int locked)
{
if (locked) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
//add servo turn 90 degrees.
}
}
Where would I put it,(
Data[currentCommand++] = key;
lcd.setCursor(currentCommand,1);// set the cursor to display each character as you enter it
lcd.print(Data[currentCommand]); // displays character at set cursor
) and also how would I define Data and CurrentCommand.
if (key== '1'){
lcd.print("1");
}
if (key== '2'){
lcd.print("2");
}
if (key== '3'){
lcd.print("3");
}
What is wrong with simply:
lcd.print(key);
Don't make it more complicated than it needs to be.
char Data[20]; // 20 is the number of chars it can hold
int currentCommand = 0;
Both of these go at the top of your code.
As for how to incorporate the lcd print into your code, that's for you to research. Every time you PRESS a KEY, it should PRINT to the LCD. Hint hint.
PaulS, for some reason, when ever I try to lcd.print(key), it does not print a number, but a weird symbol, and it does not scroll. Hazardsmind, thank you, I will see what I can do.
Hazardsminds, I tried putting in the code just now, i put
char Data[20]; // 20 is the number of chars it can hold
int currentCommand = 0;
at the top, and
Data[currentCommand++] = key;
lcd.setCursor(currentCommand,1);// set the cursor to display each character as you enter it
lcd.print(Data[currentCommand]); // displays character at set cursor
right before
if (key == '1'){
lcd.print("1");
}
and I also tried taking out the if key ==1 lcd print 1. Every time, I get the same result. The whole bottom rows fills up with some weird symbol, then the top row does too. _____ It probably not help, but hat is sort of what the symbol looks like.
| __|
You left something out. Look back at reply# 21.
if(key != NO_KEY) // Do nothing if no key is pressed, incorporated from PaulS's example.
I added the if no key aspect and now the code looks like this
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char* secretCode = "235711";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {6,7,8,9}; //connect to the row pinouts of the keypad
byte colPins[cols] = {10,A4,A5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys),
rowPins, colPins,
rows, cols);
int redPin = 8;
int greenPin = 9;
//test code
char Data[20]; // 20 is the number of chars it can hold
int currentCommand = 0;
//test code
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Enter Password");
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,1);
//test code
if(key != NO_KEY) // Do nothing if no key is pressed
Data[currentCommand++] = key;
lcd.setCursor(currentCommand,1);// set the cursor to display each character as you enter it
lcd.print(Data[currentCommand]); // displays character at set cursor
//test code
if (key == '1'){
lcd.print("1");
}
if (key == '2'){
lcd.print("2");
}
if (key == '*' || key == '#') {
position = 0;
setLocked(true);
}
if (key == secretCode[position]) {
position++;
}
if (position == 6) {
setLocked(false);
}
delay(50);
}
void setLocked(int locked)
{
if (locked) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
//add servo turn 90 degrees.
}
}
The numbers move to the next spot like it should when pressed, but the previous numbers turn into the same weird symbol.
if(key != NO_KEY) // Do nothing if no key is pressed
Data[currentCommand++] = key;
lcd.setCursor(currentCommand,1);// set the cursor to display each character as you enter it
lcd.print(Data[currentCommand]); // displays character at set cursor
//test code
if (key == '1'){
lcd.print("1");
}
if (key == '2'){
lcd.print("2");
}
if (key == '*' || key == '#') {
position = 0;
setLocked(true);
}
if (key == secretCode[position]) {
position++;
}
You are missing curly braces for the if() statement.
If you put each { on a new line, and used Tools + Auto Format, and
a reasonable amount of while space, your code would be a lot more readable.
You don't need these anymore,
if (key == '1'){
lcd.print("1");}
if (key == '2'){
lcd.print("2");
I added the { and took out the
if (key == '1'){
lcd.print("1");
}
if (key == '2'){
lcd.print("2");
and now when I press a key the same symbol come up for each key(the same one that i have been getting for a while), but the good thing is when a key is press it goes after the previous key.
Problem solved.
Data[currentCommand] = key;
lcd.setCursor(currentCommand,1);// set the cursor to display each character as you enter it
lcd.print(Data[currentCommand]);
currentCommand++;
Thank you all so much. The lcd is displaying the numbers and the password is working. If any of you know how I could make the second LCD line(the numbers being entered) clear after a few seconds, and also have the key be cleared too. So that if the code is not entered in 5 seconds or so, then the LCD clears the numbers entered, and the key resets. You don't need to respond if you don't want to, i can work around it.
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char* secretCode = "235711";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {6,7,8,9}; //connect to the row pinouts of the keypad
byte colPins[cols] = {10,A4,A5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys),
rowPins, colPins,
rows, cols);
int redPin = 8;
int greenPin = 13;
//test code
char Data[20]; // 20 is the number of chars it can hold
int currentCommand = 0;
//test code
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Enter Password");
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,1);
//test code
if(key != NO_KEY){ // Do nothing if no key is pressed
Data[currentCommand] = key;
lcd.setCursor(currentCommand,1);// set the cursor to display each character as you enter it
lcd.print(Data[currentCommand]);
currentCommand++;
}
//test code
if (key == '*' || key == '#') {
position = 0;
setLocked(true);
}
if (key == secretCode[position]) {
position++;
}
if (position == 6) {
setLocked(false);
}
delay(50);
}
void setLocked(int locked)
{
if (locked) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(4000)
digitalWrite(redPin, HIGH)
digitalWrite(greenpin, LOW)
//add servo turn 90 degrees.
}
}
If any of you know how I could make the second LCD line(the numbers being entered)
lcd.setCursor(0,1) // second line
You will have to alter the code a bit, but you can have it so that if you press a key the first time, it stores the current millis() into a variable. Then using maybe a while loop you can take the difference of the actual current millis() and subtract it from your stored millis(). What math problem is not complete without a answer to look for, so you want " difference < 5000 ". (5000 equals 5 seconds).
Think you can code it in?
Bonus: My keypad password. (my lcd is I2C)
/*
|| @version 1.0
|| @author Andrew Mascolo
||
|| @description
|| Simple use of keypad, password and LCD
*/
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,20,4);
char Data[20]; // 20 is the number of chars it can hold
char Master[] = {'1','3','5','4','2','6'};
int currentCommand = 0;
boolean good;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {
2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
10,9,8}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Data[currentCommand] = customKey;
lcd.setCursor(currentCommand,1);
lcd.print(Data[currentCommand]);
currentCommand++;
}
if(currentCommand == 6){
for(int count = 0; count < currentCommand; count++){
if(Data[count] == Master[count]) { //needs to be tweaked a bit
good = true;
}
else {
good = false;
break; // If at any point they dont match, break out of FOR loop and display error message
}
}
lcd.setCursor(0,0);
if(good) {
lcd.clear();
lcd.print("Password is good");
clearData();
}
else {
lcd.clear();
lcd.print("Password is bad");
clearData();
}
}
}
void clearData() {
while(currentCommand !=0){ // This can be used for any array size,
Data[currentCommand--] = 0; //clear for new data
}
return;
}
Instead of having the lcd clear the numbers after a couple seconds, I decided to have the numbers clear when the * key is pressed I tried
if (key == '*' ) {
position = 0;
setLocked(true);
lcd.clear;
}
But I get an error saying "statement cannot resolve address of overloaded function. What does that mean? Also, I saw on the Arduino website/ libraries that there is a lcd.clearLine function, but it does not seem to work for me, as in the clearLine does not turn orange.
No clue what that means. Have you tried simply,
lcd.setCursor(0,1);
lcd.print(" ");