#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "************************************";
char ssid[] = "**************";
char pass[] = "************";
#include <Servo.h>.
// Defines Tirg and Echo pins of the Ultrasonic Sensor
const int trigPin = D7;
const int echoPin = D8;
// Variables for the duration and the distance
long duration;
float distance;
float sensorMin;
int y;
Servo myServo; // Creates a servo object for controlling the servo motor
void setup() {
Blynk.begin(auth, ssid, pass);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
myServo.attach(D6); // Defines on which pin is the servo motor attached
}
void loop() {
// rotates the servo motor from 15 to 165 degrees
sensorMin = calculateDistance();
for(int i=15;i<=165;i++){
Blynk.run();
Blynk.virtualWrite(V3,i);
myServo.write(i);
delay(20);
distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
if (distance < sensorMin){
y = i; //hold angle of servo motor
sensorMin = distance;
Blynk.virtualWrite(V1,y);
Blynk.virtualWrite(V4,sensorMin);
Serial.print(i); // Sends the current degree into the Serial Port
Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
Serial.print(y); // Sends the hold degree into the Serial Port
Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
Serial.print(sensorMin); // Sends the distance value into the Serial Port
Serial.print("\n.\n"); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
}
// Repeats the previous lines from 165 to 15 degrees
for(int i=165;i>15;i--){
Blynk.run();
Blynk.virtualWrite(V3,i);
myServo.write(i);
delay(20);
distance = calculateDistance();
if (distance < sensorMin){
y = i; //hold angle of servo motor
sensorMin = distance;
Blynk.virtualWrite(V1,y);
Blynk.virtualWrite(V4,sensorMin);
Serial.print(i); // Sends the current degree into the Serial Port
Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
Serial.print(y); // Sends the hold degree into the Serial Port
Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
Serial.print(sensorMin); // Sends the distance value into the Serial Port
Serial.print("\n.\n"); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
}
}
// Function for calculating the distance measured by the Ultrasonic sensor
float calculateDistance(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
return distance;
}
@11147 welcome.
Don’t post unformatted code or your token.
Check PUSH_DATA example to see how loop() should be structured when Blynking.