Help with servo code to make servo run for a few seconds when sensor is tripped

Hello! I am super new to programming, electronics and arduinos! I am trying to figure out the code to run my project but I am stuck. I am trying to run a servo for five seconds when a sensor is tripped and then stop when it is not tripped. Currently, my servo runs when the sensor is tripped but does not stay on for five seconds as I would like (it turns off the instant that I am out of sensor range). What am I missing in my code to make it stay on for five seconds? I have run this program for a LED and it works fine but I am obviously missing something with the servo code. I am having trouble understanding some of the code as I am seeing either really basic code (which I understand) or overly complex code. I am using:
Arduino Duemilanove
Adafruit mshield
GWS S35/STD continuous rotation servo
MaxSonar Sensor

Thanks for any help!

Here is my code:

#include "WProgram.h"
#include "Servo.h"
int servoPin = 9;  //motor on pin 9 
int pin_sensor  =0;  //sensor on analog 0 
int inchesAway;    //inches away from maxsonar sensor 
int pos=0; //variable to store the servo position 
static void refresh();

void setup()
{
pinMode(servoPin,OUTPUT);
digitalWrite(servoPin,HIGH);
delay(5000);
digitalWrite(servoPin, LOW); 

Serial.begin(9600);
}


void loop()
{
inchesAway = analogRead(pin_sensor) /2; 
Serial.print(inchesAway, DEC);
  
  if(inchesAway <24) {  //if something is 24" or less away, motor on 
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(5000);  //stays on for five seconds THIS IS NOT HAPPENING
    digitalWrite(servoPin,LOW);
    delay(20); //20ms
    
  }
  
  else
  {
      Serial.print("OFF");        
      digitalWrite(servoPin, pos);  //when sensor isn't tripped, motor is off
  }
  delayMicroseconds(300);  //checks sensor every .3 seconds for person/object  

}
delayMicroseconds(5000);  //stays on for five seconds THIS IS NOT HAPPENING

5000 microseconds is just 5 milliseconds. use "delay(5000)" like you have in "setup"

Edit: I saw the most obvious error first (you highlighted it after all), but then I noticed that although you're including the servo library, you're not using it, and doing all kinds of strange things with the servo control line. What exactly is connected to pin 9? At some places you call it a "motor" and in others a "servo".

(uncompiled, untested, but with millis/micros corrected)

#include "Servo.h"
const int servoPin    = 9;   //motor on pin 9 
const int sensorPin  = A0; //sensor on analog 0 
const int servoStop  = 90; // may need some adjusting to find stop-point.
const int servoFwd  = 45;  // or 135 for the opposite direction
Servo exServo;

void setup()
{
  exServo.write (servoStop);
  exServo.attach (servoPin);
  Serial.begin(9600);
}

void loop()
{
  int inchesAway = analogRead(sensorPin) /2; 
  Serial.print(inchesAway, DEC);
  
  if(inchesAway < 24) {  //if something is 24" or less away, motor on 
    exServo.write (servoFwd);
    delay(5000);
    exServo.write (servoStop);
  } else {
    Serial.print("OFF");        
    exServo.write(servoStop);
  }
  delay(300);  //checks sensor every .3 seconds for person/object  
}

(the use of “delay” will probably get me ostracised, but I don’t care)

(the use of “delay” will probably get me ostracised, but I don’t care)

Not if you’re aware of the alternatives and impacts, and have chosen to accept the limitations that delay() brings to the party.

In this case, I’d say that delay was acceptable.

Add a second sensor, and another motor/servo/whatever, and delay() would no longer be acceptable.

Thanks so much for your help! It is working now (the motor kind of hums or buzzes gently when the sensor isn’t tripped - I’m going to assume this is normal?). I’m not surprised I messed up the code and wasn’t using the servo library very well. Is there a site/book with some good servo tutorials that I can use (that go beyond the servo ones on the arduino site)? I can’t seem to figure out the library by just looking at what I have on my computer, I guess a lack of experience. Many of the examples I found didn’t use the commands that you’ve written so I’m guessing I’m not finding the right stuff. I have gone through many tutorials with LEDs and have gone through the Getting Started With Arduino book but was really having trouble with servo code. It is a bit overwhelming to be jumping into something that I have no experience with but its super interesting and I am enjoying figuring things out and being able to get help when I can’t. Thanks again, I really appreciate it.

Some simple servo test code you can use to setup your servo. If the servo is buzzing, then it may be against a hard stop which is to be avoided.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}

Thanks! I also changed the code from servoStop to exServo.detach(); (and added exServo.attach (servoPin); into the start of the loop section) and that seems to have stopped the buzzing. I figured the stop just held it in place whereas the detach would actually shut it off. I will also run your code, I need to start understanding the Serial info as well.