# Using Limits to Control Range of Motion

In [Part 2: Robot Control](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-2/arm-control-blocks/adding-a-limit-switch) the idea of creating a limit switch was introduced using a physical sensor, like a touch sensor. We can make use of our motor's built-in encoder to do something similar. While a physical sensor would be described as a hard limit, using the built-in encoder is called a soft limit.

To set the soft limits we will build off the program created in the last sections (HelloRobot\_ArmEncoder)!

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FUg6PZEssyT8cg6ca4Xx3%2Fimage.png?alt=media&#x26;token=0b256e99-6642-4913-9820-a417c7aff50e" alt=""><figcaption><p>HelloRobot_ArmEncoder program</p></figcaption></figure>

### Creating minPosition and maxPosition

To start, we need to create our upper and lower limits. Create two new variables one called `minPosition` and one called `maxPosition` to be added to initialization.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F3RlwpLwwDk6ynta7Jp1T%2Fimage.png?alt=media&#x26;token=eed58823-2e70-44b4-b483-da895b3843a0" alt=""><figcaption><p>maxPosition and minPosition added for our limits</p></figcaption></figure>

For now we want the `minPosition` set as our starting position and the `maxPosition` set to our 90 degree position. Set `minPosition` equal to <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRB7rWtI2jshN4W7LJ%2FMath%20-%200.svg?alt=media&#x26;token=99ad04a5-2f5e-4c97-a3f1-aa6471df73b2" alt="" data-size="original"> and set `maxPosition` equal to <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-Mb7NRLlN4cP21O7utyz%2F-MbDKWl-0JA7163odOxB%2FArm%20-%20degree%20times%2045.svg?alt=media&#x26;token=e6a98555-aa7e-4a60-83f9-9d0330139052" alt="" data-size="original"> .  The ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FhrFxXaL515iousGSa632%2Fimage.png?alt=media\&token=8ad2155b-9bb1-48bd-88d5-0cdc188f22ee) block previously used for [`armPosition`](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-3/calculating-target-position#adjusting-our-if-else-statement) can be moved to our `maxPosition`.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FNrZGZoppW1oP44Ty0cYw%2Fimage.png?alt=media&#x26;token=cad24b49-bfcf-4e69-a3bc-e6b8e92a5e29" alt=""><figcaption></figcaption></figure>

### Adjusting our If/Else Statement

To start, our If/Else Statement will be changed back to a simplified format like we had at the beginning of[ estimating the position of the arm](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-3/arm-control-with-encoders-blocks/estimating-the-position-of-the-arm).

![If/Else Statement for arm movement](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-Mb7NRLlN4cP21O7utyz%2F-MbDOTQhQBREe4ZXSqoI%2FArm%20-%20limit%20if%20else%20logic.svg?alt=media\&token=f85b763f-de4a-4031-8774-4e505ebd8547)

To set the limit we need to edit our `if/else` statement to include our limits:

* If up on the Dpad is pressed and the position of the arm is less than the `maxPosition`, then the arm will move to the `maxPosition`.&#x20;
* If down on the Dpad is pressed and the position of the arm is greater than the `minPosition` then the arm will move towards the `minPosition`.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F0swyJqkv1qFX2mn8Mw3C%2Fimage.png?alt=media&#x26;token=74515439-2be2-4177-b448-3e8cb57298e6" alt=""><figcaption><p>Use the "logic" dropdown to fill out the statement's check</p></figcaption></figure>

## Overriding Limits

One of the benefits of having a soft limit is being able to exceed that limit.

Remember that the encoders zero tick position is determined by the position of the arm when the Control Hub powers on! So if we aren't careful to reset the arm before powering on our robot this will effect the arm's range of motion. For instance, if we have to reset the Control Hub while the arm is in the 90 degree position, the 90 degree position will become equal to 0 encoder ticks.

As a back up, we can create an override for the range of motion. There are a few different ways an override can be created, but in our case we are going to use the "A" button and touch sensor to help reset our range.&#x20;

### Adding a Gamepad Override

Start by editing the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-M_2CE63M33f73tG7c96%2F-M_6poiXjN6Ho7PTyWm3%2FLogic%20-%20if%20else%20if%20else.svg?alt=media&#x26;token=c6d1c93a-7ca7-4e36-a1ba-3baf88a05acb" alt="" data-size="original"> to add another <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAnBZGxGoi9TO3k6FC%2FLogic%20-%20else%20if.svg?alt=media&#x26;token=33006b12-eb23-4836-928f-9c7bcd57d0e5" alt="" data-size="original"> condition. Use the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-Mb7NRLlN4cP21O7utyz%2F-MbIJAD2Ou3ksEcYY6ya%2FGamepad%20-%20a%20button.svg?alt=media&#x26;token=d269b4d5-7832-49cf-bcc9-485485e7dfe6" alt="" data-size="original"> block as the condition. Add a <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-Ma9eNmmkAyF5RUbfPkd%2F-MaAmcRjToPMPrh6OqtT%2Farm%20-%20set%20power%20to%200.svg?alt=media&#x26;token=a3889b45-d9db-4059-a6b8-55774c2c71fd" alt="" data-size="original"> block to the do portion of the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-M_2CE63M33f73tG7c96%2F-M_6poiXjN6Ho7PTyWm3%2FLogic%20-%20if%20else%20if%20else.svg?alt=media&#x26;token=c6d1c93a-7ca7-4e36-a1ba-3baf88a05acb" alt="" data-size="original"> block and set the power to -0.5.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FL4Zbjf69uuoHn5TIWs05%2Fimage.png?alt=media&#x26;token=4a7d5f18-9374-4721-b51d-9ee11f081a85" alt=""><figcaption><p>Adding gamepad "A" override</p></figcaption></figure>

Now that we have this change in place, when the "A" button is pressed the arm will move toward the starting position.&#x20;

### Adding a Touch Sensor Limit

Next, when the arm reaches and presses the touch sensor we want to `STOP_AND_RESET_ENCODER` .

We can create an additional <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWAm39BsREHDm26Al4M%2FLogic%20-%20ifdo%20(blank).svg?alt=media&#x26;token=ce3fdbbd-68c4-4b67-bc50-981a1e204bae" alt="" data-size="original"> statement that focuses on performing this stop and reset when the touch sensor is pressed. Check out [Programming Touch Sensors](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-1/programming-touch-sensors) from Part 1: Tackling the Basics for review if needed!

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FSTm7CcIlWQEZC6JqFMbK%2Fimage.png?alt=media&#x26;token=db6d8f8c-4eb6-4819-9c3d-84bfd7960653" alt=""><figcaption><p>Touch sensor limit switch added as a separate If/Else statement</p></figcaption></figure>

Save your OpMode and try testing it!
