我对处理和arduino都很陌生。我将 3 个植物和 3 个人类连接到一个用 Arduino 读取的简单测谎仪。我正在通过 Seriel 端口将值发送到处理小瓶。在处理中,我想根据 Arduino 字符串的输入值触发声音。这是我到目前为止走了多远(2种方法)。我在这一行得到一个空指针异常:
int iv = int(val.trim());
非常感谢任何提示。我已经浏览论坛好几天了 :) 加上我觉得我很接近。
代码1:
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Serial myPort;
String val;
String portName = Serial.list()[2];
int iv = 0;
Minim minim;
AudioSample a;
AudioSample b;
AudioSample c;
AudioSample d;
AudioSample e;
AudioSample f;
AudioSample g;
AudioSample h;
AudioSample i;
AudioSample j;
AudioSample k;
AudioSample l;
AudioSample m;
AudioSample n;
AudioSample o;
AudioSample p;
AudioSample q;
AudioSample r;
AudioSample s;
AudioSample t;
AudioSample u;
AudioSample v;
AudioSample w;
AudioSample aa;
AudioSample ab;
AudioSample ac;
AudioSample ad;
AudioSample ae;
AudioSample af;
AudioSample ag;
AudioSample ah;
AudioSample ai;
AudioSample aj;
AudioSample ak;
AudioSample al;
AudioSample am;
AudioSample an;
AudioSample ao;
AudioSample ap;
AudioSample aq;
AudioSample ar;
void setup() {
size(200, 100, P3D);
background(0);
println(Serial.list());
myPort = new Serial(this, portName, 9600);
minim = new Minim(this);
a = minim.loadSample("1.mp3",128);
b = minim.loadSample("2.mp3",128);
c = minim.loadSample("3.mp3",128);
d = minim.loadSample("4.mp3",128);
e = minim.loadSample("5.mp3",128);
f = minim.loadSample("6.mp3",128);
g = minim.loadSample("7.mp3",128);
h = minim.loadSample("8.mp3",128);
i = minim.loadSample("9.mp3",128);
j = minim.loadSample("10.mp3",128);
k = minim.loadSample("11.mp3",128);
l = minim.loadSample("12.mp3",128);
m = minim.loadSample("13.mp3",128);
n = minim.loadSample("14.mp3",128);
o = minim.loadSample("15.mp3",512);
p = minim.loadSample("16.mp3",512);
q = minim.loadSample("17.mp3",512);
r = minim.loadSample("18.mp3",512);
s = minim.loadSample("19.mp3",512);
t = minim.loadSample("20.mp3",512);
u = minim.loadSample("21.mp3",512);
w = minim.loadSample("22.mp3",512);
aa = minim.loadSample("23.mp3",512);
ab = minim.loadSample("24.mp3",512);
ac = minim.loadSample("25.mp3",512);
ad = minim.loadSample("26.mp3",512);
ae = minim.loadSample("27.mp3",512);
af = minim.loadSample("28.mp3",512);
ag = minim.loadSample("29.mp3",512);
ah = minim.loadSample("30.mp3",512);
ai = minim.loadSample("31.mp3",512);
aj = minim.loadSample("32.mp3",512);
ak = minim.loadSample("33.mp3",512);
al = minim.loadSample("34.mp3",512);
am = minim.loadSample("35.mp3",512);
an = minim.loadSample("36.mp3",512);
ao = minim.loadSample("37.mp3",512);
ap = minim.loadSample("38.mp3",512);
aq = minim.loadSample("39.mp3",512);
ar = minim.loadSample("40.mp3",512);
}
void draw() {
//delay(2000);
if (myPort.available() >= 0) {
val = myPort.readStringUntil('\n');
}
int iv = int(val.trim());
if(val != null) println(val);
if(iv == 0) a.trigger();
if(iv == 1) b.trigger();
if(iv == 2) c.trigger();
if(iv == 3) d.trigger();
if(iv == 4) e.trigger();
if(iv == 5) f.trigger();
if(iv == 6) g.trigger();
if(iv == 7) h.trigger();
if(iv == 8) i.trigger();
if(iv == 9) j.trigger();
if(iv == 10) k.trigger();
if(iv == 11) l.trigger();
if(iv == 12) m.trigger();
if(iv == 13) n.trigger();
if(iv == 14) o.trigger();
if(iv == 15) p.trigger();
if(iv == 16) q.trigger();
if(iv == 17) r.trigger();
if(iv == 18) s.trigger();
if(iv == 19) t.trigger();
if(iv == 20) u.trigger();
if(iv == 21) w.trigger();
if(iv == 22) aa.trigger();
if(iv == 23) ab.trigger();
if(iv == 24) ac.trigger();
if(iv == 25) ad.trigger();
if(iv == 26) ae.trigger();
if(iv == 27) af.trigger();
if(iv == 28) ag.trigger();
if(iv == 29) ah.trigger();
if(iv == 30) ai.trigger();
if(iv == 31) aj.trigger();
if(iv == 32) ak.trigger();
if(iv == 33) al.trigger();
if(iv == 34) am.trigger();
if(iv == 35) an.trigger();
if(iv == 36) ao.trigger();
if(iv == 37) ap.trigger();
if(iv == 38) aq.trigger();
if(iv == 39) ar.trigger();
}
代码2:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
String portName = Serial.list()[2];
int v = 0;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
static final int BITRATE = 128;
static final String EXT = ".mp3";
//String[] names = new String[250];
final static String[] names = {
"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","20", "20", "21", "22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40"
};
final static int numSongs = names.length;
AudioPlayer [] songs = new AudioPlayer[numSongs];
Minim minim;
void setup()
{
size(512,320,P3D);
frameRate(60);
smooth(4);
//println(Serial.list());
myPort = new Serial(this, portName, 9600);
minim = new Minim(this);
}
void draw()
{
background(0);
stroke(255);
if (myPort.available() > 0) {
val = myPort.readString();
}
int v = int(val.trim());
if (val != null) println(val);
for (int i = 0; i != numSongs;
songs[i] = minim.loadFile(names[i++] + EXT, BITRATE));
songs[v].play();
delay(9000);
}
第二个代码最有效。过了一会儿,他抛出了一个 java 头内存错误(类似的东西)
再次感谢您的帮助