4

我正在尝试从光电管电阻器和我的 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

}

我在处理中运行的代码:

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 的“串行监视器”中查看数据时,我得到了一个看起来很有效的持续不断的数据流。

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

哈普?

4

2 回答 2

4

After a closer look at the resources at hand, I realized that the problem had already been solved for me by the folks over at http://arduino.cc

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

Oh how much time I could have saved if I had seen that earlier.

于 2008-11-02T02:03:28.523 回答
2

You could transmit that data with the Plotly Arduino API, which along with the documentation and setup is available here. Basic idea: you can continuously stream data from your Arduino, or transmit a single chunk.

Then, if you want to embed it into a site, you'll want to grab the URL and use this snippet:

<iframe id="igraph" src="https://plot.ly/~abhishek.mitra.963/1/400/250/" width="400" height="250" seamless="seamless" scrolling="no"></iframe>

You can change the width/height dimensions in that snippet. Note: you need to swap in your own URL there to get it stream through.

Here's an example of how it looks to stream Arduino data

enter image description here

Full disclosure: I work for Plotly.

于 2013-11-06T02:56:29.680 回答