【问题标题】:void serialEvent Arduino - Processing to avoid lagvoid serialEvent Arduino - 处理以避免滞后
【发布时间】:2014-05-07 20:01:24
【问题描述】:

我在将数据从 Arduino 发送到处理时遇到问题,我可以在绘图循环中正常接收数据,但它非常慢,所以我包含了一个 serialEvent() 函数来尝试消除一些滞后,但现在它似乎没有读取 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]);

}
}

【问题讨论】:

  • 那是很多代码。如果您被卡住并且没有得到答案,请尝试消除与问题无关的位 - 您可以通过串行端口读取 anything 吗?不要猜测整个程序的问题出在哪里,而是尝试取出部分,直到你有更好的主意。
  • 我同意 kevinsa5。取出除 SerialEvent 函数之外的所有内容,然后开始添加内容。您可能还想尝试使用“port.bufferUntil(end);”在您的设置中,然后在您的 SerialEvent 函数中添加“readString()”。

标签: serial-port arduino processing


【解决方案1】:

老问题,但它首先出现在 Google 搜索中,所以这是我的理解。

serialEvent() 似乎没有像文档所说的那样被串行事件调用,至少在我使用的 Arduino 平台上。它可能对某些人有效。如果它不起作用,请尝试在主循环中调用 serialEvent 。我认为无论如何这都是等效的,因为串行数据确实没有中断。您只需要阅读它,其想法是每个循环都会调用 serialEvent 函数,但它似乎不是那样工作的。

SerialEvent Arduino 文档说: “注意:目前,serialEvent() 与 Esplora、Leonardo 或 Micro 不兼容”但我怀疑该列表应该更长。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2011-11-28
    相关资源
    最近更新 更多