楼主: Limei-333526

[视频] Arduino和Processing

[复制链接]

该用户从未签到

发表于 2012-8-8 11:44:39 | 显示全部楼层 |阅读模式
分享到:
原帖由
putiandiao
发自:dev.eefocus.com
------------------------------------------------------------------------------------------------------------------------------------------

开源硬件的最大好处是降低了爱好者的入门门槛,看到一大堆底层操作的设置,会把大部分潜在的爱好者拒之门外,这也许也是开源硬件如此风行的缘故吧。。。
很长一段时间都没有上来了,自从欧洲杯开打之后,坛子就冷清了不少 003.gif
processing同样是开源的,使用起来和Arduino是差不多的。关于Arduino的高级应用中,有不少都是有Processing的影子的。主要用于设计交互图形,同样是降低了入门门槛,具有强大的拓展性和创造性!
开源硬件中最重要的是创意,而不是那些枯燥的实现技术,倘若不是如此,便丧失了开源硬件的精髓了。。。
选择Processing是因为可以和Arduino无缝连接,通过串口,将数据用图形显示出来!学习Processing的一个缘由也是因为MPIDE中自带的例子中有Processing的代码。看不懂,然后就查资料了。顺便学习了下。推荐爱上Processing,与爱上Arduino相呼应。
1.jpg

废话少说,直接上例程代码:
首先是Arduino代码: 2.jpg
程序很简单,采样A0口,使用滑动变阻器获取0~3.3V之间的电压,转换成0~1024之间的数值(AD为10位),先看串口调试:
3.jpg
下面采用Processing将数据图形化显示。


Processing代码如下:
// Processing code for this example

// Graphing sketch


// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, followed by a newline, or newline and carriage return

// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// This example code is in the public domain.

import processing.serial.*;

Serial myPort;        // The serial port  创建Serial对象myPort
int xPos = 1;         // horizontal position of the graph  左上角为00坐标
//初始化函数,仅仅执行一次
void setup ()
{
// set the window size:
size(400, 300);      

// List all the available serial ports  列出系统连接的所有串行设备
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my  Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[2], 9600);  //固定的写法,[]中填写Arduino的串口标号
//new 关键字是创建一个“新”对象。
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');  //直到换行符出现就触发调用serialEvent()
// set inital background:
background(0);    //初始化背景,有清屏的作用
}



//此函数循环执行
void draw () {
// everything happens in the serialEvent()
}

//serialEvent()需要bufferUntil()触发才可被执行,所以将其放在void draw()函数体之外
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);//除去数组或字符串的首位的空格等whitespace
// convert to an int and map to the screen height:
float inByte = float(inString);   //强制类型转换
inByte = map(inByte, 0, 1023, 0, height);  //范围转换

// draw the line:
stroke(127,34,255);//画笔颜色设置
line(xPos, height, xPos, height - inByte);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);//清屏操作,颜色可自主设定
}
else {
// increment the horizontal position:
xPos++;
}
}
}

  
最重要的是:
4.jpg
是为了获得Arduino所使用的串口,以便于从串口获取数据。程序运行结果如下:
5.jpg
调节滑动变阻器,可以获得不同的电压,程序中将0~1023的数值转换成了0~255显示,因为线路松动接触不良问题,中间的为抖动。这只是最基本的历程,在任何一本关于Processing的书上都可以找到。
共同学习! 6.gif
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条

手机版|小黑屋|与非网

GMT+8, 2024-3-29 15:33 , Processed in 0.118296 second(s), 16 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.