problems starting program using NFC

hello,

i have a small problem in my code and i can’t seem to find the problem in it.
for a school project i want to use a specific tag to start a specific program on my pc using NFC.

i made this setup with a push button before (if i press button, it starts program) and that worked. (thank you forum, as i had problems with that too). but now i swapped from button to NFC.

to start the programs i use “Gobetwino”. (WoW and FireFox are the progs i want to run atm.)
i have to code up and working, it starts WoW (and only one time, as i want to intergrate the chip in a figure that stays on the reader (ex, amiibo). at first it started multiple instances but now only one, so thats fixed.

but,

If i switch tags (place another tag on the reader) it sees it and reads it out (with the UID i need),
but it wont notice that there is a command. if i reboot gobetwino, it will read the code of the 2nd tag, but wont run the command (swapping the gobetwino commands (RUNWOW and STARTFOX), it will start firefox without a problem, so its not gobetwino’s fault either.

breadboard is MFRC522 reader connected to arduino UNO, without anything extra added.

can anyone see if i made a dumb mistake or not?

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
long UID_LONG;
int ProgON;
long LastUID_LONG;
 
void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    SPI.begin();            // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522 card
    Serial.println("Scan PICC to see UID and type...");
   
}

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
      return;
  }

  // Dump debug info about the card. PICC_HaltA() is automatically called.
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

  Serial.print("UID size : ");
  Serial.println(mfrc522.uid.size);

  Serial.print("Printing HEX UID : ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  } 
  Serial.println("");

  unsigned long UID_unsigned;
  UID_unsigned =  mfrc522.uid.uidByte[0] << 24;
  UID_unsigned += mfrc522.uid.uidByte[1] << 16;
  UID_unsigned += mfrc522.uid.uidByte[2] <<  8;
  UID_unsigned += mfrc522.uid.uidByte[3];

  Serial.println();
  Serial.println("UID Unsigned int"); 
  Serial.println(UID_unsigned);

  String  UID_string =  (String)UID_unsigned;
  UID_LONG=(long)UID_unsigned;

  Serial.println("UID Long :");
  Serial.println(UID_LONG);

  Serial.println("UID String :");
  Serial.println(UID_string);

  if (ProgON == LOW) {
    // do nothing if there is no tag
    if (UID_LONG == NULL) {
      //do nothing, let loop restart
    } else {
      //run new prog
      if (UID_LONG == 1773) {
        LastUID_LONG = UID_LONG; //sets LastUID
        Serial.println("#S|RUNWOW|[]#");
        ProgON = HIGH;
        delay(50);
      } else if (UID_LONG == 7424) {
        ProgON = LOW;
        LastUID_LONG = UID_LONG; //sets lastUID
        Serial.println("#S|STARTFOX[]#");
        ProgON = HIGH;
        delay(50);
      }
    }
  } else {
    if (UID_LONG == NULL){
      ProgON = LOW; // kunt ge gebruiken samen met LastUID_LONG om laaste programma te killen
      //kill previous prog use prog with LastUID_LONG
    }
  }
}

thank you in advance.

(String)UID_unsigned;

Casting an unsigned int to a String will NOT make a String out of an unsigned int. DO NOT DO THIS.

GoBetwino shows a log of data received. What is in that log?

i hope i pasted the correct things. after reading card, this is the status message in gobetwino when lauching WoW (wich it does)

Commandstring recieved : 
15/06/2016 15:02:58,While parsing the command string  an exception was generated
15/06/2016 15:02:58,Index was outside the bounds of the array.
15/06/2016 15:02:58,Commandstring recieved : UID size : 4
15/06/2016 15:02:58,The string recieved : UID size : 4 is not a well formed command string
15/06/2016 15:02:58,Commandstring recieved : Printing HEX UID :  03 C2 06 ED
15/06/2016 15:02:58,The string recieved : Printing HEX UID :  03 C2 06 ED is not a well formed command string
15/06/2016 15:02:58,Commandstring recieved : 
15/06/2016 15:02:58,While parsing the command string  an exception was generated
15/06/2016 15:02:58,Index was outside the bounds of the array.
15/06/2016 15:02:58,Commandstring recieved : UID Unsigned int
15/06/2016 15:02:59,The string recieved : UID Unsigned int is not a well formed command string
15/06/2016 15:02:59,Commandstring recieved : 1773
15/06/2016 15:02:59,The string recieved : 1773 is not a well formed command string
15/06/2016 15:02:59,Commandstring recieved : UID Long :
15/06/2016 15:02:59,The string recieved : UID Long : is not a well formed command string
15/06/2016 15:02:59,Commandstring recieved : 1773
15/06/2016 15:02:59,The string recieved : 1773 is not a well formed command string
15/06/2016 15:02:59,Commandstring recieved : UID String :
15/06/2016 15:02:59,The string recieved : UID String : is not a well formed command string
15/06/2016 15:02:59,Commandstring recieved : 1773
15/06/2016 15:02:59,The string recieved : 1773 is not a well formed command string
15/06/2016 15:02:59,Commandstring recieved : #S|RUNWOW|[]#
15/06/2016 15:02:59,Command parsed OK
15/06/2016 15:02:59,Executing command : RUNWOW
15/06/2016 15:02:59,Process : D:\Program Files (x86)\Blizzard\Battle.net\World of Warcraft\Wow-64.exe started. Proces ID = 0
15/06/2016 15:03:09,Process: 0 ended and was removed from the runningProcessList

