'float()' 和 'double' 类型的无效操作数到二进制 'operator<' void loop()

invalid operands of types 'float()' and 'double' to binary 'operator<' void loop()

我刚开始学习如何使用粒子 Photon 进行编程。 我正在尝试让我的 Photon 使用 Adafruit MAX 9814 测量声级。

我可以让它输出电压,但是当我尝试对噪声级别进行分类时,出现此错误:

invalid operands of types 'float()' and 'double' to binary 'operator<' void loop() {

#include "math.h"

void setup() {
pinMode(A0, INPUT);
// Serial.begin(9600);
//Serial.println("setup");
}

void loop() {
//Serial.println(noiseValue());
//delay(10000);
Particle.publish("Sound", String(noiseValue()));
delay(5000);

if (noiseValue < 130.00) Particle.publish("noiseValue", "Green", PUBLIC);
if (noiseValue >= 130.00 && h < 145.00) Particle.publish("noiseValue",      "Yellow", PUBLIC);
if (noiseValue >= 145.00) Particle.publish("noiseValue", "Red", PUBLIC);
delay(10000);
}

float noiseValue() {
int sampleWindow = 50; // Sample window width. 50ms = 20Hz
int signalMax = 0;
int signalMin = 4095;
unsigned long startMillis = millis();
unsigned int sample;
unsigned int peakToPeak;

while (millis() - startMillis < sampleWindow) {
sample = analogRead(A0);
if (sample < 4095) {
  if (sample > signalMax) {
    signalMax = sample;
  } else if (sample < signalMin) {
    signalMin = sample;
  }
}
}

peakToPeak = signalMax - signalMin;

return 20 * log(peakToPeak);
}

你想要这个:

void loop() {
  //Serial.println(noiseValue());
  //delay(10000);
  Particle.publish("Sound", String(noiseValue()));
  delay(5000);

  float nv = noiseValue();

  if (nv < 130.00)
    Particle.publish("noiseValue", "Green", PUBLIC);

  if (nv >= 130.00 && nv < 145.00)                          //<<< h replaced by nv !!
     Particle.publish("noiseValue", "Yellow", PUBLIC);

  if (nv >= 145.00)
     Particle.publish("noiseValue", "Red", PUBLIC);

  delay(10000);
}

您在这里调用 noisevalue 一次:float nv = noisevalue(); 然后您处理 nv.

顺便说一句,h 可能应该替换为 nv,请参阅代码中的注释。

请注意,即使编译通过,以下内容也很可能是错误的:

 if (noiseValue() < 130.00)
    Particle.publish("noiseValue", "Green", PUBLIC);

  if (noiseValue() >= 130.00 && noiseValue() < 145.00)                          
     Particle.publish("noiseValue", "Yellow", PUBLIC);

  if (noiseValue() >= 145.00)
     Particle.publish("noiseValue", "Red", PUBLIC);

这会多次调用 noisevalue 并且 noisevalue 函数看起来很昂贵。所以最好只调用它 一次 正如本答案第一部分所建议的那样。