我在将数据从Arduino发送到处理时遇到问题,我可以在绘制循环中接收数据,但是它非常慢,所以我包含了一个serialEvent()函数来尝试消除一些lagginess,但是现在它似乎没有读取serialEvent函数中的数据,我无法弄清楚为什么生活!
我常常想念愚蠢的错误,所以如果是这样的话,我很抱歉!
谢谢
import themidibus.*;
import processing.serial.*;
int end = 10;
String serial;
Serial port;
MidiBus myBus;
//incoming data
int c;
int d;
int e;
int f;
int g;
int a;
int b;
int C;
int pitchPot;
int tonalityPot;
int noteVol;
int pan;
int reverb;
int dlay;
int distort;
int octave;
//ellipse parameters
int noteOn = 0;
int col1 = 0;
int col2 = 0;
int col3 = 0;
int passTime;
int saveTime;
int expand = 0;
String[] A = new String[18];
String[] B = new String[18];
boolean cPress = false;
boolean dPress = false;
boolean ePress = false;
boolean fPress = false;
boolean gPress = false;
boolean aPress = false;
boolean bPress = false;
boolean CPress = false;
void setup() {
size(600,600, P3D);
frameRate(10);
smooth();
port = new Serial(this, Serial.list()[6], 115200);
port.clear();
serial = port.readStringUntil(end);
serial = null; // initially, the string will be null (empty)
myBus = new MidiBus(this, -1, "Java Sound Synthesizer");
}
void draw() {
if ((c == 1) && (cPress == false)){
col1 = 255;
col2 = 0;
col3 = 0;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (60 + pitchPot + octave), noteVol);
cPress = true;
}
if ((c == 0) && (cPress == true)){
myBus.sendNoteOff(1, (60 + pitchPot + octave), noteVol);
cPress = false;
}
//D
if ((d == 1) && (dPress == false)){
col1 = 0;
col2 = 0;
col3 = 255;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (62 + pitchPot + octave), noteVol);
dPress = true;
}
if ((d == 0) && (dPress == true)){
myBus.sendNoteOff(1, (62 + pitchPot + octave), noteVol);
dPress = false;
}
if ((e == 1) && (ePress == false)){
col1 = 0;
col2 = 255;
col3 = 0;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (64 + pitchPot + tonalityPot + octave), noteVol);
ePress = true;
}
if ((e == 0) && (ePress == true)){
myBus.sendNoteOff(1, (64 + pitchPot + tonalityPot + octave), noteVol);
ePress = false;
}
if ((f == 1) && (fPress == false)){
col1 = 255;
col2 = 0;
col3 = 255;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (65 + pitchPot + octave), noteVol);
fPress = true;
}
if ((f == 0) && (fPress == true)){
myBus.sendNoteOff(1, (65 + pitchPot + octave), noteVol);
fPress = false;
}
if ((g == 1) && (gPress == false)){
col1 = 255;
col2 = 255;
col3 = 0;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (67 + pitchPot + octave), noteVol);
gPress = true;
}
if ((g == 0) && (gPress == true)){
myBus.sendNoteOff(1, (67 + pitchPot + octave), noteVol);
gPress = false;
}
if ((a == 1) && (aPress == false)){
col1 = 0;
col2 = 255;
col3 = 255;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (69 + pitchPot + tonalityPot + octave), noteVol);
aPress = true;
}
if ((a == 0) && (aPress == true)){
myBus.sendNoteOff(1, (69 + pitchPot + tonalityPot + octave), noteVol);
aPress = false;
}
if ((b == 1) && (bPress == false)){
col1 = 50;
col2 = 250;
col3 = 130;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (71 + pitchPot + octave), noteVol);
bPress = true;
}
if ((b == 0) && (bPress == true)){
myBus.sendNoteOff(1, (71 + pitchPot + octave), noteVol);
bPress = false;
}
if ((C == 1) && (CPress == false)){
col1 = 200;
col2 = 90;
col3 = 75;
myBus.sendControllerChange(1, 10, pan);
myBus.sendNoteOn(1, (72 + pitchPot + octave), noteVol);
CPress = true;
}
if ((C == 0) && (CPress == true)){
myBus.sendNoteOff(1, (72 + pitchPot + octave), noteVol);
CPress = false;
}
if(c == 1 || d == 1 || e == 1 || f == 1 || g == 1 || a == 1 || b == 1 || C == 1){
noteOn = 1;
}
else {
noteOn = 0;
}
if (dlay == 1){
if (noteOn == 1){
passTime=millis() - saveTime;
if (passTime > 1){
expand+= 15;
saveTime = millis();
}
strokeWeight(10);
stroke(col1,col2,col3);
noFill();
ellipse((300+pan), 300 , expand, expand);
}
else {
expand = noteVol*2;
noteOn = 0;
}
fill(0, 0, 0, 100);
rect(0,0, 600,600);
fill(col1,col2, col3);
lights();
pushMatrix();
translate(300+pan, 300);
noStroke();
sphere(noteOn*noteVol);
popMatrix();
}
else if (reverb == 1){
fill(0, 0, 0, 100);
rect(0,0, 600,600);
fill(col1,col2, col3);
lights();
pushMatrix();
translate(300, 300);
rotate(frameCount / 50.0);
star(0, 0, (noteOn*noteVol* 20), ((noteOn*noteVol)/20), 40);
popMatrix();
}
else {
fill(0, 0, 0, 100);
rect(0,0, 600,600);
fill(col1,col2, col3);
lights();
pushMatrix();
translate(300+pan, 300);
noStroke();
sphere(noteOn*noteVol);
popMatrix();
}
}
void serialEvent(Serial port){
while (port.available() > 0) {
serial = port.readStringUntil(end);
}
if (serial != null) {
A = split(serial, ',');
B = trim(A);
c = Integer.parseInt(B[1]);
d = Integer.parseInt(B[1]);
e = Integer.parseInt(B[2]);
f = Integer.parseInt(B[3]);
g = Integer.parseInt(B[4]);
a = Integer.parseInt(B[5]);
b = Integer.parseInt(B[6]);
C = Integer.parseInt(B[7]);
pitchPot = Integer.parseInt(B[9]);
tonalityPot = Integer.parseInt(B[10]);
noteVol = Integer.parseInt(B[11]);
pan = Integer.parseInt(B[12]);
dlay = Integer.parseInt(B[13]);
reverb = Integer.parseInt(B[14]);
distort = Integer.parseInt(B[15]);
octave = Integer.parseInt(B[16]);
}
}
答案 0 :(得分:0)
旧问题,但它首先出现在谷歌搜索中,所以这是我的理解。
串口事件似乎没有调用serialEvent(),就像文档中至少在我使用的Arduino平台上所说的那样。它可能适用于某些人。如果它不起作用,请尝试在主循环中调用serialEvent。我认为这是等效的,因为串行数据确实没有中断。你只需要阅读它,并且想法是每个循环都会调用serialEvent函数,但它似乎不会那样工作。SerialEvent Arduino文档说: "注意:目前,serialEvent()与Esplora,Leonardo或Micro"不兼容。但我怀疑名单应该更长。