Why the signalMin is always 0?

CODE: https://sites.google.com/site/myscratchbooks/home/projects/project-19-using-max9812-mensaure-decibels-db-db-spl

I've seen this code on the internet and its really working but when i try to integrate it to node-red using mqtt protocol the sigMin value is always at 0.

#define BROKER_IP    "192.168.254.110"
#define DEV_NAME     "mqttdevice"
#define MQTT_USER    "mqtt_user"
#define MQTT_PW      "mqtt_password"

#include <WiFiNINA.h>
#include <MQTT.h>

const char* ssid = "HG8145V5_3D80E";
const char* pass = "rSCvg92E";


const char topic[]  = "motor";

WiFiClient net;
MQTTClient client;

const int sensitivity = -58; 
const int gain = 20;            
const int sensorPIN = A0;
const int sampleWindow = 1000;  
unsigned int sample;
unsigned long lastMillis = 0;

void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.print("\nconnecting...");
  while (!client.connect(DEV_NAME, MQTT_USER, MQTT_PW)) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nconnected!");
  client.subscribe("\mqtt_table_1"); //SUBSCRIBE TO TOPIC /hello
}

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
}

void setup()
{
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  client.begin(BROKER_IP, 1883, net);
  //client.onMessage(messageReceived);
  connect();
}

void loop()
{
    client.loop();
    if (!client.connected()) {
    connect();
    }
    
   
   unsigned long startMillis= millis();  
   unsigned int peakToPeak = 0;         
   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;      

   // collect data for Sample window width in mS 
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(sensorPIN);
      
      if (sample < 1024)
      {
       
         if (sample > signalMax)
         {
            signalMax = sample;
         }
         else if (sample < signalMin)
         {
            signalMin = sample;
         }
      }
   }
   peakToPeak = signalMax - signalMin;              
   
   double volts = ((peakToPeak * 5.0) / 1024)*10;  
                                                       
   double volts_db = 20*log10(volts/0.001259); 
                                                                   
   double spl_db = volts_db + 94 + sensitivity - gain; 

   String dB_mqtt =  String(spl_db, 2);

   if (millis() - lastMillis > 1000) {
   lastMillis = millis();
      String dB_mqtt =  String(spl_db, 2);
      client.publish("mqtt_table_1", dB_mqtt);
 }
              

   Serial.print("sample: " + String(sample) );
   Serial.print("\t");
   Serial.print("signalMax: " + String(signalMax) );
   Serial.print("\t");
   Serial.print("signalMin: " + String(signalMin) );
   Serial.print("\t");
   Serial.print("peakToPeak: " + String(peakToPeak) );
   Serial.print("\t");
   Serial.print("volts: " + String(volts) );
   Serial.print("\t");                        // prints a tab
   Serial.print("dB: " + String(volts_db) );
   Serial.print("\t");
   Serial.println("dB-spl: " + String(spl_db) );
   
}

Here is the output

15:16:33.088 -> sample: 522	signalMax: 1023	signalMin: 0	peakToPeak: 1023	volts: 49.95	dB: 91.97	dB-spl: 107.97
15:16:34.104 -> sample: 509	signalMax: 1023	signalMin: 0	peakToPeak: 1023	volts: 49.95	dB: 91.97	dB-spl: 107.97
15:16:35.142 -> sample: 502	signalMax: 1023	signalMin: 0	peakToPeak: 1023	volts: 49.95	dB: 91.97	dB-spl: 107.97
15:16:36.136 -> sample: 508	signalMax: 1023	signalMin: 0	peakToPeak: 1023	volts: 49.95	dB: 91.97	dB-spl: 107.97
15:16:37.170 -> sample: 513	signalMax: 1023	signalMin: 0	peakToPeak: 1023	volts: 49.95	dB: 91.97	dB-spl: 107.97
15:16:38.209 -> sample: 660	signalMax: 1023	signalMin: 0	peakToPeak: 1023	volts: 49.95	dB: 91.97	dB-spl: 107.97
15:16:39.244 -> sample: 534	signalMax: 1023	signalMin: 0	peakToPeak: 1023	volts: 49.95	dB: 91.97	dB-spl: 107.97

In that code sample is set by a call to analogRead, meaning its value will be between 0 and 1024.

sigMin is initialised with the value 0.

It only gets changed if sample is smaller - which it never will be.

The fix is to initialise sigMin to 1024 so that it starts bigger than any possible value of sample.

1 Like

Thank you for pointing it out i missed that one.
I dont know really why but i still got same output the signalMin still at 0

Get rid of the else in the above line.

I removed it and still the same output. Maybe there is some interuption in my code after i integrated the mqtt?

The output you have shows signalMax is always 1023 and signalMin is always 0. There must be some flaw in the logic, but at this point, I can't see anything obvious. The MQTT part won't be changing the local variables.

At this point I would add some more debug inside the while loop to check what is actually happening.

1 Like

Thank you very much. I really appreciate it