How to stop servo when ultrasonic sensor detects

#include <Servo.h>

Servo myservo;

int pos = 0;

boolean found = false;

const int redLED = 13;
const int greenLED = 12;
const int sen = 7;



void setup() {
  //Startup
  Serial.begin(9600);
  myservo.attach(9);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

}

void loop() {
  
  long duration, inches, cm;

  pinMode(sen, OUTPUT);
  digitalWrite(sen, LOW);
  delayMicroseconds(2);
  digitalWrite(sen, HIGH);
  delayMicroseconds(5);
  digitalWrite(sen, LOW);
  
  pinMode(sen, INPUT);
  duration = pulseIn(sen, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
 /* do {
    for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  } while (inches > 50);*/

  if (inches >=50) {
    found = false;
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
     for(pos = 0; pos <= 180; pos += 1){
    if (inches >=50) {
      myservo.write(pos);
    delay(15);
    }else {
      break;
      int serVal = myservo.read();
      myservo.write(serVal);
    }
  } 
  for(pos = 180; pos>=0; pos-=1)
  {                                
    if (inches < 50) {
    myservo.write(pos);
    delay(15); 
  }else {
      break;
      int serVal = myservo.read();
      myservo.write(serVal);
    }
  } 
    Serial.println(inches);
  } else if (inches < 50) {
    found = true;
    Serial.println(inches);
    myservo.write(pos);
    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW);
    tone(8, 262);
    delay(1000);
    noTone(8);
    delay(1000);
    tone(8, 392);
    delay(1000);
    noTone(8);
  }
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

I have no clue what im doing wrong, but whenever I put my hand in front of the sensors it waits until the full sweep takes place and then the green led lights up and detects me. I want it so that at any moment when i place my hand in front it stops the servo where it is and the green led comes on.

Thanks. Your help is appreciated.

Well, Just wondering what kind of ultrasonic sensor you are using.

  if (inches >=50) {
    found = false;
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
    for(pos = 0; pos <= 180; pos += 1){
      if (inches >=50) {

"inches" doesn't change there, so yes it will keep turning. You need to re-read it. eg.

    for(pos = 0; pos <= 180; pos += 1){
      duration = pulseIn(sen, HIGH);
      inches = microsecondsToInches(duration);
      if (inches >=50) {

Ditto further down.

Could you edit my code for me? Im getting a bunch of errors if I do it. :confused:

You do it. Post the edited code. Post the errors.

#include <Servo.h>

Servo myservo;

int pos = 0;

boolean found = false;

const int redLED = 13;
const int greenLED = 12;
const int sen = 7;



void setup() {
  //Startup
  Serial.begin(9600);
  myservo.attach(9);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

}

void loop() {
  
  long duration, inches, cm;

  pinMode(sen, OUTPUT);
  digitalWrite(sen, LOW);
  delayMicroseconds(2);
  digitalWrite(sen, HIGH);
  delayMicroseconds(5);
  digitalWrite(sen, LOW);
  
  pinMode(sen, INPUT);
  duration = pulseIn(sen, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
 /* do {
    for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  } while (inches > 50);*/

  
    for(pos = 0; pos <= 180; pos += 1){
      duration = pulseIn(sen, HIGH);
      inches = microsecondsToInches(duration);
      if (inches >=50) {
      myservo.write(pos);
    delay(15);
    }else {
      break;
      int serVal = myservo.read();
      myservo.write(serVal);
    }
  } 
  
    for(pos = 0; pos <= 180; pos += 1){
      duration = pulseIn(sen, HIGH);
      inches = microsecondsToInches(duration);
      if (inches >= 50) {
    myservo.write(pos);
    delay(15); 
  }else {
      int serVal = myservo.read();
      myservo.write(serVal);
    }
  } 
    Serial.println(inches);
  } else if (inches < 50) {
    found = true;
    Serial.println(inches);
    myservo.write(pos);
    digitalWrite(greenLED, HIGH);
    digitalWrite(redLED, LOW);
    tone(8, 262);
    delay(1000);
    noTone(8);
    delay(1000);
    tone(8, 392);
    delay(1000);
    noTone(8);
  }
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Error V

Automatic_Doorbell.ino:81:5: error: expected unqualified-id before 'else'
Automatic_Doorbell.ino:96:8: error: expected constructor, destructor, or type conversion before '(' token
Automatic_Doorbell.ino:97:1: error: expected declaration before '}' token
Error compiling.

    Serial.println(inches);
  } else if (inches < 50) {   <<< the } here closes out loop()

So you have a logic flaw with your if-else or if-else if-else it statements.

If you the put the cursor to the right of any }, then use the scroll bar to move back in the sketch,
you can find the mating {

If I take your first post, and paste in those two lines in the two places I would expect you to, it compiles. You have changed something else.

I ran your two files through "diff" - you changed a lot of stuff:

*** a 2015-06-30 14:27:29.498677500 +1000
--- b 2015-06-30 14:27:52.850677450 +1000
***************
*** 1,3 ****
--- 1,5 ----
+ 
+ 
  #include <Servo.h>
  
  Servo myservo;
***************
*** 51,62 ****
    }
    } while (inches > 50);*/
  
!   if (inches >=50) {
!     found = false;
!     digitalWrite(redLED, HIGH);
!     digitalWrite(greenLED, LOW);
!      for(pos = 0; pos <= 180; pos += 1){
!     if (inches >=50) {
        myservo.write(pos);
      delay(15);
      }else {
--- 53,63 ----
    }
    } while (inches > 50);*/
  
! 
!     for(pos = 0; pos <= 180; pos += 1){
!       duration = pulseIn(sen, HIGH);
!       inches = microsecondsToInches(duration);
!       if (inches >=50) {
        myservo.write(pos);
      delay(15);
      }else {
***************
*** 65,77 ****
        myservo.write(serVal);
      }
    }
!   for(pos = 180; pos>=0; pos-=1)
!   {
!     if (inches < 50) {
      myservo.write(pos);
      delay(15);
    }else {
-       break;
        int serVal = myservo.read();
        myservo.write(serVal);
      }
--- 66,79 ----
        myservo.write(serVal);
      }
    }
! 
!     for(pos = 0; pos <= 180; pos += 1){
!       duration = pulseIn(sen, HIGH);
!       inches = microsecondsToInches(duration);
!       if (inches >= 50) {
      myservo.write(pos);
      delay(15);
    }else {
        int serVal = myservo.read();
        myservo.write(serVal);
      }
***************
*** 104,106 ****
--- 106,109 ----
  {
    return microseconds / 29 / 2;
  }
+