首页 文章

Arduino - 处理以保存和显示数据

提问于
浏览
1

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

但总的来说问题甚至不起作用......问题出在哪里:(?

2 回答

  • 0

    很难回答一般的“我该怎么做”这类问题 . Stack Overflow专为更具体的“我试过X,期望Y,但得到Z而不是”类型问题而设计 . 话虽如此,我会尝试在一般意义上提供帮助:

    Break your problem down into smaller steps.

    忘了Arduino一秒钟 . 你能创建一个小例子草图,它显示基于某些硬编码数据的图形吗?让它先工作 .

    当您开始工作时,请尝试将图表基于硬编码值以外的其他内容 . 也许尝试随机值,或基于鼠标位置等的值 . 关键是不要连接你的Arduino,而是让动态图形工作 .

    与此分开,得到另一个忽略图形的基本示例草图,只是连接到Arduino . 将您从Arduino获得的值打印到控制台 .

    最后,当你将所有这些工作分开工作时,连接它们以根据Arduino的数据创建图形会更容易 .

    如果您遇到其中一个步骤,可以发布MCVE以及特定问题 . 祝好运 .

  • 0

    根据Engduino Thermistor Documentation(pdf链接),该值为浮点数 .

    因为您使用 println() 发送值,所以会有一个新的行字符( '\n' )与浮点值一起发送 . 这实际上可以与Processing Serial的bufferUntil()函数结合使用 .

    主要缺失的成分实际上是从收到的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();
      }
    }
    

相关问题