Update:我的建议是首先尝试自己将Arduino与Processing进行通信。这就是我下面所描述的。如果您想直接使用Processing直接控制Arduino,Binary Nerd提供的链接是您入门的最佳选择。
Update 2: 也试试这个链接:Netbeans 和处理 http://processing.org/discourse/yabb2/YaBB.pl?num=1191468953
Arduino 代码在 Arduino 上运行,而处理代码在您的计算机上运行。如果您想通过Processing控制您的Arduino,您很可能会使用串行端口,并创建两个程序。其中之一,在 Arduino 上,可能会接收命令并执行操作(打开或关闭 LED),或发回答案。另一个在处理中,可能会向 Arduino 发送必要的命令并以某种方式处理它的答案。
下面是我为一个 LED 和一个模拟输入制作的一个简单示例。这是未经测试的代码。按照步骤。完成此操作后,您可以尝试在 Netbeans 中直接使用 Arduino 进行处理。
Step 1. Arduino
- 购买一块 Arduino 板。
- 下载 Arduino IDE (http://www.arduino.cc http://www.arduino.cc)
- 将 Arduino 连接到您的计算机。
- 将 Arduino 代码(如下)复制到 Arduino IDE 中。
- 上传到Arduino。
Step 2. 加工
- 下载处理 IDE。
- 将处理代码(如下)复制到处理 IDE 中。
- 确保代码中的 COM 端口是 Arduino 连接的端口。
- 运行处理代码。
Arduino代码:
int ledPin = 13;
int analogPin = 0;
char c = 0;
void setup()
{
pinMode( ledPin, OUTPUT );
Serial.begin( 9600 );
}
void loop()
{
// Wait for a character to arrive at the serial port.
if( Serial.available() > 0 )
{
// Read one byte (character).
c = Serial.read();
switch( c )
{
case '1':
// Turn LED on.
digitalWrite( ledPin, HIGH );
break;
case '0':
// Turn LED off.
digitalWrite( ledPin, LOW );
break;
case 'q':
case 'Q':
// Send the reading from the analog pin throught the serial port.
Serial.println( analogRead( analogPin ) );
break;
}
}
}
处理代码(在您的计算机上运行)。
import processing.serial.*;
Serial serial;
String str;
void setup()
{
size(400, 400);
serial = new Serial(this, "COM1", 9600); // Use the serial port connected
// to your Arduino.
while( true )
{
serial.write( '1' ); // Turn LED on.
delay( 1000 ); // Wait one second
serial.write( '0' ); // Turn LED off.
delay( 1000 );
serial.write( 'Q' ); // Get analog reading
serial.bufferUntil( 10 ); // Wait for the data from the Arduino.
// This captures characters until a newline
// is received, the runs serialEvent()...
}
}
void draw()
{
background(0);
}
void serialEvent(Serial s)
{
println( s.readString() );
}