# Why the signalMin is always 0?

**URL:** https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806
**Category:** General
**Created:** [16 November 2021 07:17 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806 "2021-11-16T07:17:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jhunnx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jhunnx/32/50946_2.png) [@jhunnx](https://discourse.nodered.org/u/jhunnx)
#### Post date: [16 November 2021 07:17 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/1 "2021-11-16T07:17:58Z")

</div>

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

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.

```auto
#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

```auto
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

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [16 November 2021 08:05 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/2 "2021-11-16T08:05:40Z")

</div>

> [@jhunnx](#):
>
> `sample`

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`.

---

<div class="post-metadata">

### Author: ![jhunnx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jhunnx/32/50946_2.png) [@jhunnx](https://discourse.nodered.org/u/jhunnx)
#### Post date: [16 November 2021 08:24 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/3 "2021-11-16T08:24:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [16 November 2021 08:34 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/4 "2021-11-16T08:34:18Z")

</div>

> [@jhunnx](#):
>
> `else if (sample < signalMin)`

Get rid of the `else` in the above line.

---

<div class="post-metadata">

### Author: ![jhunnx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jhunnx/32/50946_2.png) [@jhunnx](https://discourse.nodered.org/u/jhunnx)
#### Post date: [16 November 2021 09:37 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/5 "2021-11-16T09:37:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [16 November 2021 09:57 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/6 "2021-11-16T09:57:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jhunnx](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jhunnx/32/50946_2.png) [@jhunnx](https://discourse.nodered.org/u/jhunnx)
#### Post date: [16 November 2021 12:36 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/7 "2021-11-16T12:36:13Z")

</div>

Thank you very much. I really appreciate it

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [15 January 2022 12:36 UTC](https://discourse.nodered.org/t/why-the-signalmin-is-always-0/53806/8 "2022-01-15T12:36:44Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
