# Programming Touch Sensors

{% hint style="danger" %}
This section applies to the use of the REV [Touch Sensor](https://www.revrobotics.com/rev-31-1425/) or [Limit Switch](https://www.revrobotics.com/rev-31-1462/). Requirements may vary when using other 3rd party touch sensors.&#x20;

The REV Touch Sensor must be configured to digital port 1, 3, 5, or 7.
{% endhint %}

{% hint style="warning" %}
It is recommended to create a new OpMode while following this tutorial. Ours is named HelloRobot\_TouchSensor!

The touch sensor block is now found under the "Sensors" dropdown as seen below:

<img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F6GH4IQ0Wn0CoGcjxrU9i%2Ftouchsensormenu.PNG?alt=media&#x26;token=7ac7f04a-5954-4a94-91dc-c05a6c5cd970" alt="" data-size="original">
{% endhint %}

## Touch Sensor Basics

Let's start by breaking down how a touch sensor works at its core!

{% hint style="info" %}
Remember what sensors and motors are available in your program are determined by your configuration! Double check the correct configuration is active if you do not see a device list.
{% endhint %}

The information collected by a touch sensor comes in two states, also known as binary states. This information is perfect to use with a conditional statement like an `if/else` statement.&#x20;

The block ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FHInNF76MHJ4tjU9YGYpi%2Fimage.png?alt=media\&token=d30f78ab-cf21-41c7-95ba-81c91188f117) collects the binary `TRUE/FALSE` state from the touch sensor and acts as the condition for the `if/else` statement.&#x20;

Let's take a look at our touch sensor block paired with our ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F5JwSsz7hxx6ZWoCz4r21%2Fimage.png?alt=media\&token=af8832ff-493e-4cfc-aca9-e5c993b1c736) block:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FUvJYDlx6XkTBqf5Sf9g0%2Ftouchblockifelse.PNG?alt=media&#x26;token=5ad1f3e0-1875-45e3-8b50-bf20dc2bc2be" alt=""><figcaption></figcaption></figure>

Take a moment to think what this code is asking the robot to do. We could read this line of code as "If the touch sensor is pressed do \_\_\_\_, else if the touch sensor is not pressed do \_\_\_\_\_."&#x20;

### Adding Telemetry

It's always helpful for us to be able to see what the robot thinks its doing on our Driver Hub's screen. To do this, let's request the robot shares some telemetry data while our program is active.&#x20;

We can access the "Telemetry" blocks under our "Utilities" dropdown on the menu. Look for the ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FNyJ7F5X59tVSpseIqrTZ%2Fimage.png?alt=media\&token=11a85bd6-fb51-4c26-acf8-357cf8122f39)block to be added in each section of the if/else statement.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FGdAcXMaY2nKX5OYPSiIW%2Ftouchsensortelemetry.png?alt=media&#x26;token=262c4135-a8eb-4f77-9c1c-fc82c8eda176" alt=""><figcaption></figcaption></figure>

What happens if you run the program right now?

When on the default "Telemetry" block the information provided is not helpful for the robot to communicate with us. Therefore we need to change "key" and "text" to match the desired information.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FJf1aNitucGHYgbfwMKrs%2Ftouchtelemetry.png?alt=media&#x26;token=5f87d549-f059-4ada-b741-0a4e7a85f05a" alt=""><figcaption></figcaption></figure>

The "key" should be something related to which sensor, motor, or other device we are receiving information from. Meanwhile "text" will tell us what is happening based on the state of our touch sensor and our if/else statement.&#x20;

Let's give our code another try to see what happens on the Driver Hub's Screen. Did you see something like the following?

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F2yz4mr1JRrwWPVlsnXQv%2FScreenshot_20240603-164913.png?alt=media&#x26;token=41e43132-a8b8-462e-8e55-b05da0cf5e0b" alt=""><figcaption></figcaption></figure>

Remember its up to us to decide what our telemetry readout says. With that in mind we could change it so our robot says "Hello World" when the button is pressed:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FCDxmd1wTgXICljrFerpd%2FScreenshot_20240604-103212.png?alt=media&#x26;token=c3aa2882-bb71-455b-b457-d1da851e882d" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
Take a moment to think about how else telemetry data could be used with your robot before moving on to the next section!
{% endhint %}

### Touch Sensor as a Limit Switch

At the moment, our robot does not have any senses to help navigate the world around it like you might. However, that's the key advantage to adding sensors to our design.&#x20;

For the touch sensor, one of the most common uses is for it to act as a [limit switch](https://docs.revrobotics.com/duo-control/sensors/digital#applications). This will help the robot know when it needs to halt the movement of a mechanism, like an arm or lift, that's at its limit similar to how your nerves help to tell your brain to do the same.

We can test this idea by adding on to our existing if/else statement. This time we are going to ask our motor to move until our sensor is pressed using the ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F1ZC4B0mdn8prCz4UqYRH%2Fimage.png?alt=media\&token=349c4d0a-0a99-4a78-98b0-deea686a62b1) block:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F8Zxdwd68z9E1ppaaOCE6%2Ftouchmotorcontrol.png?alt=media&#x26;token=0a55f12a-fce4-43a1-875b-31651d4fb3b0" alt=""><figcaption></figcaption></figure>

### Reversing it

In the above example the if/else is checking first for if the touch sensor is pressed. The full statement could be read as "If the touch sensor is pressed set the motor's power to 0 else, if it is not pressed, set the power to 0.3".

There may be situations where we want our program to read if the touch is NOT pressed first. Let's take a quick look at how that would function using the ![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Ff8RTYydPvD3q6MK1ISvS%2Fimage.png?alt=media\&token=2a85b7d0-f2fd-4fb6-9c74-3e2d13d48cfa) block from the "Logic" menu.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FzqDigwXCDPP0LI7Z7pbC%2Freversetouch.png?alt=media&#x26;token=7c33c964-7745-46f1-ace2-9d1782fcc57c" alt=""><figcaption></figcaption></figure>

Give it a try!
