Password Activated LED; code not working.

#include <Password.h>

#include <Keypad.h>


const byte rows = 4; //four rows
const byte cols = 3; //three columns

Password password = Password ( "8776" );

char keys[rows][cols] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte currentLength = 0 ;

byte rowPins[rows] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad

byte colPins[cols] = {6, 7, 8}; //connect to the column pinouts of the keypad

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

#define GREENLedPin 11
#define REDLedPin 12



void setup()
{
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(12, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(11, LOW);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if (key);
  Serial.print(key);
  if (Serial.available()) {
    char input = Serial.read();
    switch (input) {
      case '*': //reset password
        password.reset();
        currentLength = 0;

        break;
      case '#': //evaluate password
        if (password.evaluate()) {

          digitalWrite(11, HIGH);
          delay(2000);
          digitalWrite(11, LOW);
          Serial.print("Correct");
        } else {
          password.reset();
        }
        Serial.print("Incorrect");
        digitalWrite(12, HIGH);
        delay(2000);
        digitalWrite(12, LOW);
        char key = kpd.getKey();

    }








  }
}

This code isn’t working at all, i want it to activate a green LED when the correct password is entered and a red LED when the password is incorrect then it automatically resets the password.
I want the * key to reset the password
and the # key to evaluate the password
Both of these are keys on the keypad…

Could someone adjust this code so it works?

void loop()
{
  char key = kpd.getKey();
  if (key);
  Serial.print(key);
  if (Serial.available()) {
    char input = Serial.read();
    switch (input) {
      case '*': //reset password

Do you want to the user to press * or # on the keyboard or send it from the serial monitor?

From your description it sounds like you want to look at key that was read from the keypad. So why are you reading from serial and then using that for your switch case instead of the key that was read from the keypad?

You probably also need some cases for the digits to add them to the password guess. Right now you only have a way to evaluate it, but no way to actually enter the password.

I recognize this code. We've already worked through this exact code. Where did you get this from? Are you the same poster that posted it before?

Why you never reply here:

or here:

Delta_G:
Why you never reply here:

Password activated LED - Programming Questions - Arduino Forum

or here:

case '#' evaluate password - Programming Questions - Arduino Forum

not sure how to find old posts, new to the website not sure how to navigate it..
My apologies.

MARSL15:
not sure how to find old posts, new to the website not sure how to navigate it…
My apologies.

Should we even bother trying to answer here if you’re just going to run off and forget about it and start another thread?

Click on your username next to your post and it will take you to your profile where you can look at all your posts.

Or even better, just bookmark it so you can come back. No special knowledge of the forum needed, just a little bit of thought. Please don’t waste people’s time. That’s not very nice.

Are you going to address any of the issues in your code now that they've been pointed out numerous times?

Delta_G:
You probably also need some cases for the digits to add them to the password guess. Right now you only have a way to evaluate it, but no way to actually enter the password.

I recognize this code. We've already worked through this exact code. Where did you get this from? Are you the same poster that posted it before?

How would i add this function, also i would like it to be read from the keypad not serial monitor, only thing i want the monitor to do is display the keys that are pressed and whether the password is correct or not..

So would i just change the input in the void loop? What would i change it to though?

MARSL15:
So would i just change the input in the void loop? What would i change it to though?

OK, let’s apply just a little bit of thought here. It’s not too hard to see.

void loop()
{
  char key = kpd.getKey();
  if (key);
  Serial.print(key);
  if (Serial.available()) {
    char input = Serial.read();
    switch (input) {
      case '*': //reset password

You’ve got a variable named key that is storing the key that was pressed and a variable named input that came from Serial.

So key has the key and input has the serial.

key = keypad
input = from serial

So your switch case, do you want that to look at key or input? The one from the keypad or the one from the Serial line.

Hopefully you can answer that.

Now look at your switch statement.

switch (input) {

I don’t think you need to be an expert at programming to see which one you chose. Was it the right one? Remember, key came from the keypad and input came from the Serial line. Which one do you want to use here?

Again, just a tiny bit of thought here, if you don’t intend for the user to ever enter anything by serial, then will there be any reason for your code to READ from the serial line? So should there be any Serial.read() lines in your code? This doesn’t take an expert on coding. Just a little common sense. Does it make sense now?

As for dealing with digits, have you looked at any password code on the internet? This project has been done hundreds if not thousands of times over and it’s posted everywhere.

You have your switch with cases for * and #, why couldn’t it have cases for digits? Is it not pretty obvious how to do that? Do you understand what the switch case does at all? Maybe you should read up on that before going much further.

Delta_G:
OK, let's apply just a little bit of thought here. It's not too hard to see.

void loop()

{
  char key = kpd.getKey();
  if (key);
  Serial.print(key);
  if (Serial.available()) {
    char input = Serial.read();
    switch (input) {
      case '*': //reset password




You've got a variable named key that is storing the key that was pressed and a variable named input that came from Serial. 

So key has the key and input has the serial. 

key = keypad
input = from serial

So your switch case, do you want that to look at key or input? The one from the keypad or the one from the Serial line. 

Hopefully you can answer that. 

Now look at your switch statement.



switch (input) {




I don't think you need to be an expert at programming to see which one you chose. Was it the right one? Remember, key came from the keypad and input came from the Serial line. Which one do you want to use here? 

Again, just a tiny bit of thought here, if you don't intend for the user to ever enter anything by serial, then will there be any reason for your code to READ from the serial line? So should there be any Serial.read() lines in your code? This doesn't take an expert on coding. Just a little common sense. Does it make sense now?

As for dealing with digits, have you looked at any password code on the internet? This project has been done hundreds if not thousands of times over and it's posted everywhere. 

You have your switch with cases for * and #, why couldn't it have cases for digits? Is it not pretty obvious how to do that? Do you understand what the switch case does at all? Maybe you should read up on that before going much further.

had already changed that before i read yours, thinking ahead..

How do i write the code for the cases for the other digits though,
what will be written underneath the case ' ': part of the code for every digit on the keypad?

switch function has been changed to key, input char has been removed, when i click the hash on the keypad i the red LED lights up but due to the cases for other numbers not being put in yet i cant enter the password.

So how could i write these cases?

Look at the examples using that password library you’re using. What do they do with the digits of the password? There has to be a line that adds a digit to the password. That’s what needs to go there.

The statement "if (key) ;" does nothing.

You should probably start over with:
File->Examples->Password->Examples->PasswordKeypad