Why the signalMin is always 0?

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[] = "HG8145DV5_3DD8F0E";
const char pass[] = "rSCvgF9E2GE";
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;

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()
{
    if (!client.connected()) {
    connect();
    }
    client.loop();
   
   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);
   
   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

14:07:48.064 -> sample: 667	signalMax: 1018	signalMin: 0	peakToPeak: 1018	volts: 49.71	dB: 91.93	dB-spl: 107.93
14:07:49.051 -> sample: 579	signalMax: 1019	signalMin: 0	peakToPeak: 1019	volts: 49.76	dB: 91.94	dB-spl: 107.94
14:07:50.083 -> sample: 476	signalMax: 1018	signalMin: 0	peakToPeak: 1018	volts: 49.71	dB: 91.93	dB-spl: 107.93
14:07:51.144 -> sample: 452	signalMax: 1018	signalMin: 0	peakToPeak: 1018	volts: 49.71	dB: 91.93	dB-spl: 107.93
14:07:52.176 -> sample: 481	signalMax: 1018	signalMin: 0	peakToPeak: 1018	volts: 49.71	dB: 91.93	dB-spl: 107.93

I wonder why it always stays at maximum value

No idea … but if add a few print values you might see what’s happening with the variables

My only other thought is that maybe integers are of a different length in that red thing , not sure that would be an issue or not

I edited it. Thank you