carduinoparticle-photon

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


I am just starting to learn how to program using a particle Photon. I am trying to get my Photon to measure sound levels using an Adafruit MAX 9814.

I can get it to output a voltage, but when I try to categorize noise levels, I get this error:

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);
}

Solution

  • You want this:

    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);
    }
    

    You call noisevalue once here: float nv = noisevalue(); and then you process nv.

    BTW h should probably be replaced by nv, see comment in the code.

    Be aware that following is most likeky wrong even if it compiles:

     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);
    

    this calls noisevalue multiple times and the noisevalue function looks expensive. So it's better to call it only once as suggested in the first part of this answer.