[SOLVED] Servo does not work with this code

when i use this code with the ultrasonic sensors , there is no feedback from the servos. I used a mg995 servo and two HC-SR04 ultrasonic sensors .Servo works with the servo example blynk skectch.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Servo.h>

char auth[] = "my auth";

Servo servo;

#define echoPin 8 // Echo Pin
#define trigPin 9
#define echoPin1 7
#define trigPin1 6// Trigger Pin

long duration, distance, d, dp;
long duration1, distance2, d1, dp1; // Duration used to calculate distance

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(3, OUTPUT);
  servo.attach(5);
}

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(trigPin1, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);


  //Calculate the distance (in cm) based on the speed of sound.
  distance = duration / 58.2;
  d = 35 - distance;
  dp = 2.86 * d;
  distance2 = duration1 / 58.2;
  d1 = 35 - distance2;
  dp1 = 2.86 * d1;

  Serial.println(dp);
  Serial.println(dp1);


  delay(500);
  Blynk.run();
}

BLYNK_READ(V8)
{
  Blynk.virtualWrite(8, dp);
}
BLYNK_READ(V9)
{
  Blynk.virtualWrite(9, dp1);
}
BLYNK_WRITE(V3)
{
  servo.write(param.asInt());
}

Try to remove the delay(500) and use the SimpleTimer instead.

Thanks , But how to include the simple timer ,Im new to coding with this library .

Please look at the Blynk PushData example. It has an example of how to use the library :slight_smile:

Thanks got it working
:slight_smile:

Also please don’t use pulseIn as it is bad blocking way of using HC-SR04 (not your fault since all “tutorials” show this unfortunately) use interrupts instead, it will also make Blynk working smoother.