Programming Touch Sensors
Last updated
Last updated
This section applies to the use of the REV Touch Sensor or Limit Switch. Requirements may vary when using other 3rd party touch sensors.
The REV Touch Sensor must be configured to digital port 1, 3, 5, or 7.
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:
Let's start by breaking down how a touch sensor works at its core!
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.
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.
The block collects the binary TRUE/FALSE
state from the touch sensor and acts as the condition for the if/else
statement.
Let's take a look at our touch sensor block paired with our block:
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 _____."
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.
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.
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.
Let's give our code another try to see what happens on the Driver Hub's Screen. Did you see something like the following?
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:
Take a moment to think about how else telemetry data could be used with your robot before moving on to the next section!
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.
For the touch sensor, one of the most common uses is for it to act as a limit switch. 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.
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".
Give it a try!
We can access the "Telemetry" blocks under our "Utilities" dropdown on the menu. Look for the block to be added in each section of the if/else statement.
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 block:
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 block from the "Logic" menu.