Did i miss something here? Help

I have this project and when i try to use node-red the values changes

without node-red this is the my code and the serial plotter output.




const int sampleWindow = 50;                             
unsigned int sample;
 
#define SENSOR_PIN A0
const int red_ledPin =  10;
const int orange_ledPin =  9;
const int yellow_ledPin =  8;


void setup ()  
{ 
  Serial.begin(9600);
  pinMode (SENSOR_PIN, INPUT);  
  pinMode(red_ledPin, OUTPUT);
  pinMode(orange_ledPin, OUTPUT);
  pinMode(yellow_ledPin, OUTPUT);
}  
 
   
void loop(){
  
  unsigned long startMillis= millis();                  
  float peakToPeak = 0;
  float volts;                                    
  unsigned int signalMax = 0;                            
  unsigned int signalMin = 1024;                         
                                                
  while (millis() - startMillis < sampleWindow){
     sample = analogRead(SENSOR_PIN);                    
     if (sample < 1024){
        if (sample > signalMax){
            signalMax = sample;                           
        }
        else if (sample < signalMin){
           signalMin = sample;                           
        }
     }
  }

  
  peakToPeak = signalMax - signalMin;                    
  int db = map(peakToPeak,20,900,30,90);
          
        Serial.print("db = "); Serial.println(db,DEC);
        
  
  if (db <= 33){
    yellowBlink();
    
  }
  else if ((db > 33) && (db <= 50)){
    
    orangeBlink();
    
  }
  else if (db > 50){
    redBlink();
    
  }
}

void redBlink(){
    digitalWrite(red_ledPin, HIGH);
    digitalWrite(orange_ledPin, LOW);
    digitalWrite(yellow_ledPin, LOW);
}

void orangeBlink(){
    digitalWrite(orange_ledPin, HIGH);
    digitalWrite(red_ledPin, LOW);
    digitalWrite(yellow_ledPin, LOW);
}

void yellowBlink(){
    digitalWrite(yellow_ledPin, HIGH);
    digitalWrite(red_ledPin, LOW);
    digitalWrite(orange_ledPin, LOW);
}

This is my code and the serial plotter output when i integrate node-red

#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 ssid[] = "thesis_modem";
const char pass[] = "1234567890";

WiFiClient net;
MQTTClient client;
         
const int sensorPIN = A0;
const int sampleWindow = 50;  
unsigned int sample;
unsigned long lastMillis = 0;

const int red_ledPin =  10;
const int orange_ledPin =  9;
const int yellow_ledPin =  8;

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);
  pinMode(red_ledPin, OUTPUT);
  pinMode(orange_ledPin, OUTPUT);
  pinMode(yellow_ledPin, OUTPUT);
  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();                  
  float peakToPeak = 0;                                   
  unsigned int signalMax = 0;                            
  unsigned int signalMin = 1024;                         
                                                
  while (millis() - startMillis < sampleWindow){
     sample = analogRead(sensorPIN);                    
     if (sample < 1024){
        if (sample > signalMax){
            signalMax = sample;                           
        }
        else if (sample < signalMin){
           signalMin = sample;                           
        }
     }
  }
 
  peakToPeak = signalMax - signalMin;               

   float db = map(peakToPeak,20, 90,30,90);
  
      String dB_mqtt =  String(db, 2);
      client.publish("mqtt_table_1", dB_mqtt);
    
   Serial.print("db: "); Serial.println(dB_mqtt);

  if (db <= 30){
    yellowBlink();
  }
  else if ((db > 30) && (db <= 35)){
    orangeBlink();
  }
  else if (db > 35){
    redBlink();
  }
   
}
void redBlink(){
    digitalWrite(red_ledPin, HIGH);
    digitalWrite(orange_ledPin, LOW);
    digitalWrite(yellow_ledPin, LOW);
}

void orangeBlink(){
    digitalWrite(orange_ledPin, HIGH);
    digitalWrite(red_ledPin, LOW);
    digitalWrite(yellow_ledPin, LOW);
}

void yellowBlink(){
    digitalWrite(yellow_ledPin, HIGH);
    digitalWrite(red_ledPin, LOW);
    digitalWrite(orange_ledPin, LOW);
}

Did i miss something here? I wonder why they are both different output and what could be the solution? Because the 1st one is more accurate

(post deleted by author)

In the first you have

int db = map(peakToPeak,20,900,30,90);

And then in the second there is

float db = map(peakToPeak,20, 90,30,90);

You seem to be scaling your values using a different range,

1 Like