Need help to understand Keypad and password

Hello,

I've come to ask you for help with a code to make a password. I don't understand very well how it works especially from the first If loop. If someone could explain it to me that would be very kind. In the If loop it is especially the "While" that I do not understand:

          a=keypad.getKey();

   while(a == NO_KEY)

          a=keypad.getKey();

          Serial.print(a);

What does the "NO_KEY" correspond to?
What does the "kepad.getKey" correspond to?
why write "a=keypad.getKey();" before and after the "While"?
why do the whole if loop like this?

Concerning the Can part, what does CAN.sendMsgBuf (0x10, 0, 1, Value); correspond to?

#include <Keypad.h>
#include <SPI.h>
#include "mcp_can.h"





const byte ROWS = 4;

const byte COLS = 3;

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

int Code;

unsigned char Valeur[8]; 

byte rowPins[ROWS] = {7, 5, 3, 2};

byte colPins[COLS] = {4, 9, 8};

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

void setup(){ 
  
  Serial.begin(115200); // 
  
     delay(100);

START_INIT:


  if(CAN_OK == CAN.begin(CAN_500KBPS))  
   {
        Serial.println("CAN BUS Shield init ok!");
  }
   else
  {
    Serial.println("CAN BUS Shield init fail");
      
     Serial.println("Init CAN BUS Shield again");
   delay(100);
    
      goto START_INIT;
  
   }
 }
 



 
void loop(){
 
char a;
char b;
char c;
char d;


 
  char key = keypad.getKey();
  
  
   if (key =keypad.getKey()) {
    
   

               Serial.println ( "");
     
       
       while(key != NO_KEY)key=keypad.getKey();
  
              Serial.println(" taper le mot de passe ");
 
              a=keypad.getKey();
   
       while(a == NO_KEY)
  
              a=keypad.getKey();
  
              Serial.print(a);
 
              b=keypad.getKey();
  
       while(b == NO_KEY)
  
              b=keypad.getKey();
  
              Serial.print(b);
  
              c=keypad.getKey();
  
       while(c == NO_KEY)
  
              c=keypad.getKey();
  
              Serial.print(c);
  
              d=keypad.getKey();
  
        while(d == NO_KEY)
  
             d=keypad.getKey();
  
             Serial.print(d);
 
             Serial.println ("");
 
      if (a=='2' && b=='6' && c=='0' && d=='1') {
      
            Serial.println(" code valide");
      
            Serial.println("démarrage en cours... ");

            Code=1;
      
                    }
          
     if (a!='2' | b!='6' | c!='0' | d!='1'){
 
           Serial.println("Code erroné ");

            Code=0;
            
                    }

          Valeur[0]=Code; 
  CAN.sendMsgBuf(0x10, 0, 1, Valeur); 

delay(500);

      
  }

  
  }

NO_KEY == 0, the value you get from keypad.getKey() when no key has been pressed.

It's the function you call to see which key, if any, has been pressed.

Before the 'while' it initializes 'a' so it can be tested in the 'while' loop. It is after the 'while' to check again. This would more properly be written as:

  do
  {
    a = keypad.getKey();
  } 
  while ( a == NO_KEY);

The 'while' loop? To repeatedly check the keypad until someone presses a key.

Sends a message to a device on the CAN (Controller Area Network) bus. What that does depends on what the device is.

thank you very much. Could you also tell me what is this line for :

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

And also, why do we need to write this like that, do we have to define the keys like that ? :

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

Those lines define what character gets returned by the keypad.getKey() function. If your keypad is a standard 4x4 matrix, then this is the default. If you wanted to place different labels on your keypad, you could do that and define which key press corresponds to what character is returned.

No. The 'makeKeymap' macro just turns the 2D array into a 1D array so you could write it as:
char keys[] = "123456789*0#";

See Keypad mot de passe

This Topic has been locked as it is the identical or too similar your other topic.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.