SRF04 Speed

Hi everyone. Newbie here with a coding question regarding the SRF04 Sensor. I have modified the code below to operate a servo motor when the SRF04 sensor detects an object quite close to it. My question is this.... the sensor seems to take a bit too long to read and operate the servo motor. Is there anyway I can speed up the sensor to read and react quicker? Im not sure how to proceed. Here is the code.


#include <NewPing.h> //Library for Ultrasonic Sensors
#include <Servo.h> //Library for Servo
#define trigPin 6 //Set Trigger HCSR04 in digital Pin 6
#define echoPin 7 //Set Echo HCSR04 in digital Pin 7
#define MAX_DISTANCE 25 //Set the maximum distance
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
Servo myservo; //Create object 1 servo motors
int angle = 90;

void setup() {
Serial.begin (115200); //Speed ​​serial communication
pinMode(trigPin, OUTPUT); //Set Trigger pin as an output
pinMode(echoPin, INPUT); //Set Echo pin as input
myservo.attach(9); //Set PWM servo on pin 9 // basic setup for this section. Once executes it runs only once and then done
}

void loop() { // the loop section ruins over and over forever:
int duration , distance , position = 0 , i;
digitalWrite(trigPin, LOW);
delayMicroseconds(1);
digitalWrite(trigPin, HIGH);
delayMicroseconds(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = ( duration / 2 ) / 29.1 ;
Serial.print(distance);
Serial.println(" cm");

if (distance <=15) // Distance (Cm) you can customize

for(angle = 90; angle < 180; angle += 1) // command to move from 1 degrees to 90 degrees and 1 equals speed
{
myservo.write(angle); //command to rotate the servo to the specified angle
delay(25); //controls opening speed
}

delay(2000); // controls closing delay time

for(angle = 180; angle > 90; angle -= 1) // command to move from 90 degrees to 1 degrees and 1 equals speed
{
myservo.write(90); //command to rotate the servo to the specified angle
delay(10);
}

delay(10);
}

the sensor seems to take a bit too long to read

How long does it take ?

it takes approximately 2-3 seconds to react.

  delay(2000);    When will this delay() occur ?
Is it only when distance <= 15 or will it occur each time through loop() ?
I suggest that you put braces around the code to be executed when distance <= 15 to make it clear which code depends on it.

You seem to have defined an object named sonar using the NewPing library but don't use it in your program. Have you looked at the NewPing examples ? Is there a reason for this ?

The delay(2000) will occur after the servo has turned. It waits for 2 seconds then goes back to its usual position.
I want it to happen within a distance of 15 cm or less, not more.

I actually used the code from a person who supplied me with it. What I did was modify it slightly so when an object appears in front of the SRF04 Sensor, it activates the Servo. The servo waits for 2 seconds (2000) then returns to its usual position.

Then it repeats again if the sonar detects an other object. I want to speed up the sonar detecting an object to be quicker in controlling my servo. I know how to control the servo speed but unsure of the sensor speed.

The delay(2000) will occur after the servo has turned.

That is not what the code does.
Currently it turns the servo if distance <= 15
Always waits 2 seconds then always turns the servo back to its start position even if is already there

Try this

if (distance <= 15) // Distance (Cm) you can customize
{
  for (angle = 90; angle < 180; angle += 1)   // command to move from 1 degrees to 90 degrees and 1 equals speed
  {
    myservo.write(angle);                 //command to rotate the servo to the specified angle
    delay(25);                           //controls opening speed
  }

  delay(2000);                        // controls closing delay time

  for (angle = 180; angle > 90; angle -= 1)    // command to move from 90 degrees to 1 degrees and 1 equals speed
  {
    myservo.write(90);              //command to rotate the servo to the specified angle
    delay(10);
  }
}

thank you very much. I will replace the code with yours. I appreciate your help. thanks.

Do you understand why my code works and yours doesn't ?

Not yet but once I replace the code and go through it I'm sure I will. I believe at first glance its the squirrly brackets that make the difference.

Hmmm. didn't seem to work. After compiling I get an error message reading....... expected '}' at end of input.
My original code seems to work but if I place my hand in front of the sensor it takes about 2 seconds to react. If I take my hand away before the 2 seconds then the sensor does not react in time, hence the reason for a quicker sensor response.

I have uploaded a short video if you want to see my code in action with the servo and SRF04 and how slow it is to react.

Link: - YouTube

Hmmm. didn't seem to work. After compiling I get an error message reading....... expected '}' at end of input.

How much of your program did you replace with mine ?

The part that I posted was intended to replace your if test section.

For completeness here is the complete modified program

#include <NewPing.h> //Library for Ultrasonic Sensors
#include <Servo.h> //Library for Servo
#define trigPin 6 //Set Trigger HCSR04 in digital Pin 6
#define echoPin 7 //Set Echo HCSR04 in digital Pin 7
#define MAX_DISTANCE 25 //Set the maximum distance
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
Servo myservo; //Create object 1 servo motors
int angle = 90;

void setup() {
  Serial.begin (115200); //Speed ​​serial communication
  pinMode(trigPin, OUTPUT); //Set Trigger pin as an output
  pinMode(echoPin, INPUT); //Set Echo pin as input
  myservo.attach(9); //Set PWM servo on pin 9            // basic setup for this section. Once executes it runs only once and then done
}

void loop() {                                           // the loop section ruins over and over forever:
  int duration , distance , position = 0 , i;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(1);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = ( duration / 2 ) / 29.1 ;
  Serial.print(distance);
  Serial.println(" cm");

  if (distance <= 15) // Distance (Cm) you can customize
  {
    for (angle = 90; angle < 180; angle += 1)   // command to move from 1 degrees to 90 degrees and 1 equals speed
    {
      myservo.write(angle);                 //command to rotate the servo to the specified angle
      delay(25);                           //controls opening speed
    }

    delay(2000);                        // controls closing delay time

    for (angle = 180; angle > 90; angle -= 1)    // command to move from 90 degrees to 1 degrees and 1 equals speed
    {
      myservo.write(90);              //command to rotate the servo to the specified angle
      delay(10);
    }
  }
  delay(10);
}

Note my use of code tags to make copying the code easier.

Oh my oh my. I will have to go through your code with a fine tooth comb. The sensor reacts immediately when my hand goes in front of it.

Thank you so very much. You're response and help are truly second to none. I'm actually quite impressed with your response time and your dedication to help. Thank you so very much.

I will have to go through your code with a fine tooth comb

Thanks for your thanks but all I did was to add an opening and closing brace to the if clause, which is what I suggested you do in reply #3

Now, what about the NewPing library ?

I'm not quite sure about the NewPing Library except that it was included in the code when the person emailed it to me.

All you are doing with the library is to #include it in your code and creating an object named sonar ready yo use it, which you never do. Have a look at the NewPing example in the IDE to see how to use it.

kidokwan:
delay(2000); // controls closing delay time

If you block the program execution for 2 seconds , that's exactly what happens:
delay(2000); // stop program execution for 2 seconds and wait until time has passed while doing nothing

Fast reacting programs never use any blocking commands like delay()