arduino代码读取rfid
标签[RFID阅读器为ID-12],将其与Tag1
和Tag2
进行比较,如果它等于串行端口1或2的发送,该值由应播放song1或song2的处理读取。
Arduino代码:
/*
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="";
}
}
处理代码:
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();
}
当我尝试将inString
与if条件中的另一个String进行比较时,它不会这样做,并且它不会将inString
复制到另一个字符串
答案 0 :(得分:0)
来自处理的文档说you cannot compare strings using the ==
operator,you can do it with arduino。
您应该将if (inString==Tag1)
替换为if (inString.equals(Tag1) == true)
。
答案 1 :(得分:0)
你几乎就在那里,但在字符串比较方面稍微偏了一点。
Serial.println(2);
时,您发送的是号码2
,而不是字符'2'
(不具有相同的值,请尝试println(2 == '2');
。使用println()
时,使用trim() 尝试在Processing:
中使用类似的东西void serialEvent(Serial p) {
inString = trim(myPort.readString());
}
然后
if (inString.equals(Tag1))
{
text("Song 1",10,80);
player.play();
}
if (inString.equals(Tag2))
{
text("Song 2",10,100);
player2.play();
}
如果歌曲一遍又一遍地开始(因为在Arduino发送另一条消息之前条件将是真的播放歌曲),你可能想要使用布尔变量来跟踪歌曲是否已经开始。 AudioPlayer已经提供了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();
}