Two Ultrasonic sensor 1 is not giving correct readings

HI! please Help me
I made a program that reads 2 different distances so i use 2 ultrasonic sensor and blynk app and my problem is the other sensor it's giving an exact distance it's always moving but the other is good. i dont know wahts the problem with it

here is my code:

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION "0.1.0"

#define BLYNK_PRINT Serial

#define APP_DEBUG
#define trig2 D1
#define echo2 D2

#define trig D5
#define echo D6
#include "BlynkEdgent.h"

BlynkTimer timer;
long tankDepth=24,tankDepth2 = 24;

//Ulrasonic sensor for inside

void waterLevel2(){
digitalWrite(trig2,LOW);
delayMicroseconds(2);
digitalWrite(trig2, HIGH);
delayMicroseconds(10);
digitalWrite(trig2, LOW);
long t1 = pulseIn(echo2, HIGH);
long cm1 = t1 / 29 / 2;
Serial.println(cm1);
double level1= tankDepth2-cm1;
if(level1>0){
long percentage2=((level1/tankDepth2))*100;
Blynk.virtualWrite(V1,percentage2);
}
else{
Blynk.virtualWrite(V1,0);
}

}

//Ulrasonic sensor for outside
void waterLevel(){
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long t = pulseIn(echo, HIGH);
long cm = t / 29 / 2;
Serial.println(cm);
double level= tankDepth-cm;
if(level>0){
long percentage=((level/tankDepth))*100;
Blynk.virtualWrite(V0,percentage);
}
else{
Blynk.virtualWrite(V0,0);
}

}

void setup()
{
pinMode(trig, OUTPUT);
pinMode(trig2, OUTPUT);
pinMode(echo, INPUT);
pinMode(echo2, INPUT);
Serial.begin(115200);
delay(100);

BlynkEdgent.begin();
timer.setInterval(10L, waterLevel2);
timer.setInterval(10L, waterLevel);
}

void loop() {
BlynkEdgent.run();
timer.run();
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

Are you using a serial pin for your trig2?

I use digital pin for both trig sir

On many Arduinos, D1 is used by the serial port. Which one are you using?

Try using another pin. I think serial may be causing you issues

I am using ESP8266

Okay sir I'll try, thank you.

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