A LED-strip (WS2812B, 46 pieces, PIN 8) and an Adafruit PN532 RFID shield (IRQ 2) are attached to my Arduino Mega 2560.
The GRB based strip seems to be working fine with FastLED in the beginning, I can confirm this by cycling through all three base colours in setup()
.
Once I call nfc.readPassiveTargetID(...)
and use an NFC tag on it, things start to get weird. Whenever I comment out the line, it works properly again.
The colors RGB are randomly swapped (as if I had passed a different color ordering), brightnesses may change randomly as well, and sometimes a few LEDs in the beginning or end of the strip have different brightness/color as the rest.
As I have put the brightness down to 32, I'm sure it's not a power problem.
My assumption is that there is either some memory corruption, or some PIN interference with the two libraries, although I don't know why it would do it. Please find my code below, any help is appreciated.
#include <FastLED.h>
#include <Adafruit_PN532.h>
#define LED_COUNT 46
#define LED_DATA_PIN 8
CRGB leds[LED_COUNT];
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
void setLeds(CRGB color) {
Serial.print("setLeds("); Serial.print(color.r, DEC ); Serial.print(","); Serial.print(color.g, DEC ); Serial.print(","); Serial.print(color.b, DEC ); Serial.println(")");
for (int i = 0; i < LED_COUNT; i++) {
leds[i] = color;
}
FastLED.show();
}
void ledStartSequence() {
setLeds(CRGB::Black);
for (int crtLed = 0; crtLed < 23; crtLed++) {
leds[crtLed] = CRGB::Blue;
leds[crtLed + 23] = CRGB::Blue;
FastLED.show();
delay(20);
}
Serial.println("Red");
setLeds(CRGB::Red);
delay(1000);
Serial.println("Green");
setLeds(CRGB::Green);
delay(1000);
Serial.println("Blue");
setLeds(CRGB::Blue);
delay(1000);
Serial.println("Off");
setLeds(CRGB::Black);
}
void initLeds() {
FastLED.addLeds<WS2812B, LED_DATA_PIN, GRB>(leds, LED_COUNT);
FastLED.setBrightness(32);
}
void initSerial() {
Serial.begin(9600);
Serial.println("Hello!");
}
void initNFC() {
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
Serial.print(versiondata);
if (! versiondata) {
Serial.print("Error: Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
nfc.SAMConfig();
}
void setup() {
initSerial();
initLeds();
initNFC();
ledStartSequence();
}
void loop(void) {
Serial.println("Waiting for an ISO14443A Card ...");
setLeds(CRGB::Red); // only displayed correctly in the first iteration
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
/*
* Here's where things start to go nuts!
*/
delay(1000);
setLeds(CRGB::Green); // usually not displayed correctly
delay(1000);
setLeds(CRGB::Blue); // usually not displayed correctly
delay(1000);
}
uid
with 7 bytes. If afternfc.readPassiveTargetID
the uidLength value is > 7 you'll have a buffer overflow. Do you know how many bytes the UID of the RFID card containes? Another possibillity is that the PN532 driver uses a timer (e.g. for Hardware I2C) that the LED lib also uses. Then it possibly might help if you reinitialize the LEDsinitLeds()
after die call toreadPassiveTargetID
. Both are just guesses.