Tow ultrasonic

hello iam trying this code withe tow ultra sonic
but it.s not working any help ?



// defines pins numbers
const int trigPin = 13;
const int echoPin = 14;
const int trigPinn = 12;
const int echoPinn = 15;
const int threshold = 50;

// defines variables
long duration;
long duration1;
int distance;
int distance1;
float h;


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";
BlynkTimer timer;


void setup() {
pinMode(14, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(trigPinn, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPinn, INPUT);
Serial.begin(9600); // Starts the serial communication
Blynk.begin(auth, "am", "00009999", "blynk-cloud.com", 8442);

}


int data()
{
 // Clears the trigPin
digitalWrite(trigPin, LOW);
digitalWrite(trigPinn, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
digitalWrite(trigPinn, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
digitalWrite(trigPinn, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
duration1 = pulseIn(echoPinn, HIGH);
// Calculating the distance
distance= duration*0.034/2;
distance1= duration1*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("Distance1: ");
Serial.println(distance1);
delay(10);
 if (distance > threshold){

digitalWrite(14, HIGH);
delayMicroseconds(90000);
digitalWrite(14, LOW);
}

Blynk.virtualWrite(1, distance);
Blynk.virtualWrite(1, distance);
return(distance);


}


void loop()
{
    Blynk.run();
   data();
timer.run();

}

You need to move the call to the data() function out of the void loop, and either call the function with a timer, or when you press a button widget in the app (depending on what it is would hoping to achieve).

Read this for more info:

Pete.

the problem is the second ultrasonic is always give 0 value

Believe me, that isn’t your biggest problem as far as this code is concerned.

Pete.

1 Like

is that possible that the one of ultrasonic work when i just attached the echo pin

Have you read and understood the document I linked to, and realised how this applies to your code, and what you need to do to restructure your code?

Pete.

is that better ?


// defines pins numbers
const int trigPin = 12; //6
const int echoPin = 15; //8
const int trigPin2 = 13;
const int echoPin2 = 14;
// defines variables
long duration;
int distance;
long duration2;
int distance2;

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";

BlynkTimer timer;

void myTimerEvent()
{
 // Clears the trigPin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance2= duration2*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance2: ");
Serial.println(distance2);
delay(10);

Blynk.virtualWrite(2, distance2);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(10);
Blynk.virtualWrite(1, distance);

  }
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
Blynk.begin(auth, "am", "00009999", "blynk-cloud.com", 8442);
timer.setInterval(5000L, myTimerEvent);
}




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

Looks much better.
Not sure about port 8442 for the Blynk cloud server though.

Does it work?

Pete.


it’s worke fine but lookabout how iconnect the tow ultrasonic
just the echo pin from every sensore and its work !

1 Like