我正在尝试从光电管电阻器和我的Arduino Decimila读取数据,然后使用Processing实时绘制它。

应该是痛苦的简单;但它对我来说有点噩梦。

代码我在我的Arduino上运行:

int photoPin;

void setup(){

  photoPin = 0;
  Serial.begin( 9600 );

}

void loop(){

  int val = int( map( analogRead( photoPin ), 0, 1023, 0, 254 ) );
  Serial.println( val ); //sending data over Serial

}

代码我在Processing:

中运行
import processing.serial.*;

Serial photocell;

int[] yvals;

void setup(){

  size( 300, 150 );
  photocell = new Serial( this, Serial.list()[0], 9600 );
  photocell.bufferUntil( 10 );
  yvals = new int[width];

}

void draw(){

  background( 0 );
  for( int i = 1; i < width; i++ ){
    yvals[i - 1] = yvals[i];
  }

  if( photocell.available() > 0 ){
    yvals[width - 1] = photocell.read();
  }

  for( int i = 1; i < width; i++ ){
    stroke( #ff0000 );
    line( i, yvals[i], i, height );
  }
  println( photocell.read() ); // for debugging
}

我已经分别测试了两个代码,我知道它们有效。只有当我尝试从Arduino输入处理时才会出现问题。

当我在Arduino的“串行监视器”中查看数据时,我得到了一个看似有效的恒定数据流。

但是当我通过Processing读取相同的数据时,我得到了一个重复的随机值模式。

HALP?

其他提示

仔细观察手头的资源后,我意识到问题已经由人们在 http ://arduino.cc

  

http://arduino.cc/en/Tutorial/Graph

如果我早些时候见过那么,我可以节省多少时间。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top