Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <MFRC522.h>
- //––––– PINY –––––
- #define SS_PIN 10
- #define RST_PIN 9
- #define GREEN_LED_PIN 2
- #define RED_LED_PIN 3
- #define BUZZER_PIN A5
- MFRC522 rfid(SS_PIN, RST_PIN);
- //––––– NUMERY UID TAGÓW –––––
- // Wpisz tutaj swoje rzeczywiste UID:
- byte tag1[] = { 0xDE, 0xAD, 0xBE, 0xEF }; // poprawny tag → melodia
- byte tag2[] = { 0x12, 0x34, 0x56, 0x78 }; // niepoprawny tag → ton błędu
- //––––– MELODIA “Twinkle Twinkle” –––––
- int melody[] = {
- 262, 262, 392, 392, 440, 440, 392, // C C G G A A G
- 349, 349, 330, 330, 294, 294, 262 // F F E E D D C
- };
- int noteDurations[] = {
- 4,4,4,4,4,4,2,
- 4,4,4,4,4,4,2
- };
- //––––– FUNKCJA POMOCNICZA: porównuje odczytane UID z tablicą tag[]
- bool compareTag(byte *tag) {
- if (rfid.uid.size != 4) return false;
- for (byte i = 0; i < 4; i++) {
- if (rfid.uid.uidByte[i] != tag[i]) return false;
- }
- return true;
- }
- //––––– FUNKCJA ODTWARZAJĄCA MELODIĘ –––––
- void playMelody() {
- for (int i = 0; i < sizeof(melody)/sizeof(int); i++) {
- int note = melody[i];
- int dur = 1000 / noteDurations[i]; // długość nuty w ms
- tone(BUZZER_PIN, note, dur * 0.9); // odtwarzaj 90% czasu
- delay(dur); // czekaj całą wartość
- }
- noTone(BUZZER_PIN);
- }
- void setup() {
- Serial.begin(9600);
- SPI.begin();
- rfid.PCD_Init();
- pinMode(GREEN_LED_PIN, OUTPUT);
- pinMode(RED_LED_PIN, OUTPUT);
- pinMode(BUZZER_PIN, OUTPUT);
- Serial.println("Przyłóż kartę/tag…");
- }
- void loop() {
- // jeżeli nowa karta i udało się odczytać UID
- if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
- // wypisz UID na Serial
- Serial.print("UID: ");
- for (byte i = 0; i < rfid.uid.size; i++) {
- Serial.print(rfid.uid.uidByte[i], HEX);
- Serial.print(" ");
- }
- Serial.println();
- if (compareTag(tag1)) {
- // POPRAWNY TAG → melodia + zielona dioda
- digitalWrite(GREEN_LED_PIN, HIGH);
- playMelody();
- digitalWrite(GREEN_LED_PIN, LOW);
- }
- else if (compareTag(tag2)) {
- // DRUGI TAG → błąd: czerwony LED + krótki ton
- digitalWrite(RED_LED_PIN, HIGH);
- tone(BUZZER_PIN, 200, 500);
- delay(500);
- digitalWrite(RED_LED_PIN, LOW);
- noTone(BUZZER_PIN);
- }
- // inaczej: nieznany tag → nie robi nic
- // zakończ komunikację z kartą
- rfid.PICC_HaltA();
- rfid.PCD_StopCrypto1();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment