2015-12-15 03:06:49 [显示全部楼层]
7394浏览
查看: 7394|回复: 0

[入门教程] 【processing系列I】processing与arduino通信

[复制链接]
本帖最后由 iooops 于 2016-12-13 13:13 编辑

首先,我们来解决一个问题,那就是processing与Arduino通信的意义何在。


processing擅长做视觉交互,arduino擅长做电子交互,两者相结合往往能产生出人意料的惊奇化学反应。 = =

比如 = = 你想做一个打地鼠的游戏
你就可以在processing里把地鼠们都安排好= =
然后,用arduino搞几个按键,或者拿个力度感应器,锤子一敲 地鼠就被灭掉啦啊哈哈哈哈哈哈

或者,你想在万圣节的时候搞个恶作剧 = =
你就可以搞个距离检测器(比如超声波、红外之类哒),然后当有人走进屋子的时候,偷偷地把灯关掉
然后触发processing编好的程序,用个大屏幕 = = 把贞子的视频调出来,再加点夸张的音效
啊哈哈哈哈哈哈哈!!

或者,你是个文艺青年(比如说老夫)
就可以做些交互艺术装置玩玩 当然还得有点人文关怀 = =

你可以猛戳下面这篇来速度了解各种可能性:
Processing与Arduino的互动

小白在这里直接请上大大们的项目视频


利用processing、arduino和webcam做的一个小机器人啊哈哈哈哈~~


这个是用arduino做画图控制器的啊哈哈哈哈~~~~~



啊然后如果你要做processing与arduino结合的应用的话,下面是arduino和processing通信的方式:

【方案一】
1. 打开Arduino软件,打开文件,路径:File -> Examples -> Firmata -> StandardFirmata并上传到Arduino中,等几秒,直到LED停止闪烁。
2. 在processing中导入arduino和serial库,在processing中添加一条
  1. arduino = new Arduino(this, Arduino.list()[0], 57600);
复制代码
其中[0]是要连接的端口,运行以下代码
  1. import processing.serial.*;
  2. import cc.arduino.*;
  3. println(Arduino.list());
复制代码
找出arduino所在端口,替换[0]
3. 接下来你就可以在processing里面写arduino代码啦~~~就是语法稍稍有点不一样,基本上就是在每一语句之前加上“arduino.”,具体看官方参考文档,下面有链接。
官方给出的例子:
  1. import processing.serial.*;
  2. import cc.arduino.*;
  3. Arduino arduino;
  4. int ledPin = 13;
  5. void setup()
  6. {
  7.   //println(Arduino.list());
  8.   arduino = new Arduino(this, Arduino.list()[0], 57600);
  9.   arduino.pinMode(ledPin, Arduino.OUTPUT);
  10. }
  11. void draw()
  12. {
  13.   arduino.digitalWrite(ledPin, Arduino.HIGH);
  14.   delay(1000);
  15.   arduino.digitalWrite(ledPin, Arduino.LOW);
  16.   delay(1000);
  17. }
复制代码
【方案二】
这个方案个人觉得比较不太好用,不过它可以使你的arduino主要控制代码还是在arduino软件中完成。
1.写arduino代码,但是要将需要发送给processing的数据通过serial.print()输出,下例是arduino代码,读取analog 0和1的数据并以0.5s的间隔时间以十进制输出:
  1. int AnalogPin0 = A0; //Declare an integer variable, hooked up to analog pin 0
  2. int AnalogPin1 = A1; //Declare an integer variable, hooked up to analog pin 1
  3. void setup() {
  4.   Serial.begin(9600); //Begin Serial Communication with a baud rate of 9600
  5. }
  6. void loop() {
  7.    //New variables are declared to store the readings of the respective pins
  8.   int Value1 = analogRead(AnalogPin0);
  9.   int Value2 = analogRead(AnalogPin1);
  10.   
  11.   /*The Serial.print() function does not execute a "return" or a space
  12.       Also, the "," character is essential for parsing the values,
  13.       The comma is not necessary after the last variable.*/
  14.   
  15.   Serial.print(Value1, DEC);
  16.   Serial.print(",");
  17.   Serial.print(Value2, DEC);
  18.   Serial.println();
  19.   delay(500); // For illustration purposes only. This will slow down your program if not removed
  20. }
复制代码
2.在processing中导入serial库,路径:Sketch-Import Library-serial,注意baud rate(波特率)要与前面的arduino程序相匹配,然后就能得到arduino中传输的数据啦。
下例是与上例相匹配的processing代码:
  1. /*
  2. ARDUINO TO PROCESSING
  3. Read Serial messages from Arduino for use in Processing.
  4. *Even
  5. though Serial Library comes with install of Processing, upon first
  6. usage, you may be prompted to execute two sudo Terminal
  7. commands after entering your user password*
  8. Created by Daniel Christopher 10/27/12
  9. Public Domain
  10. */
  11. import processing.serial.*; //import the Serial library
  12. 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
  13. String serial;   // declare a new string called 'serial' . A string is a sequence of characters (data type know as "char")
  14. Serial port;  // The serial port, this is a new instance of the Serial class (an Object)
  15. void setup() {
  16.   port = new Serial(this, Serial.list()[0], 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino)
  17.   port.clear();  // function from serial library that throws out the first reading, in casewe started reading in the middle of a string from Arduino
  18.   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')
  19.   serial = null; // initially, the string will be null (empty)
  20. }
  21. void draw() {
  22.   while (port.available() > 0) { //as long as there is data coming from serial port, read it and store it
  23.     serial = port.readStringUntil(end);
  24.   }
  25.     if (serial != null) {  //if the string is not empty, print the following
  26.    
  27.    /*  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
  28.     */
  29.    
  30.       String[] a = split(serial, ',');  //a new array (called 'a') that stores values into separate cells (separated by commas specified in your Arduino program)
  31.       println(a[0]); //print Value1 (in cell 1 of Array - remember that arrays are zero-indexed)
  32.       println(a[1]); //print Value2 value
  33.     }
  34. }
复制代码

推荐资料:
http://playground.arduino.cc/Interfacing/Processing
http://www.instructables.com/id/Arduino-to-Processing-Serial-Communication-withou/

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail