Servo Motor + LED Question.

I have a question concerning a project I've been working on. I have a Servo motor starting at angle 0 then delaying one second and going to angle 160, delaying one second and going back to 0. What I want to do is once it reaches 160 degrees to turn on a LED. And when it goes back down to 0, turn off the led. This is what I have so far.

#include <Servo.h>

Servo myServo;
int led = 3;

int pos = 0;

void setup()
{
myServo.attach(9);
pinMode(3, OUTPUT);

}
void loop()
{

digitalWrite(3, HIGH);
myServo.write(0);
delay(1000);
myServo.write(160);
delay(1000);
myServo.write(0);
delay(1000);

}

--

Any help would be appreciated.

Did you connect a led with resistor to pin 3 ?
Turn led on: digitalWrite(3, HIGH);
Turn ledd off: digitalWrite(3, LOW);

Yes. A 220-ohm resistor is connected and it works just fine. I just need to know the code to turn it on and off based on the angle of the motor.

After you set the angle to 160 degrees (and before the delay) turn the led on.
And after you set the angle back to 0 degrees, turn the led off.
Can you do that ? Give it a try.
You can copy-paste your sketch between code tags, use the '#'-button (above the text field when you type a reply).

I am not good at programming, but hope this works :::

#include <Servo.h>

Servo myServo; 
int led = 3;

int pos = 0;

void setup()
{
  myServo.attach(9);
  pinMode(3, OUTPUT);
  
}
void loop()
{
  myServo.write(0);
  delay(1000);
  myServo.write(160);
 digitalWrite(led, HIGH);
  delay(1000);
  myServo.write(0);
  digitalWrite(led, LOW);
  delay(1000); 

}

If the led lights up before completing the rotation, note the time it takes to complete the rotation and add that much delay before digital writing led.
Please copy-paste your sketch between code tags, use the '#'-button (above the text field when you type a reply).

To synchronize the code to the rotation you'll need to step the servo a bit at a time, or
to estimate the seek-time of the servo.

So stepping in synch:

  for (int i = 0 ; i <= 160 ; i++)
  {
    servo.write (i) ;
    delay (2) ;
  }
  digitalWrite (LED, HIGH) ;
  delay (1000) ;
  for (int i = 160 ; i >= 0 ; i--)
  {
    servo.write (i) ;
    delay (2) ;
  }
  digitalWrite (LED, LOW) ;
  delay (1000) ;

or estimating:

  servo.write (160) ;
  delay (200) ;
  digitalWrite (LED, HIGH) ;
  delay (1000) ;
  servo.write (0) ;
  delay (200) ;
  digitalWrite (LED, LOW) ;
  delay (1000) ;

Frenzy654 and MarkT answered my question, thank you.