【发布时间】:2014-05-02 23:14:57
【问题描述】:
我正在开发一个程序,该程序将消息从 Arduino 发送到处理,将它们作为十进制数字从 arduino 打印到串行端口,然后在处理中将它们提取到字符串数组中。我遇到的问题是:“ArrayIndexOutOfBounds:18”,它突出显示 dlay = Integer.parseInt(B[18]) 的行,我认为该数组不够大,无法存储 17 岁以后的任何内容,而且我我可能犯了一个非常愚蠢的错误,但我不知道如何扩展它!
有什么建议吗?
提前非常感谢!
import processing.serial.*; //import the Serial library
int end = 10;
String serial;
Serial port;
int c;
int d;
int e;
int f;
int g;
int a;
int b;
int C;
int p1t;
int p2t;
int p3t;
int p4t;
int p5t;
int p6t;
int p7t;
int p8t;
int pan;
int reverb;
int dlay;
int distort;
//ellipse parameters
int noteOn = 0;
int col1 = 0;
int col2 = 0;
int col3 = 0;
String[] A;
String[] B;
void setup() {
size(600,600);
frameRate(30);
port = new Serial(this, Serial.list()[7], 115200);
port.clear();
serial = port.readStringUntil(end);
serial = null; // initially, the string will be null (empty)
}
void draw() {
while (port.available() > 0) {
serial = port.readStringUntil(end);
}
if (serial != null) { //if the string is not empty, print the following
A = split(serial, ',');
B = trim(A);
c = Integer.parseInt(B[1]);
if (c == 1){
col1 = 255;
col2 = 0;
col3 = 0;
}
d = Integer.parseInt(B[2]);
if (d == 1){
col1 = 0;
col2 = 0;
col3 = 255;
}
e = Integer.parseInt(B[3]);
if (e == 1){
col1 = 0;
col2 = 255;
col3 = 0;
}
f = Integer.parseInt(B[4]);
if (f == 1){
col1 = 255;
col2 = 0;
col3 = 255;
}
g = Integer.parseInt(B[5]);
if (g == 1){
col1 = 255;
col2 = 255;
col3 = 0;
}
a = Integer.parseInt(B[6]);
if (a == 1){
col1 = 0;
col2 = 255;
col3 = 255;
}
b = Integer.parseInt(B[7]);
if (b == 1){
col1 = 50;
col2 = 250;
col3 = 130;
}
C = Integer.parseInt(B[8]);
if (C == 1){
col1 = 200;
col2 = 90;
col3 = 75;
}
if(c == 1 || d == 1 || e == 1 || f == 1 || g == 1 || a == 1 || b == 1 || C == 1){
noteOn = 1;
}
else {
noteOn = 0;
}
p1t = Integer.parseInt(B[9]);
p2t = Integer.parseInt(B[10]);
p3t = Integer.parseInt(B[11]);
p4t = Integer.parseInt(B[12]);
p5t = Integer.parseInt(B[13]);
p6t = Integer.parseInt(B[14]);
p7t = Integer.parseInt(B[15]);
p8t = Integer.parseInt(B[16]);
pan = Integer.parseInt(B[17]);
dlay = Integer.parseInt(B[18]);
//reverb = Integer.parseInt(B[19]);
//distort = Integer.parseInt(B[18]);
}
fill(0, 0,0, 255);
rect(0,0, 600,600);
fill(col1,col2, col3);
ellipse(300+pan, 300, (noteOn*p8t), (noteOn*p8t));
}
【问题讨论】:
标签: arrays string arduino processing