代码之家  ›  专栏  ›  技术社区  ›  Josh Sandlin

从Arduino发送到Processing的奇怪随机数据

  •  4
  • Josh Sandlin  · 技术社区  · 17 年前

    我试着从光电池电阻器和我的Arduino Decimila读取数据,然后通过处理实时绘制它。

    应该非常简单;但对我来说,这有点像一场噩梦。

    我在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
    }
    

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

    哈尔普?

    2 回复  |  直到 8 年前
        1
  •  4
  •   Josh Sandlin    17 年前

    在仔细查看了手头的资源后,我意识到这个问题已经为我解决了 http://arduino.cc

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

        2
  •  2
  •   Mateo Sanchez    12 年前

    您可以使用Plotly Arduino API传输该数据,该API与文档和设置一起提供 here . 基本思想:您可以连续地从Arduino传输数据,或传输单个数据块。

    然后,如果要将其嵌入到站点中,则需要获取URL并使用以下代码段:

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

    您可以更改该代码段中的宽度/高度标注。注意:您需要在那里交换您自己的URL以使其通过流。

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

    enter image description here

    充分披露:我为Plotly工作。

    推荐文章