1
votes

The arduino code read the rfid tag [RFID reader is ID-12], compare it with Tag1 and Tag2, if it is equal send on Serial Port 1 or 2, this value is read by processing that should play song1 or song2.

Arduino Code:

/*
Documentazione: http://www.logicaprogrammabile.it/arduino-leggere-tag-rfid-id-12/

*/

byte datarfid[16];
byte i = 0;
int LD1 = 8;
int LD2 = 6;

String tag ="";

void setup()
{
//init seriale a 9600 bps
  pinMode(LD1, OUTPUT);
  pinMode(LD2, OUTPUT);
  Serial.begin(9600);

}

void loop()
{
  digitalWrite(LD1, LOW);
  digitalWrite(LD2, LOW);   
  //attendo la presenza di tutti e 16 byte provenienti
  //dal lettore id-12
  if(Serial.available() >= 16)
  {
    //leggo ogni byte e lo memorizo nell’array
    //precedentemente definito
    for(i = 0; i <= 15; i++)
    {
      datarfid[i] = Serial.read();
    }

    //ora ricavo i 10 byte che compongono il codice del tag
    //e li concateno in un ogetto stringa
    //da notare la conversione dei byte in char
    for(i = 1; i <= 10; i++)
    {
      //concateno la stringa convertendo i byte in char
      tag += (char)datarfid[i];
    }

    //ora verifico se il codice del tag corrisponde
    //a quello memorizzato nel sistema
    if(tag == "0107EB826F"){
      /*
      Serial.print("Song");
      Serial.print(":");
      */
      Serial.println(1);
      digitalWrite(LD1, HIGH);   
      delay(500);              
      digitalWrite(LD1, LOW);

    }
    else if (tag == "0F0002F973"){
      /*
      Serial.print("Song");
      Serial.print(:");
      */
      Serial.println(2);
      digitalWrite(LD1, HIGH);   
      delay(500);              
      digitalWrite(LD1, LOW);
    }      
    else
    {
      Serial.print(0);
      digitalWrite(LD2, HIGH);   
      delay(500);              
      digitalWrite(LD2, LOW);
    }
    tag="";
  }
}

Processing Code:

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;

    import processing.serial.*;
    Minim minim;

    Serial myPort;
     String Tag1;
     String Tag2;
     String inString;

     int lf = 10;



    //this is the object that plays your file
    AudioPlayer player;
    AudioPlayer player2;

    void setup()
    {
      size(300, 300);

      //initialize minim
      minim = new Minim(this);
     player=minim.loadFile("Song1.mp3");
     player2=minim.loadFile("Song2.mp3");

      // List all the available serial ports:
      println(Serial.list());
      // Open the port you are using at the rate you want:
      myPort = new Serial(this, Serial.list()[0], 9600);
      myPort.bufferUntil(lf);

      Tag1="1";
      Tag2="2";



    }

    void draw()
    {
     background(0);

     text("Ricevuto:" +inString, 10, 50);



    if (inString!=null){


      if (inString==Tag1)
      {
        text("Song 1",10,80);
        player.play();
      }

      if (inString==Tag2)
      {
        text("Song 2",10,100);
        player2.play();
      }
    }
  }

    void serialEvent(Serial p) {
          inString = (myPort.readString());

    }


    void mousePressed()
    {
      //it's weird but you have to rewind a file to play it
      player.rewind();
      player.play();
    }

When i try to compare the inString with another String in an if condition it doesn't do that and it doesn't copy the inString to another String

2

2 Answers

0
votes

You are almost there, but just a bit off in terms of string comparison.

  1. Comparing Strings in Java/Processing. It's safer to use use String's equals() method (or equalsIgnoreCase() )
  2. When you send Serial.println(2); from Arduino, you're sending the number 2, not the character '2'(which don't have the same value, try println(2 == '2');. When using println() it might be worth also removing any extra white spaces(spaces/tabs/new line/carriage return/etc) using trim()

Try using something like this in Processing:

void serialEvent(Serial p) {
          inString = trim(myPort.readString());

    }

then

if (inString.equals(Tag1))
      {
        text("Song 1",10,80);
        player.play();
      }

      if (inString.equals(Tag2))
      {
        text("Song 2",10,100);
        player2.play();
      }

In case the song starts over and over again (because the condition will be true to play a song until Arduino sends another message), you might want to use a boolean variable to keep track if a song has started or not. AudioPlayer already provides something in the form of isPlaying():

if (inString.equals(Tag1))
          {
            text("Song 1",10,80);
            if(!player.isPlaying()) player.play();
          }

          if (inString.equals(Tag2))
          {
            text("Song 2",10,100);
            if(!player2.isPlaying()) player2.play();
          }
0
votes

The documentation from processing says that you cannot compare strings using the == operator, although you can do it with arduino.

You should for instance replace if (inString==Tag1) with if (inString.equals(Tag1) == true).