as soon as i replace with the other tag to lauch Firefox:

Commandstring recieved : UID size : 4
15/06/2016 15:03:23,The string recieved : UID size : 4 is not a well formed command string
15/06/2016 15:03:23,Commandstring recieved : Printing HEX UID :  1D A4 1D 00
15/06/2016 15:03:23,The string recieved : Printing HEX UID :  1D A4 1D 00 is not a well formed command string
15/06/2016 15:03:23,Commandstring recieved : 
15/06/2016 15:03:23,While parsing the command string  an exception was generated
15/06/2016 15:03:23,Index was outside the bounds of the array.
15/06/2016 15:03:23,Commandstring recieved : UID Unsigned int
15/06/2016 15:03:23,The string recieved : UID Unsigned int is not a well formed command string
15/06/2016 15:03:23,Commandstring recieved : 7424
15/06/2016 15:03:23,The string recieved : 7424 is not a well formed command string
15/06/2016 15:03:23,Commandstring recieved : UID Long :
15/06/2016 15:03:23,The string recieved : UID Long : is not a well formed command string
15/06/2016 15:03:23,Commandstring recieved : 7424
15/06/2016 15:03:23,The string recieved : 7424 is not a well formed command string
15/06/2016 15:03:23,Commandstring recieved : UID String :
15/06/2016 15:03:23,The string recieved : UID String : is not a well formed command string
15/06/2016 15:03:23,Commandstring recieved : 7424
15/06/2016 15:03:23,The string recieved : 7424 is not a well formed command string

gobetwino, when using the 2nd tag without using the 1st tag:

15/06/2016 14:50:46,Commandstring recieved : 
15/06/2016 14:50:46,While parsing the command string  an exception was generated
15/06/2016 14:50:46,Index was outside the bounds of the array.
15/06/2016 14:50:46,Commandstring recieved : UID size : 4
15/06/2016 14:50:46,The string recieved : UID size : 4 is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : Printing HEX UID :  1D A4 1D 00
15/06/2016 14:50:46,The string recieved : Printing HEX UID :  1D A4 1D 00 is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : 
15/06/2016 14:50:46,While parsing the command string  an exception was generated
15/06/2016 14:50:46,Index was outside the bounds of the array.
15/06/2016 14:50:46,Commandstring recieved : UID Unsigned int
15/06/2016 14:50:46,The string recieved : UID Unsigned int is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : 7424
15/06/2016 14:50:46,The string recieved : 7424 is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : UID Long :
15/06/2016 14:50:46,The string recieved : UID Long : is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : 7424
15/06/2016 14:50:46,The string recieved : 7424 is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : UID String :
15/06/2016 14:50:46,The string recieved : UID String : is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : 7424
15/06/2016 14:50:46,The string recieved : 7424 is not a well formed command string
15/06/2016 14:50:46,Commandstring recieved : #S|STARTFOX[]#
15/06/2016 14:50:46,The string recieved : #S|STARTFOX[]# is not a well formed command string

Now that you know some stuff is being handled correctly, remove the writes to the serial port that are upsetting GoBetwino, and try again.

i have removed all cables, reconnected them all, still the same problem, otherwise i have no clue wich serial port/cable is annoying gobetwino.

i do have to tell i suck at coding and i started with arduino’s and coding these like 3-4 weeks ago. most of the code is searched and edited or with some help. but i still can’t find anything what i should change to make it work. switching the uno with a mega doesn’t change a thing.

i have removed all cables, reconnected them all, still the same problem, otherwise i have no clue wich serial port/cable is annoying gobetwino.

Where does GoBetwino get the data that it is showing? From the Arduino!

So, when GoBetwino says:

15/06/2016 15:02:58,Commandstring recieved : UID size : 4 15/06/2016 15:02:58,The string recieved : UID size : 4 is not a well formed command string

doesn't it seem reasonable that

  Serial.print("UID size : ");
  Serial.println(mfrc522.uid.size);

are sending data to GoBetwino that it is not prepared to deal with?

So, why ARE you sending that data?

Now, it would seem reasonable that you are trying to debug your code, sending the data sometimes to the Serial Monitor app and sometimes to GoBetwino. If that is the case, you need a way to define that the sketch should be compiled one way when sending data to the serial monitor app, and another way when sending data to GoBetwino.

But, since that is an advanced technique, and you are not quite ready for that, simply comment out the Serial.print() statements that are NOT for GoBetwino.

See if that fixes the issue.

i commented everything exept

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
long UID_LONG;
int ProgON;
long LastUID_LONG;
 
