Switchcase Password execution with Keypad

Hello,

im pretty new to Arduino and programming.

I got the following Code which works, but not like intended, it should ask me for a password after pressing 1 on the 4x4 Keypad. Somehow it stops after "Serial.print(Taste);" and I have no idea why.

What I want my programm to do:

If I press 1 on the Keypad it should execute Case 1, which is powering a LED and asking me for a password that also gets typed in over the Keypad

#include <Keypad.h>
#define LED1 10

//Hier wird die größe des Keypads definiert
const byte COLS = 4; //4 Spalten
const byte ROWS = 4; //4 Zeilen
//Die Ziffern und Zeichen des Keypads werden eingegeben:
char hexaKeys[ROWS][COLS] = {
  {'D', '#', '0', '*'},
  {'C', '9', '8', '7'},
  {'B', '6', '5', '4'},
  {'A', '3', '2', '1'}
};

byte colPins[COLS] = {2, 3, 4, 5}; //Definition der Pins für die 4 Spalten
byte rowPins[ROWS] = {6, 7, 8, 9}; //Definition der Pins für die 4 Zeilen
char Taste; //Taste ist die Variable für die jeweils gedrückte Taste.
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld" angesprochen werden


const String password = "9999"; // change your password here
String input_password;



void setup()
{
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  delay(1000);


}

void loop()
{
  char Taste = Tastenfeld.getKey();

  if (Taste)
  {
    switch (Taste)
    {
      case '1':
        digitalWrite(LED1, HIGH);
        Serial.println("LED wurde angeschalten");
        if (Taste == '1') {
          Serial.println("Type Password");
          Serial.print(Taste);
          input_password = ""; // clear input password
        } else if (Taste == '#') {
          if (password == input_password) {
            Serial.println("password is correct");

            // DO YOUR WORK HERE

          } else {
            Serial.println("password is incorrect, try again");
          }

          input_password = ""; // clear input password
        }
        break;

      case '2':
        digitalWrite(LED1, LOW);
        Serial.println("Led wurde ausgeschalten");
        break;


    }
  }
}

You do this

switch (Taste)
    {
      case '1':
         ...
        if (Taste == '1') {
           ...
        } else if (Taste == '#') {
           ...
          } else {
           ...
          }
        }
        break;

➜ if you enter the first case, Taste is '1', how do you think it could be anything else magically afterwards since you did not change it? so your if is useless, there is no chance it can be '#' or anything else than '1'

Hi @lyrix1234
try this sketch:

RV mineirin

#include <Keypad.h>
#define LED1 10

//Hier wird die größe des Keypads definiert
const byte COLS = 4; //4 Spalten
const byte ROWS = 4; //4 Zeilen
//Die Ziffern und Zeichen des Keypads werden eingegeben:
char hexaKeys[ROWS][COLS] = {
  {'D', '#', '0', '*'},
  {'C', '9', '8', '7'},
  {'B', '6', '5', '4'},
  {'A', '3', '2', '1'}
};

byte colPins[COLS] = {2, 3, 4, 5}; //Definition der Pins für die 4 Spalten
byte rowPins[ROWS] = {6, 7, 8, 9}; //Definition der Pins für die 4 Zeilen
char Taste; //Taste ist die Variable für die jeweils gedrückte Taste.
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld" angesprochen werden
const String password = "9999"; // change your password here
String input_password;
bool flag = false;
//--------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  delay(1000);
}
//--------------------------------------------------------------------------------
void loop() {
  char Taste = Tastenfeld.getKey();

  if (flag == true)
  {
    if (Taste != '#')
    {
      input_password += (String)Taste;
    }
  }

  switch (Taste)
  {
    case '1':
      if (flag == false)
      {
        digitalWrite(LED1, HIGH);
        Serial.println("LED wurde angeschalten");
        flag = true;
        Serial.println("Type Password");
        Serial.println(Taste);
        input_password = ""; // clear input password
      }
      break;

    case  '#':
      if (password == input_password)
      {
        // Serial.println(input_password);
        flag = false;
        Serial.println("password is correct");
        // DO YOUR WORK HERE
      }
      else
      {
        //Serial.println(input_password);
        flag = false;
        Serial.println("password is incorrect, try again");
      }
      input_password = ""; // clear input password
      break;

    case '2':
      digitalWrite(LED1, LOW);
      Serial.println("Led wurde ausgeschalten");
      break;
  }
}

Thanks man, it works excatly how I wanted it to!

Can you maybe explain to me how you did it?

First I created a flag to identify if the number 1 had already been typed.
Then I released save the password in string format only after the number 1 is typed.
I changed the structure of witch to create "a state machine".
PS:
I changed again the sketh removing an unnecessary if.
(if (Taste) ).
I've already posted the new version with this modification.

RV mineirin

Here is an example I wrote a while back to do numeric passwords from keypad.

#include <Keypad.h>

// Combination is "08675309" 
// Use any value up to 4 billion

// NOTE: 08675309 is not a valid number and will
// not compile.  The leading zero means 'octal 
// constant' and the digits 8 and 9 are not valid
// octal digits. 
//
// To get leading zeroes, set CombinationLength
// higher than the number of digits

const unsigned long Combination = 8675309;
const byte CombinationLength = 8; 

const byte ROWS = 4; // set four rows
const byte COLS = 4; // set four columns
const char keys[ROWS][COLS] =   // Define the keymap
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = { 36, 34, 32, 30 };
byte colPins[COLS] = { 28, 26, 24, 22 };
// Create the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long InputValue = 0;
byte InputLength = 0;

void setup() {
  Serial.begin(115200);
  pinMode(46, OUTPUT); // Set green LED as output
  pinMode(48, OUTPUT); // Set red LED as output
}

void loop()
{
  char key = keypad.getKey();

  switch (key)
  {
    case '*':  // Clear
      InputValue = 0;
      InputLength = 0;
      break;

    case '0'...'9':
      InputValue *= 10;
      InputLength++;
      InputValue += key - '0';
      break;

    case '#': // Enter
      if (InputLength == CombinationLength && InputValue == Combination)
      {
        Serial.println("Success, Come inside"); // If the password is correct
        digitalWrite(46, HIGH); // Turn on green LED
        delay(500); // Wait 5 seconds
        digitalWrite(46, LOW); // Turn off green LED
      }
      else
      {
        Serial.println("Access Denied"); // If the password is incorrect
        digitalWrite(48, HIGH); // Turn on red LED
        delay(500); // Wait 5 seconds
        digitalWrite(48, LOW); // Turn off red LED
      }
      InputValue = 0;
      InputLength = 0;
      break;

  }  // switch(key)
}  // loop()