Logo

Westside Robotics FTC Java Programming Guide

Detect Contact with a Limit Switch

There are many different sensors we can use with our robot. Distance sensor, color sensor and touch sensor are the most common. Since we don’t always start motors in the same position, reaching an exact position becomes very difficult. A touch sensor will allow us to know when our mechanism is at a known location.

Grab a touch sensor, plug it into your robot in the Digital section. Digital channels have 2 ports per location. When you add the touch sensor to your robot hardware configuration, it will need to be the odd port number of the 2.

Just like our motor, we need to import the TouchSensor library:

import com.qualcomm.robotcore.hardware.TouchSensor;

We also need to create an instance variable for our sensor at the beginning of our class:

private TouchSensor myTouchSensor = null;

Next, we need to get a reference to our sensor out of the hardware map in our init method:

myTouchSensor = hardwareMap.get(TouchSensor.class, "touch");

Touch sensors output a true or false, just like a gamepad button. To command to read the state of the touch sensor is:

myTouchSensor.isPressed();

Add the output of the touch sensor to a telemetry line and add that to our loop method:

// apparently we need to write this part

Build and run your OpMode. Try moving the servo to different positions based on whether the touch sensor is pressed or not.