Using a Gamepad with a Servo
Last updated
Last updated
Having our robot able to rotate the servo automatically can be incredibly useful, especially when writing an autonomous program, but what if I want to control the positions with my gamepad?
Let's take a look at how we can add input commands to our code!
For this example the known state will stay at position 0, so that after initialization the servo will be a the -135 degree position of the servo range. The following list shows what buttons correspond with which servo position:
If you are using a PS4 Controller, selecting the appropriate button from the dropdown in Blocks may be easier to follow when looking back at your code. The buttons are also interchangeable when programming in Blocks. (ex: Y in code = Triangle pressed on controller)
Button | Degree Position | Code Position |
Y/Triangle | -135 | 0 |
X/Square | 0 | 0.5 |
B/Circle | 0 | 0.5 |
A/Cross | 135 | 1 |
Blocks for adding controller inputs can be found in the "Gamepad" menu:
One of the most common logic statements used in programming is an if/else statement, also known as an if/then statement. This block can be found under the "Logic" menu in Blocks:
In its most simple format we will be asking our robot to check IF something is happening and if the answer is yes, or true in our robot's mind, THEN it will DO what has been asked.
During this section we are going to be asking "If the Y button is pressed on our controller then move our servo to position 0."
If our servo will move to position 0 when the previous statement is TRUE, what do expect to happen when the answer is FALSE (or the Y button is not pressed)?
Now our statement is checking first if Y is being pressed to move to position 0, but has added now the option to look for something else, such as another button being pressed.
Let's add to our existing logic statement the ability to move our servo to position 1 when A is pressed on our controller. Give it a try first before revealing the answer below!
How would our full logic statement be read once our new blocks are added?
What happens when both buttons are pressed at the same time?
To add all of our gamepad inputs we need to further extend our if/else if statement:
Now there are three different paths in our if/else if block that our robot may follow based on each input request. We've previously added our ability to move to position 0 and 1, but what about 0.5?
The logical operator or considers two operands if either (or both) are true the or statement is true. If both operands are false the or statement is false.
Similar the logical operator and considers two operands requiring both to be true for the whole statement to be true.
Our previously added blocks for A and Y inputs can be temporarily moved to the side in the workspace to be readded as applicable.
Add each button block to the if/else if block as seen in the image below.
Click Save OpMode and give your program a try!
There are three different paths in this if/else if statement. If the first conditional statement is true (the Y button is pressed) the servo moves to code position 0 and the other conditional statements are ignored.
If the first condition is false (the Y button is not pressed) the second condition is analyzed. This means the order we add our pathways DOES matter. If X and A are pressed at the same time, the robot will will try to prioritize the X button first.
Give it a try!
Our if/else statement can come in many forms that includes multiple different conditional statements. Blocks allows for our base block to be easily added to add as many conditional statements as we need by clicking the blue gear on our block.
Adding an block by clicking and dragging to our existing if statement converts it into becoming an if/else if statement. Using our previous example we can see how this may look in Blocks:
If you have not already, test the code we have written thus far! Our logic statement should be add to our and previous removed.
You may have noticed in our gamepad chart at the beginning of this section that we are going to have two buttons able to move our servo to position 0.5. This is so we can practice using a logical operator like the block in our program!
From the Logic Menu in Blocks select the block.
Add this block to the if/else if block, as shown in the image below. Use the dropdown menu on the block to change it from an block to an block.
Now to finish by adding our blocks to each section of the If/else if block. Set the servo position to correspond with the assigned gamepad button.