RFID, Unity, Arduino


hello,

i'm working on schoolproject musea exhibition.
at moment i'm controlling 3d model in unity 2 poteniameters rotate , exploded 3d model.

now have added rfid sensor scanning materials (rfid tags).

with current script in unity (and firmatestandard on arduino) don't rfid working.
when using 2nd arduino rfid separate arduino sketch , unity script worked well, need space 2 arduino's.

the current unity script potentiameters standardfirmata on arduino.
code: [select]

using unityengine;
using system.collections;
using uniduino;
using system.io.ports;

public class rotatie : monobehaviour {
public arduino arduino;


/********* pins *********/  
public int _pin_potentialmtr = 1; // potentialmeter rotatie
public int _pin_exploded = 2; // potentialmeter exploded view
public byte _pin_rfid = 13; // rfid

/********* gameobjects *********/
/*
* ************** chair part **************
*
* poten
* kuip
* kussen rug
* kussen zitvlak
* schroeven rug
*
*/
public gameobject arduino2;

public gameobject chairlegs;
public gameobject chairtub;
public gameobject chairbackcushion;
public gameobject chairbottomcushion;
public gameobject chairbackscrews;
public gameobject chairtotal;
serialport port;

void start( )
{
// uitlezen van tweede arduino (potentialmeters)
if (arduino2 != null) {

if (arduino2.getcomponent<arduino>().connected)
{
int data = arduino2.getcomponent<arduino>().analogread(13);
debug.log(data);
        }
}





arduino = arduino.global;
arduino.setup(configurepins);


//port = new serialport("/dev/tty.usbmodem1411", 57600);
//port.open ();
//port.readtimeout = 1;




/********* chair model *********/
chairlegs = gameobject.find("chairlegs");
chairtub = gameobject.find("chairtub");
chairbackcushion = gameobject.find("chairbackcushion");
chairbottomcushion = gameobject.find("chairbottomcushion");
chairbackscrews = gameobject.find("chairbackscrews");

chairtotal = gameobject.find ("pod_chair");
}
void configurepins( )
{
/********* potentialmeter rotation *********/
arduino.pinmode(_pin_potentialmtr, pinmode.analog);
arduino.reportanalog(_pin_potentialmtr, 1);

/********* potentialmeter exploded view *********/
arduino.pinmode(_pin_exploded, pinmode.analog);
arduino.reportanalog(_pin_exploded, 1);

/********* rfid *********/
arduino.pinmode(_pin_rfid, pinmode.output);
arduino.reportanalog(_pin_rfid, 1);

}
void update(){



if (arduino.isopen) {
debug.log ("arduino: "+arduino.analogread(_pin_rfid).tostring());
}



int rotatiewaarde = arduino.analogread(_pin_potentialmtr); // potentialmeter
/********* rotate chair 360 degreas *********/
chairtotal.transform.rotation = quaternion.euler (0,1f*rotatiewaarde,0);

int explodedwaarde = arduino.analogread(_pin_exploded); // potentialmeter

/********* chair model push objects away *********/
chairlegs.transform.position = new vector3(0.002f,-0.002f*explodedwaarde,0);
chairtub.transform.position = new vector3(-0.001f*explodedwaarde,0.0001f*explodedwaarde,0);
chairbackcushion.transform.position = new vector3(0.001f*explodedwaarde,0*explodedwaarde,0.007f*explodedwaarde);
chairbottomcushion.transform.position = new vector3(-0.003f*explodedwaarde,-0.001f*explodedwaarde,0.0001f*explodedwaarde);
chairbackscrews.transform.position = new vector3(-0.001f*explodedwaarde,0.003f*explodedwaarde,0);


}
}


arduino script separate unity script on serial

arduino script
code: [select]

int rfidresetpin = 13;

//register rfid tags here
char tag1[13] = "100008005d45";
char tag2[13] = "10000800dac2";

void setup(){
  serial.begin(9600);

  pinmode(rfidresetpin, output);
  digitalwrite(rfidresetpin, high);

  //only needed if controling these pins - eg. leds
  pinmode(2, output);
  pinmode(3, output);
}

void loop(){

  char tagstring[13];
  int index = 0;
  boolean reading = false;

  while(serial.available()){

    int readbyte = serial.read(); //read next available byte

    if(readbyte == 2) reading = true; //begining of tag
    if(readbyte == 3) reading = false; //end of tag

    if(reading && readbyte != 2 && readbyte != 10 && readbyte != 13){
      //store tag
      tagstring[index] = readbyte;
      index ++;
    }
   
  }

  checktag(tagstring); //check if match
  cleartag(tagstring); //clear char of value
  resetreader(); //eset rfid reader
}

void checktag(char tag[]){
///////////////////////////////////
//check read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need contunue

  if(comparetag(tag, tag1)){ // if matched tag1, this
//serial.println("left");
serial.write(1);
serial.flush();
delay(20);

  }else if(comparetag(tag, tag2)){ //if matched tag2, this
//serial.println("right");
  serial.write(2);
serial.flush();
delay(20);
 
}else{
    serial.println(tag); //read out unknown tag
    delay(20);
  }

}


void resetreader(){
///////////////////////////////////
//reset rfid reader read again.
///////////////////////////////////
  digitalwrite(rfidresetpin, low);
  digitalwrite(rfidresetpin, high);
  delay(150);
}

void cleartag(char one[]){
///////////////////////////////////
//clear char array filling null - ascii 0
//will think same tag has been read otherwise
///////////////////////////////////
  for(int = 0; < strlen(one); i++){
    one[i] = 0;
  }
}

boolean comparetag(char one[], char two[]){
///////////////////////////////////
//compare 2 value see if same,
//strcmp not working 100% this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int = 0; < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}

unity code rfid
code: [select]

using system.collections;
using system.io.ports;
using unityengine;

public class showhide : monobehaviour {
serialport sp = new serialport("/dev/tty.usbmodem1451", 9600);


// use initialization
void start () {

sp.open();
sp.readtimeout = 1;

}
renderer[] listofchildren;
// update called once per frame
void update () {
if (sp.isopen) {
try {
aanuit (sp.readbyte ());
print (sp.readbyte ());
} catch (system.exception) {
}
}
}
void aanuit (int test) {
if (test == 1) {

listofchildren = getcomponentsinchildren<renderer>();
foreach(renderer child in listofchildren)
{
child.enabled = false;

}


}
if (test == 2) {
listofchildren = getcomponentsinchildren<renderer>();
foreach(renderer child in listofchildren)
{
child.enabled = true;

}
}

}
}


my question how last rfid script working in de first unity script uniduino. or better how data rfid in first script.

i've posted topic on uniduino , unity forums els more support in smaller time.

thanks time, greets.

quote
and firmatestandard on arduino)


that problem firmate firmate does, not rfid.
if want arduino rfid have program arduino , not turn arduino dumb i/o port, firmate does.


Arduino Forum > Using Arduino > Programming Questions > RFID, Unity, Arduino


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support