Como comparar una clave 3 dígitos incluida en código de 8 dígitos.

Hola a todos,,
alguien me podria ayudar a solucionar este problema de codigo,,
pues soy novato en el tema de programacion y estoy batallando para lograr que mi codigo haga lo que yo deseo,,,

la idea del programa es esta:
ingresar por medio de un teclado 4x4 un codigo de 8 digitos, los tres primeros deben corresponder a una clave (225) y el resto de digitos son para un modulo de control diferente

por ejemplo ingreso el codigo 22587414
inicialmente quiero validar esta clave pero el programa siempre me arroja (" incORRECTO");
pero deberia ser correcto puesto que he ingresado 225

alguien me puede decir que error tengo en el codigo??
muchas gracias

el codigo es este:

// include the library code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x3f,2,1,0,4,5,6,7,3, POSITIVE);
#include <Keypad.h>
char key;
char buffer[5];
int i; //loop key counter for counting number of keys pressed
int x; //used to multiply each digit entered by 1 then 10 then 100 etc

/*
keypad section note include library #include <keypad.h> after copying library in arduino\library
then define coulms in example code below
*/

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char codigoSecreto[] = {'2','2','5'};
int bandera=0;
int estado=0;

void setup(){
Serial.begin(9600);
// set up the LCD's number of columns and rows: 
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
 lcd.clear();
 delay(1000);
 

}

void loop(){
i = 0;
buffer[0] = '\0';
key = keypad.getKey();

 if ( key != NO_KEY)
  {
    lcd.print(key);
    buffer[i] = key;
    i++;
    //buffer[i]= '\0';
    Serial.print(buffer);
    }


if (key == '#')
   {
     lcd.setCursor(1, 1);
     delay(2000);
     lcd.setCursor(1, 1);
     delay(1000);
     lcd.clear();

   comparar_codigo();
   
}
}

char getChar()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
   return key;
}
else { 
return NO_KEY;
 }
}

void comparar_codigo (){
int c=0;
if (buffer[0]==codigoSecreto[0])
  {
     c=1;
     if (buffer[1]==codigoSecreto[1])
     {
       c=2;
       if (buffer[2]==codigoSecreto[2]){
        c=3;
        bandera=1; 
        Serial.println(" CORRECTO"); 
        }
      }
    }
   else 
  Serial.println(" incORRECTO"); 
   
}

ejemplo__cadenas.ino (1.99 KB)

Usa Strings para la comparación y dentro de los strings usa substrings

ejemplo

  String stringOne = "Content-Type: text/html";

  // you can also look for a substring in the middle of a string:
  if (stringOne.substring(14,18) == "text") {

  }

En tu caso

// include the library code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x3f,2,1,0,4,5,6,7,3, POSITIVE);
#include <Keypad.h>
char key;
String buffer ="";
int i; //loop key counter for counting number of keys pressed
int x; //used to multiply each digit entered by 1 then 10 then 100 etc

/*
keypad section note include library #include <keypad.h> after copying library in arduino\library
then define coulms in example code below
*/

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
                        {'1','2','3','A'},
                        {'4','5','6','B'},
                        {'7','8','9','C'},
                        {'*','0','#','D'}
                        };
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String codigoSecreto = "225";
int bandera=0;
int estado=0;

void setup(){
    Serial.begin(9600);
    // set up the LCD's number of columns and rows: 
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("hello, world!");
    lcd.clear();
    delay(1000);
}

void loop(){
    i = 0;
    buffer[0] = '\0';
    key = keypad.getKey();

     if ( key != NO_KEY)  {
        lcd.print(key);
        buffer[i++] = key;
        Serial.print(buffer);
    }

    if (key == '#')      {
        lcd.setCursor(1, 1);
        delay(2000);
        lcd.setCursor(1, 1);
        delay(1000);
        lcd.clear();
        bandera = 0;
        if (buffer.substring(1,3) == codigoSecreto) {
            bandera=1; 
            Serial.println(" CORRECTO");        
        }
        else 
            Serial.println(" incORRECTO"); 
    }
}

char getChar() {
    char key = keypad.getKey();
    if (key != NO_KEY){
       return key;
    }
    else { 
    return NO_KEY;
 }
}

Prueba a ver si funciona. ME da algun error pero en la librería LCD que no es como la que yo tengo.

Muchas gracias surbyte
monte el codigo y aunque en la compilacion no aparecen errores, no logro ver los numeros ingresados en el LCD y en el monitor serie,

en este hilo: Keypad library - Getting String instead of Character - FYI - Displays - Arduino Forum
parece ser que el teclado genera char,
no se estoy confundido,
conoces del problema?? porque no logro leer los datos con tu codigo utilizando los String

Y se ve CORRECO o INCORRECTO?
yo simplemente me base en tu código y en tu código no envias nada al LCD.
Yo estoy comparando tu entrada por teclado con el password y te informo via SErial (tal como dice tu código) si es correcto o incorrecto. Nada mas.