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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
#include <Wire.h>
#define Slave 0x01
#include <LiquidCrystal.h>
int rs = 13;
int en = 12;
int d4 = 7;
int d5 = 6;
int d6 = 5;
int d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define ligne_0 8
#define ligne_1 9
#define ligne_2 2
#define ligne_3 3
#define colonne_0 A1
#define colonne_1 A0
#define colonne_2 10
#define colonne_3 11
int quoiFaire;
byte valeurs[3];
int tailleDesDonnees = sizeof(valeurs);
boolean code_juste;
char code1[4] = {
'4','9','4','4',
};
char code2[8] = {
'1','8','1','2','0','9','5','5',
};
//TEST
union N1 {
unsigned long longN1;
byte longBytesN1[4];
};
N1 nombre1;
union N2 {
unsigned long longN2;
byte longBytesN2[4];
};
N2 nombre2;
union N3 {
unsigned long longN3;
byte longBytesN3[4];
};
N3 nombre3;
//TEST
void setup() {
lcd.begin(16, 2);
lcd.cursor();
pinMode(ligne_0, OUTPUT);
pinMode(ligne_1, OUTPUT);
pinMode(ligne_2, OUTPUT);
pinMode(ligne_3, OUTPUT);
pinMode(colonne_0, INPUT_PULLUP);
pinMode(colonne_1, INPUT_PULLUP);
pinMode(colonne_2, INPUT_PULLUP);
pinMode(colonne_3, INPUT_PULLUP);
Wire.begin();
}
void loop() {
lcd.print("Que voulez vous");
lcd.setCursor(0 ,1);
lcd.print("faire ?");
quoiFaire = oui_non();
if(quoiFaire == 'A') {
lcd.clear();
lcd.print("Option choisit:");
lcd.setCursor(0 ,1);
lcd.print("A");
delay(2000);
mdp(1);
if(code_juste == true) {
Wire.beginTransmission(Slave);
Wire.write(1);
Wire.endTransmission();
delay(2000);
rep();
lcd.setCursor(0 ,1);
lcd.print(nombre1.longN1);
delay(2000);
lcd.clear();
lcd.print(nombre2.longN2);
delay(2000);
lcd.setCursor(0 ,1);
lcd.print(nombre3.longN3);
delay(2000);
lcd.clear();
}
} else {
if(quoiFaire == 'B') {
lcd.clear();
lcd.print("Option choisit:");
lcd.setCursor(0 ,1);
lcd.print("B");
delay(2000);
mdp(1);
} else {
if(quoiFaire == 'C') {
lcd.clear();
lcd.print("Option choisit:");
lcd.setCursor(0 ,1);
lcd.print("C");
delay(2000);
mdp(1);
} else {
if(quoiFaire == 'D') {
lcd.clear();
lcd.print("Option choisit:");
lcd.setCursor(0 ,1);
lcd.print("D");
delay(2000);
mdp(2);
} else {
quoiFaire = 0;
}
}
}
}
}
char rep() {
lcd.clear();
byte tailleDesDonnees1 = sizeof(nombre1.longBytesN1);
byte tailleDesDonnees2 = sizeof(nombre2.longBytesN2);
byte tailleDesDonnees3 = sizeof(nombre3.longBytesN3);
Wire.requestFrom(Slave, tailleDesDonnees1 + tailleDesDonnees2 + tailleDesDonnees3);
delay(500);
int tailleReponse = Wire.available();
if (tailleReponse == tailleDesDonnees1 + tailleDesDonnees2 + tailleDesDonnees3) {
lcd.print("BONNE RECEPION: ");
for (byte i = 0; i < tailleDesDonnees1; i++) {
nombre1.longBytesN1[i] = Wire.read();
}
for (byte i = 0; i < tailleDesDonnees2; i++) {
nombre2.longBytesN2[i] = Wire.read();
}
for (byte i = 0; i < tailleDesDonnees3; i++) {
nombre3.longBytesN3[i] = Wire.read();
}
} else {
lcd.print("MAUVAISE RECEPTION");
while (Wire.available()) Wire.read();
}
}
char mdp(int option) {
code_juste = true;
int taille;
if(option == 1) {
taille = 4;
}
if(option == 2) {
taille = 8;
} else {
code_juste = 'error';
}
char code[taille];
if(taille == 4) {
for(int set = 0 ; set < taille ; set++ ) {
code[set] = code1[set];
}
}
if(taille == 2) {
for(int set = 0 ; set < taille ; set++ ) {
code[set] = code2[set];
}
}
lcd.clear();
lcd.print("Entrez le code:");
lcd.setCursor(0 ,1);
char code_tape[taille] = {
0
};
for(int nbr_tape = 0 ; nbr_tape < taille ; nbr_tape++ ) {
code_tape[nbr_tape] = oui_non();
lcd.print("*");
}
for(int index=0; index < taille; index++) {
if(code[index] != code_tape[index]) {
code_juste = false;
break;
}
}
lcd.clear();
if(code_juste == true) {
lcd.print("CODE BON");
}
else {
lcd.print("CODE INCORRECT");
lcd.setCursor(0, 1);
lcd.print("(ou erreur...)");
}
delay(2000);
lcd.clear();
}
byte getSeed() {
byte seed = 10;
byte i;
while(seed > 9) {
seed = 0;
i = 4;
while (i > 0) {
byte firstBit = analogRead(A0) & 0x01;
byte secondBit = analogRead(A0) & 0x01;
if (firstBit != secondBit) {
seed = (seed << 1) | firstBit;
i--;
}
}
}
return seed;
}
char oui_non() {
char bouton = NULL;
do {
bouton = bouton_presse();
} while(bouton == NULL);
delay(100);
while(bouton_presse() != NULL);
delay(100);
return bouton;
lcd.clear();
}
char bouton_presse() {
const char boutons[4][4] = {
'A','B','C','D',
'3','6','9','#',
'2','5','8','0',
'1','4','7','*',
};
for(int ligne=0;ligne<4;ligne++) {
digitalWrite(ligne_0, ligne == 0 ? LOW : HIGH);
digitalWrite(ligne_1, ligne == 1 ? LOW : HIGH);
digitalWrite(ligne_2, ligne == 2 ? LOW : HIGH);
digitalWrite(ligne_3, ligne == 3 ? LOW : HIGH);
int etat_colonne_0 = digitalRead(colonne_0);
int etat_colonne_1 = digitalRead(colonne_1);
int etat_colonne_2 = digitalRead(colonne_2);
int etat_colonne_3 = digitalRead(colonne_3);
if(etat_colonne_0 == LOW) {
return boutons[ligne][0];
}
if(etat_colonne_1 == LOW) {
return boutons[ligne][1];
}
if(etat_colonne_2 == LOW) {
return boutons[ligne][2];
}
if(etat_colonne_3 == LOW) {
return boutons[ligne][3];
}
}
return NULL;
} |
Partager