本帖最后由 清水 于 2012-11-22 21:57 编辑
arduino接线请参考弘毅教程;
接好线后,这是arduino代码 编译环境0022 - int val=0;
- void setup()
- {
- Serial.begin(9600);
- }
- void loop()
- {
- val=analogRead(A0)/4;
- Serial.println( val,BYTE);
- delay(100);
- }
复制代码
接下来是processing代码
[pre lang="processing" line="1"]import processing.serial.*;
Serial port;
float val;
int x;
float easing=0.05;
float easedVal;
void setup()
{
size(440,440);
frameRate(30);
smooth();
String arduinoPort=Serial.list()[0];
port=new Serial(this,arduinoPort,9600);
background(0);
}
void draw()
{
if(port.available()>0)
val=port.read();
val=map(val,0,255,0,height);
float targetVal=val;
easedVal+=(targetVal-easedVal)* easing;
stroke(0);
line(x,0,x,height);
stroke(255);
line(x+1,0,x+1,height);
line(x,220,x,val);
line(x,440,x,easedVal+220);
x++;
if(x>width)
{
x=0;
}
}[/code]
效果图
|