【processing系列I】processing与arduino通信
首先,我们来解决一个问题,那就是processing与arduino通信的意义何在。processing擅长做视觉交互,arduino擅长做电子交互,两者相结合往往能产生出人意料的惊奇化学反应。 = =
比如 = = 你想做一个打地鼠的游戏 {:5_172:}
你就可以在processing里把地鼠们都安排好= = {:5_196:}
然后,用arduino搞几个按键,或者拿个力度感应器,锤子一敲 {:5_174:} 地鼠就被灭掉啦啊哈哈哈哈哈哈
或者,你想在万圣节的时候搞个恶作剧 = ={:5_197:}
你就可以搞个距离检测器(比如超声波、红外之类哒),然后当有人走进屋子的时候,偷偷地把灯关掉{:5_198:}
然后触发processing编好的程序,用个大屏幕 = = 把贞子的视频调出来,再加点夸张的音效{:5_172:}
啊哈哈哈哈哈哈哈!!{:5_182:}
或者,你是个文艺青年(比如说老夫){:5_196:}
就可以做些交互艺术装置玩玩{:5_197:} 当然还得有点人文关怀 = =
你可以猛戳下面这篇来速度了解各种可能性:
Processing与Arduino的互动
小白在这里直接请上大大们的项目视频{:5_185:}
http://player.youku.com/player.php/sid/XMTQxMTU2MDkwMA==/v.swf
利用processing、arduino和webcam做的一个小机器人啊哈哈哈哈~~
http://player.youku.com/player.php/sid/XMTQxMTU2MDAyOA==/v.swf
这个是用arduino做画图控制器的啊哈哈哈哈~~~~~
啊然后如果你要做processing与arduino结合的应用的话,下面是arduino和processing通信的方式:
【方案一】
1. 打开Arduino软件,打开文件,路径:File -> Examples -> Firmata -> StandardFirmata并上传到Arduino中,等几秒,直到LED停止闪烁。
2. 在processing中导入arduino和serial库,在processing中添加一条
<font size="2" face="微软雅黑">arduino = new Arduino(this, Arduino.list(), 57600);</font>其中是要连接的端口,运行以下代码<font size="2" face="微软雅黑">import processing.serial.*;
import cc.arduino.*;
println(Arduino.list());</font>找出arduino所在端口,替换
3. 接下来你就可以在processing里面写arduino代码啦~~~就是语法稍稍有点不一样,基本上就是在每一语句之前加上“arduino.”,具体看官方参考文档,下面有链接。
官方给出的例子:
<font size="2" face="微软雅黑">import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 13;
void setup()
{
//println(Arduino.list());
arduino = new Arduino(this, Arduino.list(), 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);
}
void draw()
{
arduino.digitalWrite(ledPin, Arduino.HIGH);
delay(1000);
arduino.digitalWrite(ledPin, Arduino.LOW);
delay(1000);
}</font>【方案二】
这个方案个人觉得比较不太好用,不过它可以使你的arduino主要控制代码还是在arduino软件中完成。
1.写arduino代码,但是要将需要发送给processing的数据通过serial.print()输出,下例是arduino代码,读取analog 0和1的数据并以0.5s的间隔时间以十进制输出:
<font size="2" face="微软雅黑">int AnalogPin0 = A0; //Declare an integer variable, hooked up to analog pin 0
int AnalogPin1 = A1; //Declare an integer variable, hooked up to analog pin 1
void setup() {
Serial.begin(9600); //Begin Serial Communication with a baud rate of 9600
}
void loop() {
//New variables are declared to store the readings of the respective pins
int Value1 = analogRead(AnalogPin0);
int Value2 = analogRead(AnalogPin1);
/*The Serial.print() function does not execute a "return" or a space
Also, the "," character is essential for parsing the values,
The comma is not necessary after the last variable.*/
Serial.print(Value1, DEC);
Serial.print(",");
Serial.print(Value2, DEC);
Serial.println();
delay(500); // For illustration purposes only. This will slow down your program if not removed
}</font>2.在processing中导入serial库,路径:Sketch-Import Library-serial,注意baud rate(波特率)要与前面的arduino程序相匹配,然后就能得到arduino中传输的数据啦。
下例是与上例相匹配的processing代码:
<font size="2" face="微软雅黑">/*
ARDUINO TO PROCESSING
Read Serial messages from Arduino for use in Processing.
*Even though Serial Library comes with install of Processing, upon first usage, you may be prompted to execute two sudo Terminal
commands after entering your user password*
Created by Daniel Christopher 10/27/12
Public Domain
*/
import processing.serial.*; //import the Serial library
int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages
String serial; // declare a new string called 'serial' . A string is a sequence of characters (data type know as "char")
Serial port;// The serial port, this is a new instance of the Serial class (an Object)
void setup() {
port = new Serial(this, Serial.list(), 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino)
port.clear();// function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino
serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called 'serial')
serial = null; // initially, the string will be null (empty)
}
void draw() {
while (port.available() > 0) { //as long as there is data coming from serial port, read it and store it
serial = port.readStringUntil(end);
}
if (serial != null) {//if the string is not empty, print the following
/*Note: the split function used below is not necessary if sending only a single variable. However, it is useful for parsing (separating) messages when
reading from multiple inputs in Arduino. Below is example code for an Arduino sketch
*/
String[] a = split(serial, ',');//a new array (called 'a') that stores values into separate cells (separated by commas specified in your Arduino program)
println(a); //print Value1 (in cell 1 of Array - remember that arrays are zero-indexed)
println(a); //print Value2 value
}
}</font>
推荐资料:
http://playground.arduino.cc/Interfacing/Processing
http://www.instructables.com/id/Arduino-to-Processing-Serial-Communication-withou/
processing与arduino通信的意义何在。{:5_142:} 大连林海 发表于 2015-12-11 18:06
processing与arduino通信的意义何在。
界面和外界可以交互 大连林海 发表于 2015-12-11 18:06
processing与arduino通信的意义何在。
界面和外界可以交互
页:
[1]