# Did i miss something here? Help

**URL:** <https://discourse.nodered.org/t/did-i-miss-something-here-help/54968>\
**Category:** General\
**Created:** [9 December 2021 23:22 UTC](https://discourse.nodered.org/t/did-i-miss-something-here-help/54968 "2021-12-09T23:22:12Z")\
**Posts on this page:** 4\
**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:** [9 December 2021 23:22 UTC](https://discourse.nodered.org/t/did-i-miss-something-here-help/54968/1 "2021-12-09T23:22:12Z")

</div>

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.

```auto

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

```

 ![noprob](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db1809ee2282124076222f7e6eb9408f3bab1feb.png)

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

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

```

 ![prob](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30df47c32bd819e1ecfbe8bf23ad500fb20a447e.png)

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 December 2021 00:12 UTC](https://discourse.nodered.org/t/did-i-miss-something-here-help/54968/2 "2021-12-10T00:12:58Z")

</div>

(post deleted by author)

---

<div class="post-metadata">

**Author:** ![M-a-r-t-i-n](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/m-a-r-t-i-n/32/52391_2.png) [@M-a-r-t-i-n](https://discourse.nodered.org/u/M-a-r-t-i-n)\
**Post date:** [10 December 2021 05:37 UTC](https://discourse.nodered.org/t/did-i-miss-something-here-help/54968/3 "2021-12-10T05:37:45Z")

</div>

> [@jhunnx](#):
>
> `float db = map(peakToPeak,20, 90,30,90);`

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,

---

<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:** [8 February 2022 05:38 UTC](https://discourse.nodered.org/t/did-i-miss-something-here-help/54968/4 "2022-02-08T05:38:16Z")

</div>

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