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)