keypad and pin code

Hi all, I’m using a keypad to unlock a door with multiple PIN (7 chars each). What I need to do is:

  1. ignore every key pressed until a key is *

  2. if the 7th char inserted is # then send the stored PIN to a PHP page

2a) else reset the index and wait for * to be pressed

I need to store the pin in an array because I will have to send it to a PHP page and check if the pin is right.

this is what I’ve done so far and I guess there are few mistakes.

The 1) works but it has to be related to the 2a) and right now it’s not

the 2a) seems to be working as the index gets a reset but after that it won’t start again from 1)

#include <Keypad.h>


const byte numRows= 4;
const byte numCols= 4;

char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};

char attempt[7]={'0','0','0','0','0','0','0'}; // array to store the pressed keys
int z = 0; //array index


Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

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

  
void readKeypad(){
  char key = myKeypad.getKey();
  if(key != NO_KEY){ //if a key is pressed
    
    if(z == 0){ //if first key
      while(key != '*') //ignore everything until char * is inserted
        ;
    }
    if(z == 6){ //last key
      if(attempt[6] != '#') //if the last char is not # just restart 
        z = 0;
    }//else the pin might be right
        //send attempt to PHP
       
    attempt[z] = key; //store in array

    Serial.println(attempt[z]);
    
    z++;
  } 
}

void loop(){
  readKeypad();
}

thanks

    if(z == 0){ //if first key
      while(key != '*') //ignore everything until char * is inserted
        ;
    }

Since the value of key never changes while this loop is running, once it starts, it will never end.

You need to have a static or global boolean that is set to false initially. Set it to true when the '*' key is pressed. Set it to false when the '#' is pressed. Save data in the array only when the flag is true. Get rid of this while loop.

ok but where do I reset the index of the array (z) to 0?

void readKeypad(){
  char key = myKeypad.getKey();
  if(key != NO_KEY){
    
    if(key == '*')
      var = true;
      
    if(var){
      attempt[z] = key;
      z++;
    }

    if(key == '#')
      var = false;

    if(z == 6)
      checkPin();
  } 
}

void checkPin(){
  if(attempt[6] == '#')
    //send to PHP
    
}
void loop(){
  readKeypad();
}

thanks

ok but where do I reset the index of the array (z) to 0?

When the '#' is pressed.

cool thank you! here's the working code

void readKeypad(){
  char key = myKeypad.getKey();
  if(key != NO_KEY){
    
    if(key == '*')
      var = true;
      
    if(var){
      attempt[z] = key;
      z++;
    }
    
    if(key == '#'){
      if(z == 7)
        checkPin();
        
      var = false;
      z = 0;
      }  
    }
}

I noticed a thing. if I press * and then insert more than 6 characters before the # it will stuck and I won't be able to start again from the *.

for example

*12345# is correct *123456# is wrong but after inserting this I'm not able to start again when I press *

how can I check this?

thanks

how can I check this?

If the password must be exactly 5 digits (a silly policy, in my opinion), then simply don't record a value, or increment the index, when there are already 5 values stored.

well it was much easier..just reset the index when is equals to 7

void readKeypad(){
  char key = myKeypad.getKey();
  if(key != NO_KEY){

    if(z > 6)
      z = 0;  
      
    if(key == '*')
      var = true;
     
    if(var){
      Serial.println(z);
      attempt[z] = key;
      z++;
    }
   
    if(key == '#'){
      if(z == 7)
        checkPin();
       
      var = false;
      z = 0;
      } 
    }
}

thanks!