Skip to content

Commit 90e6a6a

Browse files
committed
Dormitory 3 fully working
1 parent 31ccf6f commit 90e6a6a

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

Margoulineur2000.ino

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ int encoderWrite()
166166
else
167167
{
168168
lcd.clear();
169-
lcd.print("New Balance :");
169+
lcd.print("New balance :");
170+
lcd.setCursor(0,1);
170171
lcd.print(newPosition / 4);
172+
171173
oldPosition = newPosition;
172174
Serial.println(newPosition / 4);
173175
}
@@ -186,16 +188,14 @@ void nfc_write()
186188
uint8_t currentblock; // Counter to keep track of which block we're on
187189
bool authenticated = false; // Flag to indicate if the sector is authenticated
188190
uint8_t data[16] = {0x04, 0x0A, 0x52, 0x18, 0x7B, 0x2C, 0x10, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x55, 0x43}; // Array to store block data during reads
189-
uint8_t savedData[16];
190-
int currentBalance = 0;
191+
uint8_t savedData[16];
191192

192-
int value = encoderWrite() * 100;
193+
int newBalance = encoderWrite() * 100;
193194

194-
195-
Serial.print("New value = ");Serial.println(value);
195+
Serial.print("New value = ");Serial.println(newBalance);
196196
uint8_t balance[] = {0, 0};
197-
balance[0] = value / 256;
198-
balance[1] = value - (balance[0] * 256);
197+
balance[0] = newBalance / 256;
198+
balance[1] = newBalance - (balance[0] * 256);
199199
Serial.print("Hex 1 = ");Serial.println(balance[0], HEX);
200200
Serial.print("Hex 2 = ");Serial.println(balance[1], HEX);
201201
digitalWrite(writeLedPin, HIGH);
@@ -206,8 +206,8 @@ void nfc_write()
206206

207207
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
208208

209-
if (success) {
210-
// Display some basic information about the card
209+
if (success)
210+
{
211211
Serial.println("Found an ISO14443A card");
212212
lcd.clear();
213213
lcd.print("I Found a card !");
@@ -267,8 +267,32 @@ void nfc_write()
267267
}
268268
else
269269
{
270-
if (currentblock == 24)
270+
if (currentblock == 24 || currentblock == 26 )
271271
{
272+
nfc.mifareclassic_ReadDataBlock(currentblock, data);
273+
data[6] = balance[0];
274+
data[7] = balance[1];
275+
success = nfc.mifareclassic_WriteDataBlock(currentblock, data);
276+
if (success)
277+
{
278+
Serial.print("Block ");Serial.print(currentblock, DEC);
279+
if (currentblock < 10)
280+
{
281+
Serial.print(" ");
282+
}
283+
else
284+
{
285+
Serial.print(" ");
286+
}
287+
nfc.PrintHexChar(data, 16);
288+
289+
}
290+
}
291+
else if (currentblock == 25)
292+
{
293+
nfc.mifareclassic_ReadDataBlock(currentblock, data);
294+
data[5] = balance[0];
295+
data[6] = balance[1];
272296
success = nfc.mifareclassic_WriteDataBlock(currentblock, data);
273297
if (success)
274298
{
@@ -300,15 +324,12 @@ void nfc_write()
300324
Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
301325
}
302326
}
303-
// Wait a bit before trying again
304-
// Serial.println(savedData[0], HEX);
305-
// Serial.println(savedData[1], HEX);
306-
327+
307328
lcd.clear();
308-
lcd.print("Balance :");
329+
lcd.print("New balance :");
309330
lcd.setCursor(0,1);
310-
lcd.print(currentBalance);
311-
Serial.println(currentBalance, DEC);
331+
lcd.print(newBalance / 100);
332+
Serial.println(newBalance, DEC);
312333
Serial.println("\n\nDONE ! waiting 4 the button");
313334
digitalWrite(writeLedPin, LOW);
314335
wait4button();
@@ -439,22 +460,17 @@ void nfc_read()
439460
Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!");
440461
}
441462
}
442-
// Wait a bit before trying again
443-
// Serial.println(savedData[0], HEX);
444-
// Serial.println(savedData[1], HEX);
445-
446463
lcd.clear();
447464
lcd.print("Balance :");
448465
lcd.setCursor(0,1);
449-
lcd.print(currentBalance);
466+
lcd.print(currentBalance / 100);
450467
Serial.println(currentBalance, DEC);
451468
Serial.println("\n\nDONE ! waiting 4 the button");
452469
digitalWrite(readLedPin, LOW);
453470
wait4button();
454471
lcd.clear();
455472
Serial.print("end read = ");Serial.println(oldPosition);
456473

457-
458474
Serial.flush();
459475
}
460476

0 commit comments

Comments
 (0)