修訂Arduino 與Processing 通訊一:從Processing 中接收Arduino 的數據

https://www.wandianshenme.com/play/arduino-processing-communication-part-1-processign-receive-arduino-data/#-1-from-arduino-

步驟1:From Arduino...


void setup()
{
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);
}

void loop()
{
//send 'Hello, world!' over the serial port
Serial.println("Hello, world!");
//wait 100 milliseconds so we don't drive ourselves crazy
delay(100);
}

步骤2:...to Processing


import processing.serial.*;



Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port


void setup()
{
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  // String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  String portName = "COM15"; //change the number  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
}

步驟3:結論

優秀!我們現在征服瞭如何將數據從Arduino 發送數據到Processing。我們的下一步就是找出相反的方式- 從Processing 到Arduino 發送數據。
原文鏈接:https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing

留言