我遇到了嘗試將加速器的G值從int更改爲float的問題。當我簡單地改變變量的類型從int浮動,這是串行輸出看起來像坐在這一點的同時仍:將G值從Arduino加速度計轉換爲浮點數
-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對他們來說效果不佳。 –