在arduino中打印格式

问题描述 投票:0回答:1
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;  // float distance ;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.println(distance);
}

我想得到

1 作为 01 对于int

2.54 作为 02.54 对于浮动

在我的arduino串行监视器中。请问我怎么去做。我的传感器发出的值没有把零放在前面,这是正常的。我怎样才能编辑打印格式呢?谢谢大家。

printing arduino format sensor content-length
1个回答
0
投票

最简单的方法就是简单。

if (distance < 10) Serial.write('0'); Serial.println(distance);

不在乎负的ints,这对于距离来说可能是好的。

© www.soinside.com 2019 - 2024. All rights reserved.