BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Show values from Ultrasonic sensor and Arduino UNO using Gauge

arduino

#1

I have been trying to display distance measurements using the widget Gauge but I don’t know why it won’t work. Here are the codes I used

#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX    
#include <BlynkSimpleStream.h>
char auth[] = "...";

const int trigPin = 12;
const int echoPin = 13;
const int Buzzer = 6;
const int ledPin= 8;
int duration, distance;
int saveDis = 0;
int sound = 50;
int sensorData = 12;

BlynkTimer timer;

void sendSensor (void) {
    long duration, distance;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    saveDis = distance; 
    distance = (duration/2) / 29.1;

  Serial.println("Distance: " + String(distance) + "cm");
  if (distance >= 300 || distance <= 0) 
        {
        Serial.println("no object detected");
        digitalWrite(Buzzer,LOW);
        digitalWrite(ledPin,LOW);
        noTone (Buzzer);
        }
  else {
        Serial.println("object detected \n");
        Serial.print("distance= ");              
        Serial.print(distance);
        tone(Buzzer,400);
        digitalWrite(ledPin,HIGH);
        
      Blynk.notify("Distance: " + String(saveDis) + "cm to " + String(distance) + "cm!");
       }

   if (isnan(distance)){
   Serial.println("Failed to read from ultrasonic sensor!");
   return;
        }
        
   if(distance<300) {
    Blynk.notify(String("DISTANCE:") + distance);
   }   
   Blynk.virtualWrite(V0,distance);
delay (5);
}

BLYNK_READ(V0)
{
Blynk.virtualWrite(V0,distance);
}

void myTimerEvent(void)
{
  sensorData = digitalRead(13);
  Blynk.virtualWrite(V0, sensorData);
}

void setup(void){
        pinMode(trigPin, OUTPUT); 
        pinMode(echoPin, INPUT);
        pinMode(Buzzer, OUTPUT);
        pinMode(ledPin, OUTPUT);
        SwSerial.begin(9600);
        Serial.begin(9600);
        Blynk.begin(Serial, auth);
        timer.setInterval(1000L, sendSensor);
        Blynk.virtualWrite(V0,distance);
}



void loop(void) {
  Blynk.run();
  timer.run();
  sendSensor();
  int duration, distance;
  distance = (duration/2) / 29.1;
  
  
}

void loop()


#2

Because you didn’t take the time to learn how to properly use BLYNK.


#3

Here is an example of how to properly use an ultrasonic sensor with Blynk…