【问题标题】:Arduino - Processing to save and display the dataArduino - 处理以保存和显示数据
【发布时间】:2016-11-09 18:10:14
【问题描述】:

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

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

【问题讨论】:

  • 也许你应该调用这个函数...

标签: arduino processing


【解决方案1】:

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

将您的问题分解为更小的步骤。

暂时忘记 Arduino。你能创建一个小示例草图,显示基于一些硬编码数据的图表吗?先让它工作。

当你得到它的工作时,然后尝试将你的图表建立在硬编码值之外的其他东西上。也许尝试随机值,或者基于鼠标位置的值等。重点不是连接你的 Arduino,而是让动态图工作。

除此之外,获取另一个忽略图形并仅连接到 Arduino 的基本示例草图。将您从 Arduino 获得的值打印到控制台。

最后,当您让所有这些单独工作时,将它们连接起来以创建基于 Arduino 数据的图表会更容易。

如果您在某个步骤上遇到困难,可以发布MCVE 以及具体问题。祝你好运。

【讨论】:

    【解决方案2】:

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

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

    主要缺失的成分实际上是从收到的字符串中解析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++;
         }
    

    总的来说,检查错误是个好主意,所以这里有一个版本,可以执行上述操作并检查串行端口和 cmets:

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

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多