Can someone help me? How can I stop a servo motor? I'm new at Arduino Programming Language

#include<Servo.h>
Servo motor;
void setup() {
  motor.attach(3);
}
void loop() {  
OpenDoor();
CloseDoor();
delay(5000);
}
void OpenDoor(){
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  motor.write(9);
  delay(500);
  motor.write(20);
  delay(500);
  motor.write(30);
  delay(500);
   motor.write(40);
  delay(500);
    motor.write(50);
  delay(500);
  motor.write(60);
  delay(500);
  motor.write(70);
  delay(500);
  motor.write(80);
  delay(500);
  motor.write(90);
  delay(500);
  motor.write(100);
  delay(500);
    motor.write(110);
  delay(500);
    motor.write(120);
  delay(500);
  motor.write(130);
  delay(500);

  digitalWrite(4,HIGH);
  digitalWrite(5,LOW);
  delay(5000);
}
void CloseDoor(){
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  motor.write(130);
  delay(500);
  motor.write(120);
  delay(500);
  motor.write(110);
  delay(500);
  motor.write(100);
  delay(500);
  motor.write(90);
  delay(500);
  motor.write(80);
  delay(500);
  motor.write(70);
  delay(500);
  motor.write(60);
  delay(500);
  motor.write(50);
  delay(500);
  motor.write(40);
  delay(500);
  motor.write(30);
  delay(500);
  motor.write(20);
  delay(500);
  motor.write(10);
  delay(500);
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  }

Two days ago I started to use Arduino for a programming proyect... I tried to open and close a door with a micro servo motor and with leds to indicate the door open or close, but it continues open, close and it doesn't stop. What should I do?

Your question is very general. A very generalised answer to your question "What should I do? " is "change the porgram"
But I guess that doesn't help at all.

You are working on an informatic project. And what is most needed on an informatic project is information.

More information in various ways:

more information about the wanted functionality:

Do you want to have the program to continually open/close the door?
or do you want to have opening / closing initiated by different conditions?

  • what (kind of information) should open the door?

  • a button-press?

  • a certain amout of time that has passed by?

  • ....?

  • what (kind of information) should close the door?

  • a button-press?

  • a certain amout of time that has passed by?

  • ....?

is just mentioning a point of time. Not how long you worked on it.
For the next thing I write I assume you read just a little bit about programming and then started writing code and it took you 6 to 8 hours to come this far by tinkering.

Well if you take 2-3 hours to read about basic programming-techniques - that have nothing to do directly with your project then

absolutely regardless of what your project is:
this bigger knowledge about programming in genereal will speed up finishing your project

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Just I want to open and close the door (x1) And then stop. Yes I need to read more about this Language. I will being reading a lot about... i'm so inexperiencied. Thank you for your time also for your answer. Thanks, i hope to have the enough knowledge to program before I have started the course Thanks!!! :grin:

Chapter one of the tutorial gives the answer how to do that:

Read the chapter.

Take a close look where is the code placed to do the action only one single time

I hope this demonstrates you

STOP

tinkering around in your servo-code

Start

reading.

You have to collect more knowledge first

best regards Stefan

Thank you!!

Just move the end of setup() and the start of loop() down a couple of lines.

void setup() {
  motor.attach(3);
  OpenDoor();
  CloseDoor();
}
void loop() {  
  delay(5000);
}

1. Think in the following way (Fig-1) being inspired y Post-2 @StefanL38 :
(1) Press the Open button, the servo will move/turn by 60 degrees to open the door.
(2) Press the Close button, the servo will move/turn by 0 degree to close the door.
ServoDoor
Figure-1:

2. The codes (not sketch) are given below. Place these codes under global space, setup() function, loop() function, and if() statement as needed. After that, upload the sketch and check that the servo works accordingly. Initially, the door will remain in close position. You have to wait for 5-sec to close the door once it is opened and vice versa.

pinMode(4, INPUT_PULLUP);
delay(5000);
myServo.write(0);
pinMode(6, OUTPUT);
#include<Servo.h>
pinMode(7, OUTPUT);
pinMode(5, INPUT_PULLUP);
digitalWrite(6, LOW);
if(digitalRead(5) == LOW)
{
      myServo.write(0);
}
myServo.attach(3);
digitalWrite(7, LOW);
Servo myServo;
void loop()
{

}
if(digitalRead(4) == LOW)
{
      myServo.write(60);
}
Serial.begin(9600);
digitalWrite(7, HIGH);
void setup()
{

}
delay(5000);
digitalWrite(6, HIGH);