Arduino-处理以保存和显示数据 [英] Arduino - Processing to save and display the data

查看:297
本文介绍了Arduino-处理以保存和显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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发送的数据绘制图形.我认为我主要遇到的问题是线路部分.我不知道如何画一条连接上一点和新点的线.

但是总的来说,这个问题甚至无法解决....问题出在哪里:(??

解决方案

根据 bufferUntil()函数结合使用时,这实际上很有用./p>

主要缺少的成分实际上是从接收到的String中解析float. 这是一个基本的转换示例:

String temperatureString = "37.5";
float temperatureFloat = float(temperatureString);

您可以使用Serial的 readString()来获取字符串,然后 trim()删除空格,然后最终将其转换为浮点数:

temperature = float(port.readString().trim());

检查转换是否成功也是一个好主意:

if(!Float.isNaN(temperature)){
       println("read temperature",temperature);
       x++;
     }

总的来说,检查错误是个好主意,因此,这是一个执行上述操作并同时检查串行端口和注释的版本:

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();
  }
}

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);
}

Code for my Processing:

 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++;
    }

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 :( ?

解决方案

According to the Engduino Thermistor Documentation (pdf link), the value is a float.

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.

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);

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