pierdziadek

lab8+

Apr 28th, 2025
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3.  
  4. //––––– PINY –––––
  5. #define SS_PIN 10
  6. #define RST_PIN 9
  7. #define GREEN_LED_PIN 2
  8. #define RED_LED_PIN 3
  9. #define BUZZER_PIN A5
  10.  
  11. MFRC522 rfid(SS_PIN, RST_PIN);
  12.  
  13. //––––– NUMERY UID TAGÓW –––––
  14. // Wpisz tutaj swoje rzeczywiste UID:
  15. byte tag1[] = { 0xDE, 0xAD, 0xBE, 0xEF }; // poprawny tag → melodia
  16. byte tag2[] = { 0x12, 0x34, 0x56, 0x78 }; // niepoprawny tag → ton błędu
  17.  
  18. //––––– MELODIA “Twinkle Twinkle” –––––
  19. int melody[] = {
  20. 262, 262, 392, 392, 440, 440, 392, // C C G G A A G
  21. 349, 349, 330, 330, 294, 294, 262 // F F E E D D C
  22. };
  23. int noteDurations[] = {
  24. 4,4,4,4,4,4,2,
  25. 4,4,4,4,4,4,2
  26. };
  27.  
  28. //––––– FUNKCJA POMOCNICZA: porównuje odczytane UID z tablicą tag[]
  29. bool compareTag(byte *tag) {
  30. if (rfid.uid.size != 4) return false;
  31. for (byte i = 0; i < 4; i++) {
  32. if (rfid.uid.uidByte[i] != tag[i]) return false;
  33. }
  34. return true;
  35. }
  36.  
  37. //––––– FUNKCJA ODTWARZAJĄCA MELODIĘ –––––
  38. void playMelody() {
  39. for (int i = 0; i < sizeof(melody)/sizeof(int); i++) {
  40. int note = melody[i];
  41. int dur = 1000 / noteDurations[i]; // długość nuty w ms
  42. tone(BUZZER_PIN, note, dur * 0.9); // odtwarzaj 90% czasu
  43. delay(dur); // czekaj całą wartość
  44. }
  45. noTone(BUZZER_PIN);
  46. }
  47.  
  48. void setup() {
  49. Serial.begin(9600);
  50. SPI.begin();
  51. rfid.PCD_Init();
  52.  
  53. pinMode(GREEN_LED_PIN, OUTPUT);
  54. pinMode(RED_LED_PIN, OUTPUT);
  55. pinMode(BUZZER_PIN, OUTPUT);
  56.  
  57. Serial.println("Przyłóż kartę/tag…");
  58. }
  59.  
  60. void loop() {
  61. // jeżeli nowa karta i udało się odczytać UID
  62. if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
  63. // wypisz UID na Serial
  64. Serial.print("UID: ");
  65. for (byte i = 0; i < rfid.uid.size; i++) {
  66. Serial.print(rfid.uid.uidByte[i], HEX);
  67. Serial.print(" ");
  68. }
  69. Serial.println();
  70.  
  71. if (compareTag(tag1)) {
  72. // POPRAWNY TAG → melodia + zielona dioda
  73. digitalWrite(GREEN_LED_PIN, HIGH);
  74. playMelody();
  75. digitalWrite(GREEN_LED_PIN, LOW);
  76. }
  77. else if (compareTag(tag2)) {
  78. // DRUGI TAG → błąd: czerwony LED + krótki ton
  79. digitalWrite(RED_LED_PIN, HIGH);
  80. tone(BUZZER_PIN, 200, 500);
  81. delay(500);
  82. digitalWrite(RED_LED_PIN, LOW);
  83. noTone(BUZZER_PIN);
  84. }
  85. // inaczej: nieznany tag → nie robi nic
  86.  
  87. // zakończ komunikację z kartą
  88. rfid.PICC_HaltA();
  89. rfid.PCD_StopCrypto1();
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment