Buttons

Hardware

The Romi has three small push buttons that you can use to control your code. These LED’s are considered digital channels, specifically digital inputs. These means that when you read them, they can be one of two states: true or false.

The FTC Romi app already provides names for these devices to use in your code. Button A is "dio_0", B is "dio_1", and C is "dio_2".

Notice that these DIO device names overlap with some of the LED devices from the last guide. You can only use each DIO device as an LED (output) or a push button (not both). You can control this by setting the device’s mode to either output or input.

The locations of the buttons are shown below:

../../_images/hardware.jpg

If you are using Blocks, you can already access these devices through the toolbox on left when writing code!

../../_images/toolbox.png

Configuration

In order to use these digital channels as inputs, we have to configure them as such in the init section of our code. We can do this with setMode:

../../_images/input.png

Reading the Buttons

The push button devices are digital inputs, meaning that they can be one of two states: true or false. As you may expect, the state true indicates that the button is press, and the state false indicates that the button is not pressed. We can read a digital input’s state by using getState

To test that we can read the states of the buttons, we can add some basic code to the loop section of our code that outputs the states of each button to the telemetry (the screen on the Driver Station app). Add the highlighted code right before the telemetry.update() line/block.

The telemetry.addData block is under Utilities -> Telemetry in the toolbox. Make sure to use the block that has a parameter called text rather than the one with a parameter called number.

../../_images/telem.png

Using Button Inputs

Now we can do something more complicated with the buttons. Let’s make it so that while you are holding down a button, the robot moves in a certain way.

We can do this by using an if statement. Code in an if statement will only be ran if a certain condition is true. For instance, we can make it that if button A is being pressed, then the robot will move forward at 30% speed. In this case, the condition is that the state of dio_0 is true.

Add the following code right before the telemetry blocks/lines that we added earlier, but do not run this code yet:

You can grab the if block from the Logic tab of the toolbox.

../../_images/forward.png

This code may look good, but it contains one major flaw: if we stop pressing push button A, the robot will continue moving forward at 0.3 power because we never tell it to stop!

To fix this, we will add an else case to our if statement. An else case tells the program what we should do if the condition is not true (meaning that it is false). Add an else case to the if statement to set the drive motor powers to 0.

You can modify the if statement by clicking the gear on the statement and dragging the “else” block below the “if” block (shown in the picture below).

../../_images/forward_stop.png

Finally, let’s extend our if statement even further. We can add additional conditions to our if statement by using else if blocks. Else if blocks should in between the if block and the else block.

Let’s make it so that push buttons B (dio_1) and C (dio_2) make the robot turn left or right while pressed (respectively). Here is the full logic that we are going to create with our if statement:

  • If push button A is pressed, the robot moves forward.

  • Else, if push button B is pressed, the robot turns left.

  • Else, if push button C is pressed, the robot turns right.

  • Else (otherwise), stop the robot.

The if statement code should now look like this:

You can modify the if statement by clicking the gear on the statement and dragging the “else if” blocks below the “if” block (shown in the picture below).

../../_images/turn.png

Full Code

../../_images/code.png