void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    SPI.begin();            // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522 card
    //Serial.println("Scan PICC to see UID and type...");
   
}

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
      return;
  }

  // Dump debug info about the card. PICC_HaltA() is automatically called.
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

 // Serial.print("UID size : ");
  //Serial.println(mfrc522.uid.size);

  //Serial.print("Printing HEX UID : ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
   // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    //Serial.print(mfrc522.uid.uidByte[i], HEX);
  } 
  //Serial.println("");

  unsigned long UID_unsigned;
  UID_unsigned =  mfrc522.uid.uidByte[0] << 24;
  UID_unsigned += mfrc522.uid.uidByte[1] << 16;
  UID_unsigned += mfrc522.uid.uidByte[2] <<  8;
  UID_unsigned += mfrc522.uid.uidByte[3];

  //Serial.println();
  //Serial.println("UID Unsigned int"); 
  //Serial.println(UID_unsigned);

  String  UID_string =  (String)UID_unsigned;
  UID_LONG=(long)UID_unsigned;

  //Serial.println("UID Long :");
  //Serial.println(UID_LONG);

  //Serial.println("UID String :");
  //Serial.println(UID_string);

  if (ProgON == LOW) {
    // do nothing if there is no tag
    if (UID_LONG == NULL) {
      //do nothing, let loop restart
    } else {
      //run new prog
      if (UID_LONG == 1773) {
        LastUID_LONG = UID_LONG; //sets LastUID
        Serial.println("#S|RUNWOW|[]#");
        ProgON = HIGH;
        delay(50);
      } else if (UID_LONG == 7424) {
        LastUID_LONG = UID_LONG; //sets lastUID
        Serial.println("#S|RUNFOX[]#");
        ProgON = HIGH;
        delay(50);
      }
    }
  } else {
    if (UID_LONG == !LastUID_LONG){
      ProgON = LOW; // kunt ge gebruiken samen met LastUID_LONG om laaste programma te killen
      //kill previous prog use prog with LastUID_LONG
    }
  }
}

but the problem is it will not run the 2nd command (RUNFOX). and it will not load the other tag unless i reset the arduino. should it be feasible to add a reset function after it loaded the 1st tag?

i went over my head with this, and the downside is i have to get it finished by next week >_<

  // Dump debug info about the card. PICC_HaltA() is automatically called.
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

Why? Either you scanned a tag successfully, or you didn’t. GoBetwino doesn’t give a f**k either way.

  for (byte i = 0; i < mfrc522.uid.size; i++) {
   // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    //Serial.print(mfrc522.uid.uidByte[i], HEX);
  }

Loop several times doing nothing. Why?

  String  UID_string =  (String)UID_unsigned;

This stump-stupid INCORRECT cast is still here. DELETE THIS INSTANCE! It is WRONG!

  UID_LONG=(long)UID_unsigned;

Why do you need to cast an unsigned long to a long? Why do you need to copy the value AT ALL?

  if (ProgON == LOW) {

Since you appear to be trying to determine if a program is on (whatever the heck on means), the type of ProgON should be boolean. The VALUES assigned to ProgON should be true or false.

WHY CAN YOU NOT FOLLOW CONVENTION?

What is GoBetwino seeing now? ONLY YOU HAVE THE HARDWARE AND THE CONFIGURATION ON THE PC. Share what you see or don’t bother posting again.

i have to keep this, otherwise it wont start WoW, if i comment it, it wont start.

 UID_LONG=(long)UID_unsigned;

i changed the ProgON to a boolean (also changing LOW/HIGH to false/true, but nothing changed in what runs and what doesnt. i dont know how i can express my problems better, do you need a film or something.

What it should do:

tag on reader > read UID > run program associated with ID (WoW) > swap  to another tag > read another UID > start firefox.

what it does: - 1st tag on reader > read UID > run wow > swap to 2nd tag > wont read > try 1st tag again > wont read

  • 2nd tag on reader > read UID > reads command but gives error > try 1st tag > wont read > try 2nd tag again > wont read

the problem that i have now is that the reader wont read anymore after using a tag, resulting in an arduino/gobetwino reset. (maybe bacuase it wont set progON to false soon enough).

  • 2nd tag on reader > read UID > reads command but gives error

that we can't see.

I suspect that you have more than one problem. Decide whether actually starting the applications is more important or if getting the order of data sent to the PC is more important.

If getting the order of data was more important, then use the Serial Monitor app to do the testing.

Delete the ProgON statement and ALL of the code that deals with it.

Read and print every tag as it is scanned, doing NOTHING else.

If you can successfully read every tag, regardless of the order in which they are scanned, then you can start to add code to do something with the data.

Start by creating a function that tells you JUST whether the scanned tag is the WOW tag, the FOX tag, or the 'who knows what the f**k that tags supposed to do" tag.

Then, instead of printing the tag information, print the tag number (WOW = 1, FOX = 2, unknown = 0).

Next, replace the tag number with the command to start the appropriate application using GoBetwino.

When each tag start the appropriate app (you will need to make sure that you don't scan the same tag twice), then you can add logic to keep track of whether you have started each app. I don't see how you can do that with one boolean, but I might be missing something.