gpt4 book ai didi

serial-port - void serialEvent Arduino - 处理以避免滞后

转载 作者:行者123 更新时间:2023-12-05 07:58:48 26 4
gpt4 key购买 nike

我在将数据从 Arduino 发送到 Processing 时遇到问题。我可以在绘图循环中正常接收数据,但它非常慢,所以我包含了一个 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]);
}
}

最佳答案

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

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

关于serial-port - void serialEvent Arduino - 处理以避免滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23527268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com