【发布时间】:2014-07-08 01:01:40
【问题描述】:
我在尝试将加速器中的 G 值从 int 更改为 float 时遇到问题。当我简单地将变量类型从 int 更改为 float 时,它的串行输出在静止时看起来像这样:
-26844 -17153 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-26844 -17153 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
-20133 -17217 -26844
mph/s: -0.17
当它只是一个普通的 int 变量类型时,它看起来很正常:
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
0 0 1
mph/s: 0
但是,我需要 G 值的浮点版本,以便准确计算 mph/s。
编辑:出于保密原因,完整代码已被删除。
//we send the x y z values as a string to the serial port
sprintf(str, "%d %d %d", xg, yg, zg);
Serial.print(str);
Serial.write(10);
Serial.print("mph/s: ");
Serial.println(mphs);
//It appears that delay is needed in order not to clog the port
delay(15);
}
【问题讨论】:
-
你能显示你用于浮点版本的确切代码吗?请注意,具有 %d 格式的 sprintf 不适用于他们。
标签: c arduino accelerometer