Particle NFC and RGB led help

For some reason this is not compiling but i think my ide is tripping. Can anyone please see if this code compiles? my goal: i am trying to change the RGB led light whenever the NFC is read… Also please let me know if there are any mistakes and the best way to tackle them!

Thanks Again to Scruff before answering :smiley:

#include "Particle.h"
#include "Grove_ChainableLED.h"

SerialLogHandler logHandler(LOG_LEVEL_TRACE);

SYSTEM_MODE(MANUAL);
// This #include statement was automatically added by the Particle IDE.
ChainableLED leds(RX, TX, 1);

static void nfcCallback(nfc_event_type_t type, nfc_event_t* event, void* context);

int counter = 0;
volatile bool updateCounter = true;

void setup(){
    leds.init();
    NFC.on();
    NFC.setText("OFF" + updateNFC) + "en");
    NFC.update();
    }

void loop(){
    if (updateCounter) {
        updateCounter = false;

        char buf[64];
        snprintf(buf, sizeof(buf), "testing counter=%d", ++counter);

        NFC.setText(buf, "en");
        NFC.on(nfcCallback);

        Log.info("next read should show: %s", buf);
    }
}

static void nfcCallback(nfc_event_type_t type, nfc_event_t* event, void* context) {
    switch (type) {
    case NFC_EVENT_FIELD_ON: {
        leds.setColorRGB(0, 255, 0, 0);
        break;
    }
    case NFC_EVENT_FIELD_OFF: {
        leds.setColorRGB(0, 0, 0, 255);
        break;
    }
    case NFC_EVENT_READ: {
        updateCounter = true;
        break;
    }
    default:
        break;
    }
}
1 Like

Any error messages to be seen?

But this stray paretheses seems wrong :wink:

Where does updateNFC come from?

1 Like

@ScruffR is lightning fast!!!

3 Likes

This is the only error code i am getting. I forgot to change updateNFC to updateCounter to move along with particle nfc reference
08%20PM

It is weird that there’s no error reporting here, but your code compiles when the contents of setup() are cleaned up per the request above.

now i am getting this when trying multiple times to compile. Is there any way to solve this?

can you PM me your particle user ID?

hidden

Hi - I can compile CODEHELP.ino without issue? I recommend commenting out lines to try and isolate the issue if you continue to run into the issue.

So after trying commenting and uncommenting while compiling this is where the code stops working:

    // NFC.setText("OFF" + updateCounter)+ "en");

and when commenting this lines the compiling works. Does anyone have an idea how to solve this or if my code has errors?

#include "Particle.h"
#include "Grove_ChainableLED.h"

SerialLogHandler logHandler(LOG_LEVEL_TRACE);

SYSTEM_MODE(MANUAL);
ChainableLED leds(RX, TX, 1);

static void nfcCallback(nfc_event_type_t type, nfc_event_t* event, void* context);

int counter = 0;
volatile bool updateCounter = true;

void setup() {
    leds.init();
    NFC.on();
//     NFC.setText("OFF" + updateCounter) + "en");
//     NFC.update();
    }

void loop(){
    // if (updateCounter) {
    //     updateCounter = false;

    //     char buf[64];
    //     snprintf(buf, sizeof(buf), "testing counter=%d", ++counter);

    //     NFC.setText(buf, "en");
    //     NFC.on(nfcCallback);

    //     Log.info("next read should show: %s", buf);
    // }
}

static void nfcCallback(nfc_event_type_t type, nfc_event_t* event, void* context) {
    switch (type) {
    case NFC_EVENT_FIELD_ON: {
        leds.setColorRGB(0, 255, 0, 0);
        break;
    }
    case NFC_EVENT_FIELD_OFF: {
        leds.setColorRGB(0, 0, 0, 255);
        break;
    }
    case NFC_EVENT_READ: {
        updateCounter = true;
        break;
    }
    default:
        break;
    }
}

@talalgedeon, did you not notice @ScruffR’s comments above:

But this stray paretheses seems wrong :wink:

NFC.setText(“OFF” + updateNFC) + “en”);

2 Likes

so i edited the updateNFC to updateCounter and added a parenthesis.

NFC.setText((“OFF” + updateCounter) + “en”);

Also, would this effect my void loop{} because when i try to uncomment it, it gives me the same error

@ScruffR @peekay123 @marekparticle Thanks for all the answers! Sorry if im asking too much i am still advancing my skills :stuck_out_tongue:

Have you checked that you are actually targeting a device OS version that supports NFC?
You may also try creating an entirely new project and just copy/paste the code in there.
Sometimes a project gets into some funky state which can cause all kinds of symptoms. Ditching that and starting over often helps.

BTW, it has proven more robust to use standard C strings (char array) instead of String objects.
I’d rewrite your function call this way

  char txt[16];
  snprintf(txt, sizeof(txt), "OFF%den", updateCounter);
  NFC.setText(txt);

However, in your loop() you are using "en" for the encoding parameter, but in setup() as part of text - is this intentional? Especially since the encoding doesn’t seem to be optional.
Hence the above may rather look like this

  char txt[16];
  snprintf(txt, sizeof(txt), "OFF%d", updateCounter);
  NFC.setText(txt, "en");
3 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.