模拟读数计算错误
Calculations with analog readings coming out wrong
我正在编程一个Arduino Uno,由于在对一些传感器值进行计算时遇到问题,我陷入了while循环。当我读取我的四个传感器值并比较前两个与后两个的比率时,无论值是多少,我都会得到0.00。这是我的代码:
while ( ( (frontleft + frontright) / (backleft + backright) <= 0.8 ) || ( (frontleft + frontright) / (backleft + backright) > 1.2) ) {
if (backleft + backright > frontleft + frontright) {
myStepper2.step(10);
}
if (backleft + backright < frontleft + frontright) {
myStepper2.step(-10);
}
int backleft = analogRead(A0);
int backright = analogRead(A1);
int frontleft = analogRead(A2);
int frontright = analogRead(A3);
Serial.print("Backleft Reading: ");
Serial.println(backleft);
Serial.print("n");
Serial.print("BackRight Reading: ");
Serial.println(backright);
Serial.print("n");
Serial.print("Frontleft Reading: ");
Serial.println(frontleft);
Serial.print("n");
Serial.print("Frontright Reading: ");
Serial.println(frontright);
Serial.print("n");
float ratio = (frontleft + frontright) / (backleft + backright);
float math = (5 + 6) / (4 + 5);
Serial.print("Math: ");
Serial.println(math);
Serial.print("Ratio: ");
Serial.println(ratio);
Serial.print("n");
delay(500);
}
我得到了传感器backleft
、backright
、frontleft
和frontright
的值,但"比值"总是为0.00。正因为如此,我相信我陷入了困境。这是怎么回事?
此外,为了检查一下,我添加了"数学"浮点作为快速计算,但我在串行监视器中得到了1.00的值。为什么?应该是1.222。
尝试
float ratio = (frontleft + frontright) / float((backleft + backright));
while ( ratio <= 0.8 || ratio > 1.2) {
//do your thing here
ratio = (frontleft + frontright) / float((backleft + backright));
}
然后当你试图打印时
Serial.println(ratio, 4) //prints four decimal places
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- OpenCV - Python 断言错误:SAD 算法 - 立体相机视差图计算
- 如何在常量计算表达式中获取编译时错误?
- 计算阶乘的 C++17 倍表达式中的错误
- 为什么 2 个双精度值之间的差值计算错误?
- 我在c++中遇到了一个奇怪的错误,其中一个计算2个小整数加法的语句溢出到一个长值中
- 错误 C2064:术语的计算结果不是采用 3 个参数的函数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- C++ openmp 并行计算计算错误的结果
- 为什么按位运算符在使用与整数中提供的位数相同的位数计算可能的最大范围时会产生错误
- 术语不计算为函数采用 1 个参数错误?
- Travis CI 中奇怪的编译器错误不会在本地计算机上发生
- 点云库:计算筛选关键点 - 输入云错误
- 在线程错误 C2064 中:term 的计算结果不为 0 个参数的函数
- GetAsyncKeyState(VK_RETURN) 错误地计算为 true
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- log2 计算在 CPP 中给出了错误的结果
- 错误计算RSI或错误在MetaTrader 4
- c++程序错误计算