Can´t get the distance with blynk app using a ultrasonic sensor and arduino uno

Hi again, I can’t read the distance in the blynk app of my ultrasonic sensor. I’m using an Arduino UNO and an HC-RS04 ultrasonic sensor. This is my code:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <SPI.h>
    #include <Ethernet.h>
    #include <BlynkSimpleEthernet.h>
    #include <SimpleTimer.h>

    char auth[] = "c046b3f28da144598e5119d4d2cfb74d";

    SimpleTimer timer;

    #define echoPin 3 // Echo Pin
    #define trigPin 5 // Trigger Pin

    long duration, distance; // Duration used to calculate distance
     
    void MeasureCm()
    {
      /* The following trigPin/echoPin cycle is used to determine the
     distance of the nearest object by bouncing soundwaves off of it. */ 

     
     digitalWrite(trigPin, LOW); 
     delayMicroseconds(2); 

     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10); 
     
     digitalWrite(trigPin, LOW);
     duration = pulseIn(echoPin, HIGH);
     
     //Calculate the distance (in cm) based on the speed of sound.
     distance = duration/58.2;

     Serial.print("Distance: ");
     Serial.println(distance);    
    }
      
    void setup() {
     Serial.begin(9600);
     Blynk.begin(auth);
      
     pinMode(trigPin, OUTPUT);
     pinMode(echoPin, INPUT);
     
     timer.setInterval(1000, MeasureCm);
     }

     BLYNK_WRITE(V8)
     {
     Blynk.virtualWrite(8,distance);// virtualpin 8 distance
     }

     
    void loop() {
      Blynk.run(); // Initiates Blynk
      timer.run(); // Initiates SimpleTimer
    }

I read the distance in the serial monitor but in the app. I’m using a V level widget with V8 port in push. Somebody help me please.

So when the distance measurement has been taken, how is the function that pushes the result upto the Blynk server being called?

When you’ve figured this out, you might want to think about using a Blynk button widget to trigger the measurement process, rather than constantly taking measurements and blasting the data at the Blynk server.

Pete.

I’d thinking that this:

BLYNK_WRITE(V8)
 {
 Blynk.virtualWrite(8,distance);// virtualpin 8 distance
 }

This is not the function to push data from arduino upto Blynk server? judding your answer i’m now thinking mabe not :slight_smile: I understand that if I send to much data upto the server my Arduino disconect from the server and the flood error appears. So I use the timer function to take measure every second and avoid the error. I’m right?

Is a distance measurement every second really necessary? I don’t think it will cause any disconnect errors, but more that it was just a bit over kill.

BLYNK_WRITE (Vx)

is the function that gets called when you do something in the APP, for example push the button widget.

You need move this to the end of your MeasureCm function.

Blynk.virtualWrite(8,distance);// virtualpin 8 distance

It is, if the Widget calls for it as it would if it was a Button or Slider… typically set for PUSH.

Normally you would use a function like this, but periodically called by a timer

myDistance()
 {
 Blynk.virtualWrite(8,distance);// virtualpin 8 distance
 }

However, using this command with a display set for a timed frequency, so that the App does the timing, will work as well

BLYNK_READ(V8)
 {
 Blynk.virtualWrite(8,distance);// virtualpin 8 distance
 }

Thanks Pete, Toro & Gunner. Issue solved.

Hi, if im using this with mega in proteus, would it be the same code? do you know? i cant seem to make it work