Advertisement
Guest User

arduino LED + RFID

a guest
Jun 24th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <Wire.h>
  3. #include <PN532_I2C.h>
  4. #include <PN532.h>
  5. #include <NfcAdapter.h>
  6.  
  7. #define LED_RING_PIN D2
  8. #define NUMPIXELS 16
  9.  
  10. Adafruit_NeoPixel pixels(NUMPIXELS, LED_RING_PIN, NEO_GRB + NEO_KHZ800);
  11. PN532_I2C pn532i2c(Wire);
  12. PN532 nfc(pn532i2c);
  13.  
  14. uint32_t greenColor = pixels.Color(0, 255, 0);
  15. uint32_t color1 = pixels.Color(255, 69, 0);
  16. uint32_t color2 = pixels.Color(75, 0, 130);
  17. uint32_t color3 = pixels.Color(0, 255, 255);
  18.  
  19. const int MIN_BRIGHTNESS = 5;
  20. const float MAX_BRIGHTNESS_SCALE = 0.8;
  21. const int pixelMapping[NUMPIXELS] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8};
  22.  
  23. int flashCount = 3;
  24. int ledPin = 13;
  25. unsigned long lastAnimationTime = 0;
  26. const int animationInterval = 20;
  27.  
  28. bool cardDetected = false;
  29. unsigned long cardCheckTime = 0;
  30. unsigned long cardDetectedTime = 0;
  31. const int blinkDuration = 500;
  32. int blinkCount = 0;
  33. bool blinkState = false;
  34.  
  35. void setup() {
  36. Serial.begin(115200);
  37. pixels.begin();
  38.  
  39. nfc.begin();
  40. if (!nfc.getFirmwareVersion()) {
  41. Serial.println("Didn't find PN53x board");
  42. while (1);
  43. }
  44.  
  45. Serial.println("Waiting for an ISO14443A card");
  46. nfc.SAMConfig();
  47.  
  48. pinMode(ledPin, OUTPUT);
  49. digitalWrite(ledPin, LOW);
  50. }
  51.  
  52. void loop() {
  53. unsigned long currentTime = millis();
  54.  
  55. if (!cardDetected && currentTime - cardCheckTime > (800 - random(100, 400))) {
  56. // Serial.println("Checking .......");
  57. checkRFID();
  58. cardCheckTime = currentTime;
  59. }
  60.  
  61. if (cardDetected) {
  62. // Serial.println("handle");
  63. handleCardDetected(currentTime);
  64. }
  65.  
  66. // Serial.println("done...");
  67.  
  68. if (!cardDetected && currentTime - lastAnimationTime >= animationInterval) {
  69. // Serial.print("x");
  70. runGradientAnimation();
  71. lastAnimationTime = currentTime;
  72. }
  73. }
  74.  
  75. void checkRFID() {
  76. uint8_t uid[7];
  77. uint8_t uidLength;
  78.  
  79. if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1)) {
  80. Serial.println("Found a card!");
  81. Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
  82. Serial.print("UID Value: ");
  83. for (uint8_t i = 0; i < uidLength; i++) {
  84. Serial.print(" 0x"); Serial.print(uid[i], HEX);
  85. }
  86. Serial.println("");
  87.  
  88. cardDetected = true;
  89. cardDetectedTime = millis();
  90. blinkCount = 0;
  91. blinkState = true;
  92. setRingColor(greenColor);
  93. }
  94. }
  95.  
  96. void handleCardDetected(unsigned long currentTime) {
  97. if (currentTime - cardDetectedTime >= blinkDuration) {
  98. cardDetectedTime = currentTime;
  99. blinkState = !blinkState;
  100. if (blinkState) {
  101. setRingColor(greenColor);
  102. } else {
  103. setRingColor(0);
  104. blinkCount++;
  105. }
  106.  
  107. if (blinkCount >= flashCount) {
  108. Serial.println("reset");
  109. cardDetected = false;
  110. delay(500);
  111. }
  112. }
  113. }
  114.  
  115. void setRingColor(uint32_t color) {
  116. for (int i = 0; i < NUMPIXELS; i++) {
  117. pixels.setPixelColor(i, color);
  118. }
  119. pixels.show();
  120. }
  121.  
  122. void runGradientAnimation() {
  123. const int cycleTime = 3000;
  124. unsigned long currentTime = millis();
  125. float wavePosition = (currentTime % cycleTime) / (float)cycleTime;
  126.  
  127. for (int i = 0; i < NUMPIXELS; i++) {
  128. float pixelFraction = (float)pixelMapping[i] / (NUMPIXELS - 1);
  129. uint32_t color;
  130.  
  131. if (pixelFraction < 0.5) {
  132. color = interpolateColor(color1, color2, pixelFraction * 2);
  133. } else {
  134. color = interpolateColor(color2, color3, (pixelFraction - 0.5) * 2);
  135. }
  136.  
  137. float angle = 2.0 * PI * (wavePosition + (float)i / NUMPIXELS);
  138. float brightnessFraction = (sin(angle) + 1) / 2;
  139. brightnessFraction = pow(brightnessFraction, 2.5);
  140. float brightness = brightnessFraction * MAX_BRIGHTNESS_SCALE;
  141. brightness = MIN_BRIGHTNESS / 255.0 + brightness * (1.0 - MIN_BRIGHTNESS / 255.0);
  142.  
  143. uint32_t adjustedColor = adjustBrightness(color, brightness);
  144. pixels.setPixelColor(i, adjustedColor);
  145. }
  146. pixels.show();
  147. }
  148.  
  149. uint32_t interpolateColor(uint32_t color1, uint32_t color2, float fraction) {
  150. uint8_t r = ((color1 >> 16) & 0xFF) + fraction * (((color2 >> 16) & 0xFF) - ((color1 >> 16) & 0xFF));
  151. uint8_t g = ((color1 >> 8) & 0xFF) + fraction * (((color2 >> 8) & 0xFF) - ((color1 >> 8) & 0xFF));
  152. uint8_t b = (color1 & 0xFF) + fraction * ((color2 & 0xFF) - (color1 & 0xFF));
  153. return pixels.Color(r, g, b);
  154. }
  155.  
  156. uint32_t adjustBrightness(uint32_t color, float brightnessFactor) {
  157. uint8_t r = ((color >> 16) & 0xFF) * brightnessFactor;
  158. uint8_t g = ((color >> 8) & 0xFF) * brightnessFactor;
  159. uint8_t b = (color & 0xFF) * brightnessFactor;
  160. return pixels.Color(r, g, b);
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement