Button 基于串行数据的if语句(处理)

Button 基于串行数据的if语句(处理),button,if-statement,arduino,processing,Button,If Statement,Arduino,Processing,我有一个Arduino发送串行数据给处理器。像“3High”、“3Low”之类的东西 我希望处理过程不断读取数据,并使用if语句触发事件。例如,如果它看到字符串“4High”,它会做一些事情 这就是我所拥有的,但我无法理解If语句 import processing.serial.*; Serial myPort; // Create object from Serial class String val; // Data received from the serial port

我有一个Arduino发送串行数据给处理器。像“3High”、“3Low”之类的东西

我希望处理过程不断读取数据,并使用if语句触发事件。例如,如果它看到字符串“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);    
  }


}

处理是基于Java的语言,比较两个字符串的正确方法是使用
equals()
函数。有关字符串的详细信息,请阅读或

if(val.equals(“4High”)){
println(“是”);
}

为了读取串行数据,您应该考虑使用“代码> SerialEngult代码>如语句,如在您的示例中。

只需将比较的首选样式添加到<代码>字符串 >文字是“代码>文字”。“4High”。等于(val)。