Sharp IR and Servo

Hi everyone, i'm hoping someone can help me out with this code
and any help will be greatly appreciated.

So basically I want the servo to turn 180 degrees when the sensor comes in contact
at a range below 50 cm and stay there for 2 seconds, then return to 0 degrees.
When I put my hand over the beam it goes 180 degrees but I have to hold it there,
is there any way to have it rotate 180 degrees with out having to hold your hand
over the sensor once it detects the required position?

#include <Servo.h>

Servo myservo1; //first servo

int SHARP_IR_PIN = 1; //analog pin used to connect the sensor
int ledPin = 9;  //select pin for the servo
int val = 0;  //variable to read the value from the analog pin
int adc_to_cm(int adc) {
  return 14251 * pow(adc,-1.17);
}

void setup()
{
  myservo1.attach(9); //set up the servo as usual first servo
    pinMode(ledPin, OUTPUT);  //variable to store the value coming from the sensor
  Serial.begin(9600);
}

void loop() {
  
  int reading = analogRead(SHARP_IR_PIN); // replace this pin with the analog pin your sensor is connected to
  int distance = adc_to_cm(reading);

  char buf[50];
  sprintf(buf, "Distance: %d, voltage: %.02f, adc: %d\r\n", distance, reading * 5.00 / 1024 , reading);
  Serial.print(buf);
  
  if (distance < 40) {
    myservo1.write(0);
  } else {
    myservo1.write(180);
  }
    
delay(1);

}

Add a 2 second delay in the else condition, after moving to the 180 deg position.

is there any way to have it rotate 180 degrees with out having to hold your hand
over the sensor once it detects the required position?

Put while (1); immediately after the servo write.

Why have you got a 1 millisecond delay at the end of your loop?

Why have you got a 1 millisecond delay at the end of your loop?

by accident I was looking for similar projects to mine and I tweaked the code and missed the delay

So I tried the code and now the servo stays at 180 degrees it doesn't return back to 0 after 2 seconds

#include <Servo.h>

Servo myservo1; //first servo

int SHARP_IR_PIN = 1; //analog pin used to connect the sensor
int ledPin = 9;  //select pin for the servo
int val = 0;  //variable to read the value from the analog pin
int adc_to_cm(int adc) {
  return 14251 * pow(adc,-1.17);
}

void setup()
{
  myservo1.attach(9); //set up the servo as usual first servo
    pinMode(ledPin, OUTPUT);  //variable to store the value coming from the sensor
  Serial.begin(9600);
}

void loop() {
  
  int reading = analogRead(SHARP_IR_PIN); // replace this pin with the analog pin your sensor is connected to
  int distance = adc_to_cm(reading);

  char buf[50];
  sprintf(buf, "Distance: %d, voltage: %.02f, adc: %d\r\n", distance, reading * 5.00 / 1024 , reading);
  Serial.print(buf);
  
  if (distance < 40) {
    myservo1.write(0);
    while (1);
          } 
      else {
    myservo1.write(180);
    delay(1500);
    }
    
}

Sorry, I read "make it stay" but missed the "for two seconds", in which case a "delay (2000);" instead of the "while" would work, but be aware that nothing else will happen in those two seconds.

Sweet it works you guys are awesome!!!!!!!!!! :smiley:

So say if I wanted it to detect the required position then wait until it's out of position
then trigger the servo how would that look?

How do you detect the position? Can you elaborate that a bit?

well at first I followed this but I ended up finding someone that
used a slightly different method for a project very similar to mine so I disregarded this website but it could be useful for you
http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/