Pages: 1 2 3 [4]   Go Down
Author Topic: LCD and Keypad help  (Read 3297 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2926
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In reply #42, I gave you the answer.

Does this need to be double == or single = ?
Quote
int currentcommand == 0
And why are you declaring "int" again?
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh okay I see when you gave it to me, sorry.  Also the int command thing was completely made up just to give you an example.  Also where would i put the
Code:
void clearData() {
  while(currentCommand !=0){   // This can be used for any array size,
    Data[currentCommand--] = 0; //clear for new data
  }
  return;
}
in this code
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 = 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 == '*' ) {
    position = 0;
    setLocked(true);
   lcd.clear();
  lcd.setCursor(0,0);
 lcd.print("Enter Password");
lcd.setCursor(0,1);
  }
 
  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);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Access Granted");
    delay(3000);
    lcd.clear();
    lcd.print("Enter Password");
    lcd.setCursor(0,0);
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    //add servo turn 90 degrees.
  }
}
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2926
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can put it anywhere, it's just used to ensure the data array is cleared. To be honest, it's really not needed, unless your password is shortened.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I put
Code:
void clearData() {
  while(currentCommand !=0){   // This can be used for any array size,
    Data[currentCommand--] = 0; //clear for new data
  }
  return;
}
In my void loop and i get an error saying "A function-definition is not allowed here before '{' token".   Do you know how I can fix this.  Also,  wouldn't this code bit make more sense if it were put in
Code:
if (key == '*' ) {
    position = 0;
    setLocked(true);
   lcd.clear();
  lcd.setCursor(0,0);
 lcd.print("Enter Password");
lcd.setCursor(0,1);
  }
here so that when the button is press, currentcommand changes to zero, not always.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2926
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It does not go in the loop, it goes outside the loop. And if you put it in there, it will not work. Just set current command back to when your done with it the first time. I'll post me updated code later, and just learn from that.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I put the code bit outside of void loop and setup, and i sent it to my arduino, but the numbers still continue from where the others left off after pressing *.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2926
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here,
Code:
/*
 || @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;
char customKey;

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(){
  lcd.setCursor(0,0);
  lcd.print("Enter Password");
  customKey = customKeypad.getKey();
  if (customKey){
    Data[currentCommand] = customKey;
    lcd.setCursor(currentCommand,1);
    lcd.print(Data[currentCommand]);
    currentCommand++;
  }

  if(currentCommand == 6){
    delay(1000);
    for(int count = 0; count < currentCommand; count++){
      if(Data[count] == Master[count]) {
        good = true;
      }
      else {
        good = false;
        break;
      }
    }

    lcd.setCursor(0,0);
    if(good) {
      lcd.clear();
      lcd.print("Password is good");
      delay(1000);
      lcd.clear();
      clearData();
    }
    else {
      lcd.clear();
      lcd.print("Password is bad");
      delay(1000);
      lcd.clear();
      clearData();
    }
  }
  if(customKey == '*'){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Change Password");
    clearData();
    while(customKey != '#'){
      customKey = customKeypad.getKey();
      if (customKey){
        Master[currentCommand] = customKey;
        lcd.setCursor(currentCommand,1);
        lcd.print(Master[currentCommand]);
        currentCommand++;
      }
    }
    if(customKey == '#') {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Master is reset");
      delay(1000);
      clearData();
      lcd.clear();
    }
  }
}
void clearData() {
  while(currentCommand !=0){   // This can be used for any array size,
    Data[currentCommand--] = 0; //clear for new data
  }
  return;
}
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much for sending that.  I modified it and now everything is working, once again, thank you.
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: