Keypad Read

The following code compiles but when I enter the password it does not turn the LED on.
I wired my circuit correctly
Can someone give me some feedback on what I am doing wrong.
Thank you very much

#include <Password.h>
#include <Keypad.h>
Password password = Password( "1234" );
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {
    '1','2','3'    }
  ,
  {
    '4','5','6'    }
  ,
  {
    '7','8','9'    }
  ,
  {
    '*','0','#'    }
};
byte rowPins[ROWS] = {
  5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int ledPin = 13;
void setup(){
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  digitalWrite(ledPin, LOW);   // sets the LED off
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
  
void loop(){
  keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (eKey){
    case '*': guessPassword(); break;
    case '#': password.reset(); break;
      default:
        password.append(eKey);
  }
}

void guessPassword(){
  if (password.evaluate()){
    digitalWrite(ledPin,HIGH);
  }else{
    digitalWrite(ledPin,LOW);
  }
}

You shouldn’t start a new thread for every question if you have on the same topic:

It’s impossible to follow what was said and people will give up on answering to your questions.

Ok thanks for the advice.

Now I am trying the following code for the same thing trying to get an input from keypad: say 200 and compare that with the one we are looking for and do something (turn a pin high)
When I look at the serial monitor it’s just printing stuff.
Any help?

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {
    '1','2','3'          }
  ,
  {
    '4','5','6'          }
  ,
  {
    '7','8','9'          }
  ,
  {
    '*','0','#'          }
};
byte rowPins[ROWS] = {
  9, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


char str[3];
int pin =13;



void setup(){
  Serial.begin(9600);
}


void loop(){

  for (int i = 0; i < 3; i++){
    char key = keypad.getKey();
    if (key != NO_KEY){
      str[i]=key;  
    }
  }
  Serial.print(str);
  
}

I didn't get any useful help so far

When I look at the serial monitor it's just printing stuff.

If you don't show us the output, we can't help you.

I didn't get any useful help so far

I wonder why?

Now I am trying this code to get the password working but when I run it the serial monitor just prints what I press no comparison to my password 1234
Here is the code if someone can help.

//keypad with password
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {
    '1','2','3'            }
  ,
  {
    '4','5','6'            }
  ,
  {
    '7','8','9'            }
  ,
  {
    '*','0','#'            }
};
byte rowPins[ROWS] = {
  10, 4, 3, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char inputArray[4]; //array to gather user keypad presses
char currentPassword[4] = {'1','2','3','4'}; //array to hold keypad password
#define ledPin 13 //led to register keypad pressses
#define registerPin 2 //led that registers correct password entry
int i = 0;
void setup()
{
  Serial.begin(9600); //open serial porrt
  pinMode(ledPin, OUTPUT); //define led pin as output
  pinMode(registerPin, OUTPUT); //define led pin as output
}
void loop()
{
  char key = keypad.getKey();
  //if a key is pressed
  if(key)
  {
    //turn on ledPin
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
    inputArray[i] = key; //store entry into array
    i++;
    //Serial.println(key); //print keypad character entry to serial port
    if (i == 4) //if 4 presses have been made
    {
      //match input array to password array
      if (inputArray[0] == currentPassword[0] &&
        inputArray[1] == currentPassword[1] &&
        inputArray[2] == currentPassword[2] &&
        inputArray[3] == currentPassword[3])
      {
        digitalWrite(registerPin, HIGH); //turn on registerPin led
        delay(2000);
        digitalWrite(registerPin, LOW);
      }
      i=0; //reset i
    }
  }
}

There is a small glitch in this code:

#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#include <Servo.h>


Servo myservo;
Password password = Password( "1234" );

const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 10, 4, 3, 12 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
myservo.attach(9); //servo on digital pin 9
Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
  Serial.print("Pressed: ");
  Serial.println(eKey);
  switch (eKey){
   case '*': checkPassword(); break;
   case '#': password.reset(); break;
   default: password.append(eKey);
}
}
}

void checkPassword(){
if (password.evaluate()){
   Serial.println("Success");
   //Add code to run if it works
   myservo.write(160); //160deg
   digitalWrite(11, HIGH);
   delay(500);
   digitalWrite(11, LOW);
}else{
   Serial.println("Wrong");
   //add code to run if it did not work
   myservo.write(10); //10 deg
   digitalWrite(12, HIGH);
   delay(500);
   digitalWrite(12, LOW);
}
}

the error is that the serial monitor prints wrong even though I press the right sequence of keys which is 1234 and * to validate.
Thank you guys
Here is a print of the serial monitor

Pressed: 1
Pressed: 2
Pressed: 3
Pressed: 4
Pressed: *
Success
Pressed: 1
Pressed: 2
Pressed: 3
Pressed: 4
Pressed: *
Wrong
Pressed: 1
Pressed: 2
Pressed: 3
Pressed: 4
Pressed: *
Wrong

Your test for a key is incorrect. It should be:

if(key != NO_LEY)

You really should adapt one of the examples, all of which use a keypadEvent callback to handle key presses.

From the code you posted and the output you posted it seems that:
-it works first time through

  • it does not work after that

Is that repeatable?

So when do you clear the password buffer? I cannot see that anywhere. Do you need to explicitly clear it, or does the
library clear it on some event?