Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试将analogRead电位器的值映射到 0-1 之间。所以我这样做:
analogRead
float inverse_value = 1.0f / (float)analogRead( pot_pin )
但是当电位器的analogRead为0时,倒数为0(这是正确的),但是当analogRead在1023时,倒数变成了0.0009775170。
0.0009775170
我是在正确地划分还是什么?
所有电路连接正确。并且 Arduino 运行正常(上传时我没有收到任何错误)。
你想做的,大概是这样的:
float inverse_value = (float)analogRead(pot_pin) / 1023.0f;
假设最大位置是 1023。
这样,0 到 1023 将被映射为 0 到 1。
例如,当电位器处于中间位置(512)时,变为:
float inverse_value = 512.0f / 1023.0f;
这是关于0.5,可能是你想要得到的。
0.5