Hi. I'm doing a project to read data value from ultrasonic sensor using espresso lite v2.0. And, I also link it to Blynk (LCD Settings). But, I keep getting 0 value in my reading. Can you tell what is wrong with my connection or programming? Here is my code. Thank you.
- CODE: SELECT_ALL_CODE
/*******************************************************************************
Cytron IoT Workshop
This example demonstrates how to control the ESPresso Lite from iOS
and Anroid using the Blynk platform.
Created by: Cytron Technologies Sdn Bhd.
Website: http://www.cytron.com.my
Email: support@cytron.com.my
*******************************************************************************/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "4110d4dcf7724f3d867ae62136699bfe";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "etot";
char pass[] = "laici kang";
const int trigPin = 12;
const int echoPin = 13;
WidgetLCD lcd(V1);
void setup()
{
pinMode(2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Distance in cm");
}
void loop()
{
lcd.clear();
lcd.print(0, 0, "Distance in cm");
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
lcd.print(7, 1, cm);
Blynk.run();
delay(1000);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}