1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
//initialisation du clavier
#include <Keypad.h> //include keypad library - first you must install library (library link in the video description)
const uint8_t rows = 4; //number of the keypad's rows and columns
const uint8_t cols = 4;
char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
uint8_t rowPins [rows] = {13, 12, 11, 10}; //pins of the keypad
uint8_t colPins [cols] = {9, 8, 7, 6};
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
//definition des pins
const uint8_t redLEDPin = 10;
const uint8_t blueLEDPin = 11;
// On définit le type crypto
struct t_crypto {
const char* ID;
const char* Red;
const char* Blue;
};
t_crypto Code[] = {
{"A1C2", "8AA4", "7C9A"},
{"C312", "651A", "8A52"},
{"AA98", "74A1", "8C8A"},
{"7A54", "A35C", "55C2"},
{"3A3C", "3A3C", "01A5"},
{"3C5A", "AAAA", "80C4"},
{"648C", "CC1C", "92C1"},
{"9761", "C7A4", "3333"}
};
const uint8_t nbCodes = sizeof(Code) / sizeof(Code[0]);
const char* password ;
int passwordLength;
void choisirCodeAleatoire()
{
long carteCrypto = random(0, nbCodes);
Serial.print(F("Crypto carte: "));
Serial.println(carteCrypto);
Serial.println(Code[carteCrypto].ID);
Serial.println(Code[carteCrypto].Red);
Serial.println(Code[carteCrypto].Blue);
password = Code[carteCrypto].Red;
passwordLength = strlen(password);
Serial.println(F("Tapez le code"));
}
void setup ()
{
Serial.begin(115200);
pinMode(redLEDPin, OUTPUT); //set the LED as an output
pinMode(blueLEDPin, OUTPUT);
randomSeed(analogRead(A0)); // génération d'un peu d'aléatoire
choisirCodeAleatoire();
}
void loop ()
{
char code_tape[passwordLength + 1] = {'\0'}; //tableau contenant la saisie, +1 pour un caractère NULL de fin de chaîne avec initialisation vide
uint8_t indice = 0;
while (indice < passwordLength) {
char whichKey = myKeypad.getKey(); //define which key is pressed with getKey
if (whichKey != NO_KEY) {
code_tape[indice] = whichKey;
Serial.print("*");
indice++;
}
}
code_tape[passwordLength] = '\0'; // pour faire une cString bien formée (superflu car intialisé déjà avec des '\0', donc plus à titre de bonne pratique)
if (strcmp(code_tape, password) == 0) { // on peut utilsier strcmp() car les deux paramètres sont des cStrings bien formées
Serial.println(F("\ncode OK\n"));
choisirCodeAleatoire(); // on relance un jeu
} else {
Serial.println(F("\ncode Faux"));
}
} |
Partager