极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8704|回复: 3

关于串口输出格式的小问题

[复制链接]
发表于 2013-12-8 19:49:21 | 显示全部楼层 |阅读模式
本帖最后由 1900 于 2013-12-8 23:27 编辑

传感器的三个模拟信号输入Mango2的A0,A1,A2端,串口输出数字信号,想让一条数据的三个值在同一行显示
一下是我的代码
void loop() {
  // read the input on analog pin 0:
  
  int sensorValue0 = analogRead(A0);
  int sensorValue1 = analogRead(A1);
  int sensorValue2 = analogRead(A2);
  // print out the value you read:
   a = ++a;
  Serial.print(a);
  Serial.print(".");
  Serial.println(sensorValue0);
  Serial.print(".");
  Serial.println(sensorValue1);
  Serial.print(".");
  Serial.println(sensorValue2);
  delay(200);        // delay in between reads for stability
}
用串口监视器查看输出,看到自动换行了(如下),请问怎样让一条数据三个值在一行里面显示?
12.0
.0
.62
13.188
.0
.592
14.478
.28
.273
15.468
回复

使用道具 举报

发表于 2013-12-8 21:56:24 | 显示全部楼层
将值输出到一个 string 中然后一起输出试试看
     char buf[32];               //Output buffer
      snprintf(buf, sizeof(buf), "%d %d" value1,value2);      
      lcd.print(buf);
回复 支持 反对

使用道具 举报

发表于 2013-12-8 23:18:10 | 显示全部楼层
  1. void loop() {
  2.   // read the input on analog pin 0:
  3.   
  4.   int sensorValue0 = analogRead(A0);
  5.   int sensorValue1 = analogRead(A1);
  6.   int sensorValue2 = analogRead(A2);
  7.   // print out the value you read:
  8.    a = ++a;
  9.   Serial.print(a);
  10.   Serial.print(".");
  11.   Serial.print(sensorValue0);
  12.   Serial.print(".");
  13.   Serial.print(sensorValue1);
  14.   Serial.print(".");
  15.   Serial.println(sensorValue2);
  16.   delay(200);        // delay in between reads for stability
  17. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-12-8 23:26:36 | 显示全部楼层
pathletboy 发表于 2013-12-8 23:18
void loop() {
  // read the input on analog pin 0:
  

原来print是接着输出,println是换行输出,多谢指教!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-18 10:25 , Processed in 0.045607 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表