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

Printing distance on lcd-screen using HC-SR04

Hi blynkers!
So i am trying to print the distance in cm using the HC-SR04 on the lcd screen in blynk. I am using the NewPing library, and Smoothing to provide a reliable output from the sensor. However i run into some problem. I don´t realy know how to print a reliable looping value onto the lcd screen. I guess i need to use virtual pins some how, but doesen´t really know how to do it. I have looked over the provided examples, but couldn´t find anything useful for this project.

I succesfully printed the second word “cm” but not the string where the distance data is. It only appear a random number which is not updating. Anyone having a suggestion on what to do? :confused:

//distance measuring printed to blynk lcd screen

//blynk over usb definitions
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>

//newping library definitions
#include <NewPing.h>
#define TRIGGER_PIN  4         // trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2        // echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200      // Maximum distance in centimeter.

char auth[] = "2f68e88a71234776b8ca52b4678238ec";

WidgetLCD lcd(V1);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

//variables used in smothing the values from distance sensor
const int numReadings = 50;     //number of readings
String distance;                 //make average to string later

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                //the average value


void setup() {
  SwSerial.begin(9600);
  Blynk.begin(auth);

// initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
  
while (Blynk.connect() == false) {
    // Wait until connected
  }

}

void code{    //smoothing values from distance sensor

// subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = sonar.ping_cm();
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  distance = average;  // convert to string

  //print to lcd
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(4, 0, distance); //print the average distance
  lcd.print(4, 1, "cm");
  
  delay(30);    
  
}

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

Have a look here:

Thank you very much @psoro, this helped a lot :wink:

1 Like