Arduino - 处理以保存和显示数据 [英] Arduino - Processing to save and display the data
问题描述
我的 Arduino 代码:
Code for my Arduino:
#include<EngduinoThermistor.h>
void setup()
{
Serial.begin(9600);
EngduinoThermistor.begin();
}void loop()
{
float temp;
temp = EngduinoThermistor.temperature();
Serial.println(temp);
delay(1000);
}
我的处理代码:
import processing.serial.*;
Serial port;
float x = 0;
void setup() {
size(500, 400);
println(Serial.list());
String portName = Serial.list()[0];
port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
}
void draw() {
}
void serialEvent(Serial port) {
float inByte = port.read();
println(inByte);
stroke(90, 76, 99);
line(x, height, x, height - inByte);
if (x >=width) {
x=0;
background(200);
}
x++;
}
我真的很努力地尝试理解处理,但我仍然不明白如何根据arduino发送的数据绘制图形.我认为我主要遇到的问题是线路部分.我不知道如何画一条连接上一个点和新点的线.
I have tried really hard to understand processing but I still don't understand how to draw a graph based on the data sent by the arduino. I think the problem that I have mostly is the line part. I don't know how to draw a line connecting the previous point and the new point.
但总的来说,问题甚至不起作用......问题在哪里:(?
But overall the problem does not even work.... Where is the problem :( ?
推荐答案
根据Engduino 热敏电阻文档(pdf 链接),该值为浮点数.
According to the Engduino Thermistor Documentation (pdf link), the value is a float.
因为您使用 println()
发送值,所以有一个换行符 ('\n'
) 与浮点值一起发送.这实际上可以与 Processing Serial 的 bufferUntil() 函数结合使用.
Because you're sending the value using println()
, there is a new line character ('\n'
) sent along with the floating point value. This can actually be useful in conjuction with Processing Serial's bufferUntil() function.
缺少的主要成分实际上是从接收到的字符串中解析一个 float
.这是一个基本的转换示例:
The main missing ingredient is actually parsing a float
from the received String.
Here's a basic conversion example:
String temperatureString = "37.5";
float temperatureFloat = float(temperatureString);
您可以使用 Serial 的 readString() 来获取字符串,然后trim() 去掉空格,最后转换成浮点数:
You can use Serial's readString() to get the string, then trim() it to remove whitespace, then finally convert it to a float:
temperature = float(port.readString().trim());
检查转换是否成功也是一个好主意:
It's also a good idea to check if the conversion was successful:
if(!Float.isNaN(temperature)){
println("read temperature",temperature);
x++;
}
总的来说,检查错误是个好主意,所以这里有一个执行上述操作并检查串行端口和注释的版本:
Overall it's a good idea to check for errors, so here's a version that does the above and checks the serial ports as well and comments:
import processing.serial.*;
//serial port
Serial port;
//x position of current value on graph
float x = 0;
//current temperature reading
float temperature;
void setup() {
size(500, 400);
background(200);
String[] portNames = Serial.list();
println(portNames);
String portName = "not found";
//loop through available serial ports and look for an Arduino (on OSX something like /dev/tty.usbmodem1411)
for(int i = 0 ; i < portNames.length; i++){
if(portNames[i].contains("tty.usbmodem")){
portName = portNames[i];
break;
}
}
//try to open the serial port
try{
port = new Serial(this, portName, 9600);
//buffer until new line character (since values are send via println() from Arduino)
port.bufferUntil('\n');
}catch(Exception e){
System.err.println("Arduino port " + portName);
e.printStackTrace();
}
}
void draw() {
//draw graph
stroke(90, 76, 99);
//you might want to map the temperature to sketch dimensions)
line(x, height, x, height - temperature);
if (x >=width) {
x=0;
background(200);
}
}
void serialEvent(Serial port) {
try{
//read the string, trim it (in case there's a '\n' character), then convert it to a float
temperature = float(port.readString().trim());
//check if the float conversion was successfull (didn't get a NaN value)
if(!Float.isNaN(temperature)){
println("read temperature",temperature);
x++;
}
}catch(Exception e){
System.err.println("error parsing value from serial port");
e.printStackTrace();
}
}
这篇关于Arduino - 处理以保存和显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!