我刚刚开始学习如何使用粒子光子编程。
我正试图让我的光子测量声级使用一个Adafruit最大9814。
我可以让它输出一个电压,但当我尝试分类噪声级时,我会得到这个错误:
“float()”和“double”类型的操作数对binary“operator”无效{
#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
。BTW
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
函数看起来很昂贵。因此,最好按照这个答案的第一部分中的建议只调用一次。关于c - 类型'float()'和'double'到二进制'operator <'的无效操作数void loop(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39742003/