My code is based on this first code which works mostly fine (sometimes i get wrong values, but still not nice).
#define echoPin D7 // Echo Pin
#define trigPin D6 // Trigger Pin
long duration, distance; // Duration used to calculate distance
void setup()
{
Serial.begin (57600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
/* 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.println(distance);
//Delay x ms before next reading.
delay(2000);
}
But once I integrate the same code with the blynk part, I only get wrong values.
#define BLYNK_PRINT Serial
#define echoPin D7 // Echo Pin
#define trigPin D6 // Trigger Pin
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "5vJfspsKEJXnD9YNgBxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Toxxxx";
char pass[] = "xxxxx";
long duration, distance; // Duration used to calculate distance
BlynkTimer timer;
void setup()
{
Serial.begin (57600);
Blynk.begin(auth, ssid, pass);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
timer.setInterval(1000L, myTimerEvent);
}
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, distance);
}
void loop()
{
/* 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.println(distance);
//Delay x ms before next reading.
delay(2000);
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
It seems to jump in both cases between two values, 115 (the correct one) and 80 (the wrong one).
With the big difference that the first code only has occasional wrong indications, while the second one almost regularly jumps between the two values.....
why is that?
Thanks. Help is muck appreciated!