Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giltesa/ca833e8406502dde321803dfa2d47c42 to your computer and use it in GitHub Desktop.
Save giltesa/ca833e8406502dde321803dfa2d47c42 to your computer and use it in GitHub Desktop.
#include <Keypad.h>
#define COUNT(x) sizeof(x)/sizeof(*x)
HardwareSerial &pc = Serial;
class User
{
public:
String name;
String pass;
User( String name, String pass )
{
this->name = name;
this->pass = pass;
}
};
User users[] = {
User("Paco", "123A"),
User("Carmen", "456B")
};
const byte pLED = 13;
const byte pROWS[] = { 46, 48, 50, 52 };
const byte pCOLS[] = { 47, 49, 51, 53 };
const char KEYS[COUNT(pROWS)][COUNT(pCOLS)] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
Keypad keypad = Keypad( makeKeymap(KEYS), pROWS, pCOLS, COUNT(pROWS), COUNT(pCOLS) );
String password = "";
void setup()
{
pinMode(pLED, OUTPUT);
pc.begin(9600);
while(!pc);
pc.println(F("------------------ PROGRAMA INICIADO ------------------"));
pc.println(F("-- INTRODUZCA SU CLAVE Y CONFIRME CON #, BORRE CON * --"));
pc.println(F("-------------------------------------------------------"));
pc.println();
}
void loop()
{
char key = keypad.getKey();
if( key > 0 )
{
if( key != '*' && key != '#' )
{
password += key;
}
else if( key == '*' )
{
pc.println(F("Clave cancelada"));
password = "";
}
else if( key == '#' )
{
boolean found = false;
byte index;
for( index=0 ; index < COUNT(users) && !found ; index++){
found = users[index].pass.equalsIgnoreCase(password);
}
password = "";
if( !found )
pc.println(F("-> CREDENCIALES INCORRECTOS!\n"));
else
{
pc.print(F("-> BIENVENIDO "));
pc.print(users[index].name);
pc.println("!\n");
for( int i=0 ; i < 3 ; i++)
{
digitalWrite(pLED, HIGH);
delay(500);
digitalWrite(pLED, LOW);
delay(200);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment