当前位置: 首页 > 知识库问答 >
问题:

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

席兴朝
2023-03-14

我的Arduino代码:

java prettyprint-override">#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个答案

商兴朝
2023-03-14

根据Engduino热敏电阻文件(pdf链接),该值是一个浮点值。

因为您使用println()发送值,所以有一个新的行字符('\n')与浮点值一起发送。这实际上在与“处理串行”的缓冲区直到()函数结合时很有用。

缺少的主要成分实际上是从接收到的字符串中解析一个浮点。下面是一个基本的转换示例:

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

可以使用Serial的readString()获取字符串,然后修剪()以删除空白,最后将其转换为浮点:

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();
  }
}
索嘉石
2023-03-14

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

把你的问题分解成更小的步骤。

先别管Arduino了。你能根据一些硬编码数据创建一个显示图表的小示例草图吗?先让它工作起来。

当你开始工作的时候,试着把你的图表建立在硬编码值以外的基础上。也许可以尝试随机值,或者基于鼠标位置的值,等等。重点不是连接Arduino,而是让动态图形工作。

除此之外,还有一个基本的示例草图,它忽略了图形,只连接到Arduino。打印从Arduino到控制台的值。

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

如果你在其中一个步骤上卡住了,你可以发布一个MCVE,并附上一个具体的问题。祝你好运

 类似资料:
  • 所以我实际上正在用arduino和处理制作一个小项目。基本上,我使用C程序在8x8矩阵上绘图。该程序将您的绘图保存为以下格式的文本文件: 并将其显示到连接到arduino的led 8x8矩阵。 因此,首先在处理过程中,我将这个文件加载到一个字符串中,然后逐行将其发送到串行端口: 然后在arduino中,我读取串行端口并将数据存储到字符串中,我将该字符串的每一行转换为十进制。例如,如果字符串[0]的

  • 灵感来自于在WooCommerce@LoicTheAztec的回答中显示购物车和订单项目的重量,通过将产品重量替换为产品尺寸进行了一些更改,我能够在购物车项目和订单项目上随处显示项目尺寸。 问题是我不知道如何显示标签或形容词: 西班牙网站的长度厘米x安乔厘米x阿尔托厘米。 我真的很感谢你的帮助。

  • 我正试图通过(伪)串行连接使用处理从Arduino读取100个传感器数据条目。我使用的加工草图如下: 不幸的是,通常我的data.txt文件中存储的条目不到100个,其中一些条目(大约3-5个)在其中显示换行符。我做错了什么?Arduino IDE的串行监视器未打开!

  • 我有3个片段(片段Home、片段A、片段B和片段C)。应用程序首次运行时将显示片段主页(在Mainactivity中设置)。从导航绘图项可以选择每个片段。每个选定项目都将显示详细信息片段。 我在处理数据和保留片段时遇到问题: (1)。当我选择一个片段(例如片段a)时,将显示片段a的页面。但是当我旋转设备时,为什么我的片段返回到片段主页而不是停留在当前片段??如何处理 (2)。在片段B中,我在Gri

  • 我想做的是从我用Arudino制作的转速表电路中读取连续的数据流,然后将其输入处理;我已经使用下面的代码成功完成了: 我不确定如何处理数据,以便每当检测到某个值时,处理中都会发生事件。 编辑:有人建议关闭,所以我的问题是调用是一个阻塞调用,这意味着中的指令指针将保持在。指针将调用和,但永远不会到达启动串行端口的线路 建议的解决方案是将这些行移动到的顶部,并将作为的最后一行。我尝试了这个(我下面的代

  • 本文向大家介绍基于python-opencv3的图像显示和保存操作,包括了基于python-opencv3的图像显示和保存操作的使用技巧和注意事项,需要的朋友参考一下 基于python-opencv3的图像显示和保存操作,具体代码如下所示: 首先要导入cv2 和 numpy函数库,第四行的cv.imread()函数用于写入一个图像,imred()函数原型Mat imread(const Strin