我有一个Arduino将串行数据发送到Processing。喜欢" 3High"," 3Low"等...
我希望处理能够不断读取数据并使用if语句来触发事件。 EG,如果它看到字符串" 4High",它会做一些事情。
这就是我所拥有但却无法弄清楚If语句......
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
void setup()
{
size(600, 600);
// Open whatever port is the one you're using.
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
}
println(val); //print it out in the console
if (val == "3High"){
fill(255,200,200);
ellipse(60,20,100,100);
}
if (val == "3Low"){
fill(200);
ellipse(250, 250, 100, 100);
}
}