我无法让草图在实际时间内响应我的ardunio。大约有30秒的延迟。随着草图继续运行,延迟变得更长。
我的目标:在Arduino的某些条件下,一个精灵将在处理过程中发挥作用。有4个条件(1 0,1 1 2 0 2 2 1)。
代码:
int numFrames = 7;
int currentFrame = 0;
PImage[] images = new PImage[numFrames];
import processing.serial.*;
Serial myPort;
String val;
void setup() {
size(1000,600);
frameRate(5);
images[0] = loadImage("images/heart(1).gif");
images[1] = loadImage("images/heart(2).gif");
images[2] = loadImage("images/heart(3).gif");
images[3] = loadImage("images/heart(4).gif");
images[4] = loadImage("images/heart(5).gif");
images[5] = loadImage("images/heart(6).gif");
images[6] = loadImage("images/heart(7).gif");
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(255);
//Display Serial Data
if (myPort.available() > 0) {
if ( (val = myPort.readStringUntil(ENTER)) != null ) val = trim(val);
else return;
if (val != null) {
println(val);
}
}
//control sketch
currentFrame = (currentFrame+1) % numFrames; // Use % to cycle through frames
int offset = 0;
if ("1 1".equals(val)) {
image(images[(currentFrame+offset) % numFrames], 333, 200);
offset+=2;
}
if("2 1".equals(val)){
image(images[(currentFrame+offset) % numFrames], 666, 200);
offset+=2;
player.play();
}
}
非常感谢任何帮助!