Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #include <Wire.h>
- #include <PN532_I2C.h>
- #include <PN532.h>
- #include <NfcAdapter.h>
- #define LED_RING_PIN D2
- #define NUMPIXELS 16
- Adafruit_NeoPixel pixels(NUMPIXELS, LED_RING_PIN, NEO_GRB + NEO_KHZ800);
- PN532_I2C pn532i2c(Wire);
- PN532 nfc(pn532i2c);
- uint32_t greenColor = pixels.Color(0, 255, 0);
- uint32_t color1 = pixels.Color(255, 69, 0);
- uint32_t color2 = pixels.Color(75, 0, 130);
- uint32_t color3 = pixels.Color(0, 255, 255);
- const int MIN_BRIGHTNESS = 5;
- const float MAX_BRIGHTNESS_SCALE = 0.8;
- const int pixelMapping[NUMPIXELS] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8};
- int flashCount = 3;
- int ledPin = 13;
- unsigned long lastAnimationTime = 0;
- const int animationInterval = 20;
- bool cardDetected = false;
- unsigned long cardCheckTime = 0;
- unsigned long cardDetectedTime = 0;
- const int blinkDuration = 500;
- int blinkCount = 0;
- bool blinkState = false;
- void setup() {
- Serial.begin(115200);
- pixels.begin();
- nfc.begin();
- if (!nfc.getFirmwareVersion()) {
- Serial.println("Didn't find PN53x board");
- while (1);
- }
- Serial.println("Waiting for an ISO14443A card");
- nfc.SAMConfig();
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, LOW);
- }
- void loop() {
- unsigned long currentTime = millis();
- if (!cardDetected && currentTime - cardCheckTime > (800 - random(100, 400))) {
- // Serial.println("Checking .......");
- checkRFID();
- cardCheckTime = currentTime;
- }
- if (cardDetected) {
- // Serial.println("handle");
- handleCardDetected(currentTime);
- }
- // Serial.println("done...");
- if (!cardDetected && currentTime - lastAnimationTime >= animationInterval) {
- // Serial.print("x");
- runGradientAnimation();
- lastAnimationTime = currentTime;
- }
- }
- void checkRFID() {
- uint8_t uid[7];
- uint8_t uidLength;
- if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1)) {
- Serial.println("Found a card!");
- Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
- Serial.print("UID Value: ");
- for (uint8_t i = 0; i < uidLength; i++) {
- Serial.print(" 0x"); Serial.print(uid[i], HEX);
- }
- Serial.println("");
- cardDetected = true;
- cardDetectedTime = millis();
- blinkCount = 0;
- blinkState = true;
- setRingColor(greenColor);
- }
- }
- void handleCardDetected(unsigned long currentTime) {
- if (currentTime - cardDetectedTime >= blinkDuration) {
- cardDetectedTime = currentTime;
- blinkState = !blinkState;
- if (blinkState) {
- setRingColor(greenColor);
- } else {
- setRingColor(0);
- blinkCount++;
- }
- if (blinkCount >= flashCount) {
- Serial.println("reset");
- cardDetected = false;
- delay(500);
- }
- }
- }
- void setRingColor(uint32_t color) {
- for (int i = 0; i < NUMPIXELS; i++) {
- pixels.setPixelColor(i, color);
- }
- pixels.show();
- }
- void runGradientAnimation() {
- const int cycleTime = 3000;
- unsigned long currentTime = millis();
- float wavePosition = (currentTime % cycleTime) / (float)cycleTime;
- for (int i = 0; i < NUMPIXELS; i++) {
- float pixelFraction = (float)pixelMapping[i] / (NUMPIXELS - 1);
- uint32_t color;
- if (pixelFraction < 0.5) {
- color = interpolateColor(color1, color2, pixelFraction * 2);
- } else {
- color = interpolateColor(color2, color3, (pixelFraction - 0.5) * 2);
- }
- float angle = 2.0 * PI * (wavePosition + (float)i / NUMPIXELS);
- float brightnessFraction = (sin(angle) + 1) / 2;
- brightnessFraction = pow(brightnessFraction, 2.5);
- float brightness = brightnessFraction * MAX_BRIGHTNESS_SCALE;
- brightness = MIN_BRIGHTNESS / 255.0 + brightness * (1.0 - MIN_BRIGHTNESS / 255.0);
- uint32_t adjustedColor = adjustBrightness(color, brightness);
- pixels.setPixelColor(i, adjustedColor);
- }
- pixels.show();
- }
- uint32_t interpolateColor(uint32_t color1, uint32_t color2, float fraction) {
- uint8_t r = ((color1 >> 16) & 0xFF) + fraction * (((color2 >> 16) & 0xFF) - ((color1 >> 16) & 0xFF));
- uint8_t g = ((color1 >> 8) & 0xFF) + fraction * (((color2 >> 8) & 0xFF) - ((color1 >> 8) & 0xFF));
- uint8_t b = (color1 & 0xFF) + fraction * ((color2 & 0xFF) - (color1 & 0xFF));
- return pixels.Color(r, g, b);
- }
- uint32_t adjustBrightness(uint32_t color, float brightnessFactor) {
- uint8_t r = ((color >> 16) & 0xFF) * brightnessFactor;
- uint8_t g = ((color >> 8) & 0xFF) * brightnessFactor;
- uint8_t b = (color & 0xFF) * brightnessFactor;
- return pixels.Color(r, g, b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement