Created
February 28, 2020 23:17
-
-
Save enkiusz/7ed008caec6955e2ed028d0cf10dec71 to your computer and use it in GitHub Desktop.
Pełen kod RFID + klawiatura
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Typical pin layout used: | |
* ----------------------------------------------------------------------------------------- | |
* MFRC522 Arduino Arduino Arduino Arduino Arduino | |
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro | |
* Signal Pin Pin Pin Pin Pin Pin | |
* ----------------------------------------------------------------------------------------- | |
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST | |
* SPI SS SDA(SS) 10 53 D10 10 10 | |
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 | |
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 | |
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 | |
*/ | |
#include <SPI.h> | |
#include <MFRC522.h> | |
MFRC522 mfrc522(10, 9); | |
void setup() { | |
Serial.begin(9600); // Initialize serial communications with the PC | |
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) | |
SPI.begin(); // Init SPI bus | |
mfrc522.PCD_Init(); // Init MFRC522 | |
delay(4); // Optional delay. Some board do need more time after init to be ready, see Readme | |
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details | |
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks...")); | |
pinMode(4, OUTPUT); | |
digitalWrite(4, 1); | |
pinMode(3, OUTPUT); | |
} | |
byte correct_card[] = { 0xC6, 0x69, 0x79, 0x89 }; | |
#include <Keypad.h> | |
const byte ROWS = 4; | |
const byte COLS = 4; | |
char keys[ROWS][COLS] = { | |
{'1','2','3', 'A'}, | |
{'4','5','6', 'B'}, | |
{'7','8','9', 'C'}, | |
{'*','0','#', 'D'} | |
}; | |
byte rowPins[ROWS] = {5, 6, 7, 8} | |
byte colPins[COLS] = {A3, A1, A2, A0}; | |
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); | |
String s=String(); | |
String password = "1234"; | |
void unlock() { | |
digitalWrite(3, 1); | |
delay(100); | |
digitalWrite(3, 0); | |
delay(100); | |
digitalWrite(3, 1); | |
delay(100); | |
digitalWrite(3, 0); | |
digitalWrite(4,0); | |
delay(3000); | |
digitalWrite(4,1); | |
} | |
void badcard() { | |
tone(3, 200, 600); | |
} | |
void loop() { | |
char key = keypad.getKey(); | |
if (key != 0) { | |
digitalWrite(3,1); | |
delay(10); | |
digitalWrite(3,0); | |
if (key == '#') { | |
if (s.equals(password)) { | |
unlock(); | |
s = String(); | |
} else { | |
badcard(); | |
s = String(); | |
} | |
} else { | |
s.concat(key); | |
} | |
} | |
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. | |
if ( ! mfrc522.PICC_IsNewCardPresent()) { | |
return; | |
} | |
// Select one of the cards | |
if ( ! mfrc522.PICC_ReadCardSerial()) { | |
return; | |
} | |
mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); | |
if ( mfrc522.uid.uidByte[0] == correct_card[0] && mfrc522.uid.uidByte[1] == correct_card[1] && | |
mfrc522.uid.uidByte[2] == correct_card[2] && mfrc522.uid.uidByte[3] == correct_card[3] ) { | |
unlock(); | |
mfrc522.uid.uidByte[0] = mfrc522.uid.uidByte[1] = mfrc522.uid.uidByte[2] = mfrc522.uid.uidByte[3] = 0; | |
} else { | |
badcard(); | |
mfrc522.uid.uidByte[0] = mfrc522.uid.uidByte[1] = mfrc522.uid.uidByte[2] = mfrc522.uid.uidByte[3] = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment