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

发布于 2024-07-08 10:10:55 字数 1388 浏览 13 评论 0原文

我正在尝试从光电管电阻器和我的 Arduino Diecimila 读取数据然后使用 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 的“串行监视器”中查看数据时,我得到了一个看起来有效的持续数据流。

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

I'm trying to read data from a photocell resistor and my Arduino Diecimila and then graph it in real-time with Processing.

It should be painfully simple; but it’s growing into a little bit of a nightmare for me.

The code I'm running on my 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
}

The code I'm running in 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
}

I've tested both bits of code separately, and I know that they work. It's only when I try to have the input from the Arduino going to Processing that the problems start.

When I view the data in Arduino's "Serial Monitor", I get a nice constant flow of data that seems to look valid.

But when I read that same data through Processing, I get a repeating pattern of random values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-07-15 10:10:55

仔细查看手头的资源后,我意识到问题已经被 http:// 的人们解决了。 arduino.cc

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

哦,我可以有多少时间如果我早点看到的话就保存了。

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.

自由范儿 2024-07-15 10:10:55

您可以使用 Plotly Arduino API 传输该数据,此处提供了该数据以及文档和设置。 基本思想:您可以连续地从 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 才能使其流通过。

以下是传输 Arduino 数据的示例

在此处输入图像描述

全面披露:我为 Plotly 工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文