# Programming Servo Basics

### Locating the Servo Blocks

Let's start by reviewing how to access servos within Blocks. At the top of the Categorize Blocks section there is a drop down menu for **Actuators**. When the menu is selected it will drop down two choices: **DcMotor** or **Servo**. Selecting Servo will open a side window filled with various servo related blocks.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FY1xEiXkBPA6zlhM8BPaH%2Fimage.png?alt=media&#x26;token=6a65eb1f-4eb3-40fd-87c6-e7e8dd63a843" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The block above will change names depending on the name of the servo in a configuration file. If there are multiple servos in a configuration file the arrow next to test\_servo will drop down a menu of all the servos in a configuration.&#x20;

Different block options will appear when using a continuous rotation servo.
{% endhint %}

### Programming Position Movements

Let's start by programming our servo to rotate to the default 1 position!

From the Servo menu, we will primarily be using the block![](https://files.gitbook.com/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAc8IF4-_ItJzHXisV%2FServo%20-%20set%20to%200.svg?alt=media\&token=e58e949a-a8a7-4917-9427-4e737208328b)

Add this block to the op mode code within the ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FBLTKYInbWgeBhXzuYoLf%2Fimage.png?alt=media\&token=767df4c5-b115-454a-8681-dacd114f36a3) **.**&#x20;

Click on the number block to change from <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAc8IF4-_ItJzHXisV%2FServo%20-%20set%20to%200.svg?alt=media&#x26;token=e58e949a-a8a7-4917-9427-4e737208328b" alt="" data-size="original"> to <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAcLYKFQSC3PM2Cbef%2FServo%20-%20set%20to%201.svg?alt=media&#x26;token=978c25bf-c57e-4e8f-84a0-285fdde44b2d" alt="" data-size="original"> **.**

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FVzobHtm2dmPOW4caTMsd%2Fservocode.PNG?alt=media&#x26;token=8fa793d1-02f3-40ea-9b38-cf4f0e95aa5b" alt=""><figcaption></figcaption></figure>

Select **Save OpMode** in the upper lefthand corner in the programming interface.

### Quick Check!

Let's give our program a try. Take a moment to observe what happens.&#x20;

When running our program for the first time, we should have seen our servo move itself to position 1 and maintain that position. But what happens if we run it again? Does the servo move?

<details>

<summary>Running our program a second time</summary>

Likely, on a second run our servo did not move since it is already at the correct position. Now check what happens if you first manually rotate the servo while the robot is disabled. Once the code is activated again by pressing play we should see it move again!

**Note:** Servos are designed to maintain their position so long as the robot's program is enabled. Trying to forcibly move the servo while ON may damage it and is not recommended.

</details>

{% hint style="info" %}
If your servo did not move as expected, double check your wiring and port are correct compared to your configuration.
{% endhint %}

### Resetting Back to Zero

The intent of the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAcLYKFQSC3PM2Cbef%2FServo%20-%20set%20to%201.svg?alt=media&#x26;token=978c25bf-c57e-4e8f-84a0-285fdde44b2d" alt="" data-size="original"> is to set the position of the servo. If the servo is already in the set position when a code is run, it will not change positions. Lets try adding another <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAc8IF4-_ItJzHXisV%2FServo%20-%20set%20to%200.svg?alt=media&#x26;token=e58e949a-a8a7-4917-9427-4e737208328b" alt="" data-size="original"> block and see what change&#x73;*.*&#x20;

In this case, we do not want our servo to reset to 0 every time our code repeats. Because of this where do you think we would snap in our  <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAc8IF4-_ItJzHXisV%2FServo%20-%20set%20to%200.svg?alt=media&#x26;token=e58e949a-a8a7-4917-9427-4e737208328b" alt="" data-size="original"> block?

Recall when we discussed the section marked by the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MVIIB5oT9i9P2l29VHj%2F-MVRzzz2RWu0i_oNzqo3%2FComment%20-%20Put%20intialization%20blocks.svg?alt=media&#x26;token=5b100e29-9c67-4bb3-be74-0d35d452cb76" alt="" data-size="original"> comment during [Programming Essentials](https://docs.revrobotics.com/duo-control/hello-robot-blocks/programming-essentials#comments). Since we only want our servo to reset ONCE we will request it do so during the initialization process when the code is first activated, but before play is pressed.&#x20;

Go ahead and click a ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FPptGnwazn3EOOOeYEOV7%2Fimage.png?alt=media\&token=497cc0f6-163a-4f17-8259-058180b06b16) block into place to match the code below:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FEP42gqEurCSKHLEyur5d%2Fservocodeinitalized.PNG?alt=media&#x26;token=6fb82ccb-e69b-418c-b5ce-495d209bf8d7" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
Try running this op mode on the test bed and consider the following question:

* What is different from the previous run?
  {% endhint %}

In many applications starting the servo in a **known state**, like at position zero, is beneficial to the operation of a mechanism. Setting the servo to the known state in the initialization ensures it is in the correct position when the OpMode runs.&#x20;

Take a moment to think about where setting the servo to a known state during initialization may be helpful before moving to the next section!
