Hi there, I am new to Arduino and absolutely love Blynk. Currently, I am trying to get distance values from HC-SR04 connected via USB connection but all I am getting in the app is value “0”. Can you please help me with my code? Thanks
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT SwSerial
#define TRIGGER 10
#define ECHO 9
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
char auth[] = "1a161c329879438e879da1362477552b";
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V5)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// process received value
}
void sendSensor()
{
long duration, distance;
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
duration = pulseIn(ECHO, HIGH);
distance = (duration/2) / 29.1;
Blynk.virtualWrite(V5, distance);
}
void setup()
{
// Debug console
SwSerial.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}