奇怪的随机数据从 Arduino 发送到处理
我正在尝试从光电管电阻器和我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仔细查看手头的资源后,我意识到问题已经被 http:// 的人们解决了。 arduino.cc
哦,我可以有多少时间如果我早点看到的话就保存了。
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
Oh how much time I could have saved if I had seen that earlier.
您可以使用 Plotly Arduino API 传输该数据,此处提供了该数据以及文档和设置。 基本思想:您可以连续地从 Arduino 传输数据,或传输单个数据块。
然后,如果您想将其嵌入到网站中,您需要获取 URL 并使用此代码段:
您可以更改该代码段中的宽度/高度尺寸。 注意:您需要在那里交换您自己的 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:
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
Full disclosure: I work for Plotly.