# Application Examples

## Application Information

The REV Touch Sensor features an off-center button. Because this sensor requires a contact interface; the sensor must be mounted with regards to the location of the button and the object, or mechanism, intended to trigger the sensor.&#x20;

Common applications for the Touch Sensor, such as limit switches, require consideration for unconstrained, or twisting motion. Limit switches limit the range of motion for a mechanism. If the mechanism is not properly [constrained](https://docs.revrobotics.com/15mm/building-techniques/constraining-motion), there is a risk that the contact interface will not trigger the Touch Sensor.

### FTC Applications&#x20;

#### Configuring in the Control System&#x20;

Configure the Touch Sensor as "REV Touch Sensor" as shown in the image below.&#x20;

<figure><img src="https://1166281274-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-ME3KPEhFI6-MDoP9nZD%2Fuploads%2FMFeLVolwJUxBSJOqLN73%2Fimage.png?alt=media&#x26;token=b8cf5592-6f7c-4a30-84da-8c5b266194a6" alt=""><figcaption></figcaption></figure>

In this example, the Touch Sensor is configured on port one. It is touched on briefly in the[ Pinout Section ](https://docs.revrobotics.com/rev-crossover-products/sensors/specs#pinout-and-schematic)that the Touch Sensor only sends a signal to the Control Hub through the n+1 communication channel. Because of this limitation, the Touch Sensor will only work when configured on the odd-numbered digital ports.&#x20;

#### Programming Applications&#x20;

The code blocks below give a basic example of how to use the Touch Sensor to limit the motion range of a motor using if/else logic. If the button is pressed then the motor stops. Otherwise, the motor is allowed to move.&#x20;

{% hint style="info" %}
To learn more about programming Touch Sensors check out Hello Robot for [Blocks ](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-1/programming-touch-sensors#touch-sensor-basics)and [OnBot Java](https://docs.revrobotics.com/duo-control/hello-robot-java/part-1/programming-touch-sensors)!
{% endhint %}

{% tabs %}
{% tab title="Blocks" %}

<figure><img src="https://1166281274-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-ME3KPEhFI6-MDoP9nZD%2Fuploads%2FFjsZSDoMYraRGSbadfqt%2Fimage.png?alt=media&#x26;token=7d9ee667-8c9b-4c03-a276-d36435510ec4" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="OnBot Java" %}
{% hint style="info" %}
The code assumes the sensor has been named "test\_touch" and the motor has been named "test\_motor" in configuration.&#x20;
{% endhint %}

```java
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.TouchSensor;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;

@TeleOp

public class HelloRobot_TouchSensor extends LinearOpMode {
    TouchSensor test_touch;  // Touch sensor Object
    private DcMotor test_motor = null;
    private Servo test_servo = null;
    
    @Override
public void runOpMode() {
        test_motor = hardwareMap.get(DcMotor.class, "test_motor");
        test_touch = hardwareMap.get(TouchSensor.class, "test_touch");
        
        // Wait for the game to start (driver presses PLAY)
        waitForStart();
        
        // run until the end of the match (driver presses STOP)
        while (opModeIsActive()) {
        
            if (touchSensor.isPressed()){
                //Touch Sensor is pressed.
                test_motor.setPower(0);
                telemetry.addData("Touch Sensor", "Is Pressed");
            } else {
                //Touch Sensor is not pressed 
                test_motor.setPower(0.3);
                telemetry.addData("Touch Sensor", "Is Not Pressed");
                        }
        telemetry.update();
        }
    }
}
```

{% endtab %}
{% endtabs %}
