Problem reading a RFID reader

edited October 2015 in Arduino

Hi there,

I have this SparkFun RFID Starter Kit (https://www.sparkfun.com/products/13198?_ga=1.242738306.1686357739.1444361942), I followed the steps in the HookUp guide (https://learn.sparkfun.com/tutorials/sparkfun-rfid-starter-kit-hookup-guide?_ga=1.177165093.1278763627.1444260091), and It seems that is working fine, although I'm not getting the same reads as the guide.

Example: The guide shows this screenshot of the Arduino serial port while reading the card:

And I get this value:

If I swipe the card another time, I get this reading:

What are those little squares? I guess is some kind of character but I don't know which, or why is there.

Any ideas?

I am also trying this RFID reader in Processing. To change a message in the screen if someone swipes the card.

Initial screen:

After one read:

The text "saldo en 0" should have changed to "deposito $50"", but it shouldn't.

Any ideas?

Thanks!!

The code:

import processing.serial.*;
Serial myPort;
String inString = "texto inicial";
String mensaje;

void setup () {
  size(400, 300);        
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
  background(0);
}
void draw () {
  background(0);
  String s = inString;
  String comparador = "4500B8C02A17";
  textSize(24);
  fill(100);
  text(s, height/3, width/3); 

  if (comparador.equals(s) == true) {
    mensaje = "deposito $50";
  } else {
    mensaje = "saldo en 0";
  }
  text(mensaje, height/3, width/3+30);
}

void serialEvent (Serial myPort) {
  inString = myPort.readStringUntil('\n');
}

Answers

  • edited October 2015 Answer ✓
    // forum.processing.org/two/discussion/12920/problem-reading-a-rfid-reader
    // 2015-Oct-09
    
    import processing.serial.Serial;
    
    static final String COMPARADOR = "4500B8C02A17";
    static final String DEPOSITO = "Deposito $50";
    static final String SALDO = "Saldo en 0";
    
    String mensaje = "Texto inicial";
    
    void setup() {
      size(400, 300);
      noLoop();
    
      textAlign(LEFT, BOTTOM);
      textSize(24);
      fill(0200);
    
      String[] portas = Serial.list();
      printArray(portas);
    
      new Serial(this, portas[0], 9600).bufferUntil(ENTER);
    }
    
    void draw() {
      background(0);
    
      text(mensaje, width/3, height>>1);
      text(COMPARADOR.equals(mensaje)? DEPOSITO : SALDO
        , width/3, height/2 + 30);
    
      frame.setTitle("Frame: #" + frameCount);
    }
    
    void serialEvent(Serial s) {
      mensaje = s.readString().trim();
      redraw = true;
    }
    
  • edited October 2015

    @GoToLoop it works! =D> now I need an explanation! :-?

  • edited October 2015

    You'd have to be more specific where you don't get it... >-)

  • @GoToLoop please see my comments in the code, thanks!

    // forum.processing.org/two/discussion/12920/problem-reading-a-rfid-reader // 2015-Oct-09

    import processing.serial.Serial;
    
    static final String COMPARADOR = "4500B8C02A17"; // why static final String ?? I don't know what it is
    static final String DEPOSITO = "Deposito $50";
    static final String SALDO = "Saldo en 0";
    
    String mensaje = "Texto inicial";
    
    void setup() {
      size(400, 300);
      noLoop();
    
      textAlign(LEFT, BOTTOM);
      textSize(24);
      fill(0200);
    
      String[] portas = Serial.list();
      printArray(portas);
    
      new Serial(this, portas[0], 9600).bufferUntil(ENTER); // is the same as myPort.bufferUntil('\n'); ????
    }
    
    void draw () {
      background(0);
      text(mensaje, width/3, height>>1);
      text(COMPARADOR.equals(mensaje)? DEPOSITO : SALDO
        , width/3, height/2 + 30);
      frame.setTitle("Frame: #" + frameCount); // what is this line for??
    }
    
    void serialEvent(Serial s) {
      mensaje = s.readString().trim(); // why do you trim? 
      redraw = true; // what does redraw ???
    }
    
  • edited October 2015

    Why static final String ?? I don't know what it is.

    https://processing.org/reference/final.html
    https://processing.org/reference/static.html

    Is the same as myPort.bufferUntil('\n'); ????

    Yes, ENTER = '\n' = 10 = 0xA. ;)
    Though I'm not using a variable, I'm calling bufferUntil() over the newly created Serial.

    frame.setTitle("Frame: #" + frameCount);

    setTitle() changes window bar's title.
    frameCount: https://Processing.org/reference/frameCount.html

    Why do you trim()?

    B/c Serial is programmed to bufferUntil(ENTER);. So we need to trim() that ending \n: :-B
    https://Processing.org/reference/trim_.html

    What does redraw???

    It's what redraw() does internally: https://processing.org/reference/redraw_.html

    Since noLoop() was issued in setup(), the auto-draw() has been turned off.
    So every time serialEvent() is called back by the Serial's instance, we need to redraw() once.

  • @GoToLoop You are the man! ^:)^

    Two more questions: 1) Why to set the title with the frame count? 2) I didn't know about the noLoop(). Why did you use it? I mean Why no leave the program running the draw automatically like usuall?

    (sorry for my non-native english)

  • edited October 2015
    1. Why to set the title with the frameCount?
      It's just informative. So we know how many times serialEvent() has happened. :P
    2. Why no leave the program running the draw() automatically like usual?
      By default Processing calls back draw() at about 60 FPS.
      We can change that via frameRate() though: https://Processing.org/reference/frameRate_.html
      We could just let it roll of course. But why waste CPU/GPU/APU to display the same thing?
      Variable mensaje only changes inside SerialEvent() after all.
      Until then text() is gonna display the same thing over & over @ 60 FPS.
      It only makes sense to update the canvas once it actually has something new to display!
      Besides, I wanted frameCount to represent how many times serialEvent() had happened.
      W/o noLoop(), frameCount would increase at about 60 each second!
    3. Sorry for my non-native English.
      I'm Brazilian and speak Portuguese! :-h
  • Again, obrigado! =D> very helpful and explicative. Cheers from Argentina. :-h

  • Uma última pergunta: I want that when mensaje is the same as COMPARADOR, the text Deposito $50 is displayed only for 5 seconds and then go back to Saldo en 0 is this OK?

    import processing.serial.Serial;
    
    static final String COMPARADOR = "4500B8C02A17";
    static final String DEPOSITO = "Deposito $50";
    static final String SALDO = "Saldo en 0";
    
    String mensaje = "Texto inicial";
    
    int tiempo;
    int espera = 5000;
    
    void setup() {
      size(400, 300);
    
      textSize(24);
      fill(0200);
    
      String[] portas = Serial.list();
      printArray(portas);
    
      new Serial(this, portas[0], 9600).bufferUntil(ENTER);
    
      tiempo = millis();
    }
    
    void draw () {
      background(0);
      text(mensaje, height/3, width/3);
      text(COMPARADOR.equals(mensaje)? DEPOSITO : SALDO
        , height/3, width/3 + 30);
    
      if (COMPARADOR.equals(mensaje)) {
        if (millis() - tiempo >= espera) {
          mensaje = SALDO;
          tiempo = millis();
        }
      }
      println(mensaje);
    }
    
    void serialEvent(Serial s) {
      mensaje = s.readString().trim();
    }
    
  • edited October 2015
    • Seems so. But then you had to remove noLoop() and thus draw() is called back @ 60 FPS.
    • As an alternative proposal, rather than having draw() looping all the time, just turn it on as long as the display timer is going on.
    • After that, issue noLoop() and await serialEvent() to loop() it again.
    • And since serialEvent() & draw() are run by diff. Threads, I'm relying on synchronized () in order to "atomize" the entire steps.
    • Still you need to consider the possibility that a new "deposit" could happen in less than 5 seconds...

    // forum.processing.org/two/discussion/12920/problem-reading-a-rfid-reader
    // 2015-Oct-10
    
    import processing.serial.Serial;
    
    static final String COMPARADOR = "4500B8C02A17";
    static final String DEPOSITO = "Deposito $50";
    static final String SALDO = "Saldo en 0";
    
    String mensaje = "Texto inicial";
    
    static final int FPS = 1, ESPERA = 5 * FPS;
    int stopFrame;
    
    void setup() {
      size(400, 300);
      frameRate(FPS);
    
      textAlign(LEFT, BOTTOM);
      textSize(24);
      fill(0200);
    
      String[] portas = Serial.list();
      printArray(portas);
    
      new Serial(this, portas[0], 9600).bufferUntil(ENTER);
    }
    
    void draw() {
      synchronized (this) {
        if (frameCount == stopFrame) {
          noLoop();
          mensaje = str(frameCount);
        }
      }
    
      background(0);
      text(mensaje, width/3, height>>1);
      text(COMPARADOR.equals(mensaje)? DEPOSITO : SALDO
        , width/3, height/2 + 30);
    }
    
    synchronized void serialEvent(Serial s) {
      mensaje = s.readString().trim();
      stopFrame = frameCount + ESPERA;
      loop();
    }
    
  • @GoToLoop Again, thanks! very clear and explicative. Now I'll continue a bit by myself :P Have a nice weekend!

  • I'm trying to change the code a bit but with no success L-)

    Why this:

        if (COMPARADOR.equals(mensaje)) {
            mensaje = DEPOSITO;
          } else {
            mensaje = SALDO;
          }
          text(mensaje, height/3, width/3 + 30);
    

    Is no the same as this:

    text(COMPARADOR.equals(mensaje)? DEPOSITO : SALDO, height/3, width/3 + 30);

    I mean... I guess is not the same because the program does different things using one or the other. And the thing is that now I need more things to happen (displaying an image among other simple things) if COMPARADOR equals mensaje, rather than displaying some text.

    Any ideas?

  • edited October 2015

    My mystake, I've updated the code:

            if (COMPARADOR.equals(mensaje)) {
                text(DEPOSITO, height/3, width/3 + 30);
              } else {
                text(SALDO, height/3, width/3 + 30);
              }
    
Sign In or Register to comment.