# Programming Your First Blocks OpMode

{% hint style="info" %}
This section pairs with Unit 1, Lesson 6 from the Intro to Robotics V2 curriculum.
{% endhint %}

## OpMode Structure

Let's take a look at the basic structure and key blocks of the OpMode. This what is provided when using the sample **BasicOpMode**:

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FlLjgZ64Ngw2vptgoOx7T%2Fimage.png?alt=media&#x26;token=ae33a883-fc54-4656-af07-0c65b4c62e75" alt=""><figcaption></figcaption></figure>

This sample is recommended as it provides the needed basic structure for a program to run properly with the Driver Hub, but it can be modified to best fit the current needs of the project.

The marked comments[^1] also help give direction for where different blocks should be added depending on their purpose.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FrTJDZkJ9ntkQpK66WiBg%2Fimage.png?alt=media&#x26;token=fd134cf4-1f2b-4f9d-8702-94d5ab19175c" alt=""><figcaption></figcaption></figure>

* **Put Initialization blocks here** - shows us where we will be setting up some variables[^2], resetting encoders, setting motor directions, and anything else that needs to happen when the code is first activated.
* **Put run blocks here** - is where anything that will be used when hitting the play button on our Driver Hub should be added.
* **Put loop blocks here** - is similar to our last comment, but is for anything that needs to be repeated the entire time our program is running and will be halted when pressing the stop button.

### runOpMode <a href="#call-waitforstart" id="call-waitforstart"></a>

This contains the components of the program of the designated name. Anything sitting loose in the programming space, unless in a created function[^3], will not be read when the program is run.&#x20;

### Call[^4] waitForStart <a href="#call-waitforstart" id="call-waitforstart"></a>

When the Robot Controller reaches the block ![](https://docs.revrobotics.com/~gitbook/image?url=https%3A%2F%2F1359443677-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUOOiQ4S2QcMWmVoSmeQ8%252Fuploads%252FbQ8CchwAyQTsbR1Xbzol%252Fimage.png%3Falt%3Dmedia%26token%3Db692a8e7-e12c-489d-b81c-0754fb1c9d49\&width=300\&dpr=3\&quality=100\&sign=e1dcb057\&sv=2) it will stop and wait until it receives a Start command from the Driver Hub. Any code after this block will get executed only after the Start button has been pressed.

### Call opModeIsActive

Whenever there is a call opModeIsActive, the Control Hub is checking that the OpMode is supposed to be running and has not been shut down by the Driver Hub. If something happens, for example the Driver Hub shuts off, this will change from true to false since it can no longer be checked, shutting down the current program.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FE6lkOKY3BKgdBENpWH4p%2Fimage.png?alt=media&#x26;token=f7bae646-9922-441e-8afe-ea8cc9120728" alt=""><figcaption></figcaption></figure>

In more complex programs, this call must be included in added loops[^5] alongside any other conditions, such as a count, sensor information, or time limit.

### Main Loop

This sample Blocks program defaults to being in an **iterative control structure**, meaning it's intended to continue looping until Stop is pressed on the Driver Hub or a different condition is met.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fwlh5kfM62DU4SQDIXmxs%2Fimage.png?alt=media&#x26;token=25c5318c-c417-4650-97b2-56efc46ddfc1" alt=""><figcaption></figcaption></figure>

Code that should continually run so long as the OpMode is active, will be placed in the loop. If a program starts and immediately stops, students may need to double check their code is set to loop.

## Creating our First OpMode

Let's create our first OpMode to do something similar to how we get started in other programming languages. Let's have the robot read out "Hello World!" on the Driver Hub.

To do this we can make use of telemetry.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F5duBdMgprpIa1Hqtl59z%2Fimage.png?alt=media&#x26;token=1974679d-331c-4679-99c2-c88c3bc250d2" alt=""><figcaption></figcaption></figure>

**Telemetry** is the process of collecting and transmitting data. In robotics, telemetry is used to output internal data, such as from the actuators and sensors, to the Driver Hub. It is a way for the robot to communicate back to the programmer what the robot thinks its doing or seeing.

From our **Telemetry** menu look for the call to add telemetry with a key and text.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FgHa4Wt6meJXX9aoju9X0%2Fimage.png?alt=media&#x26;token=6b5293dd-4d88-4102-806d-40d50d8cfeaa" alt=""><figcaption></figcaption></figure>

The "key" is how we label the data being shown on the Driver Hub. In this case we'll set it to "**Robot Says"**

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F1Pipl5UKnXgpn5M13vqk%2Fimage.png?alt=media&#x26;token=5e4e6950-0845-4945-9187-3c38f9f33f6a" alt=""><figcaption></figcaption></figure>

The "text" is then our output. This might be data from a sensor or just instructions serving as a reminder for running the code. We can manually enter "**Hello World!**" for this example.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FHiqzMHOmFTJ62Lui6b0u%2Fimage.png?alt=media&#x26;token=c3623709-f048-450b-8cf3-5bcd05b5a9ab" alt=""><figcaption></figcaption></figure>

Snap this block into the loop above the call to update the telemetry.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FDNjzziPFnTsmyZP5WbTu%2Fimage.png?alt=media&#x26;token=c4757ca4-311a-4471-a265-fa8cd86ba0f0" alt=""><figcaption></figcaption></figure>

From here we will click **Save OpMode** and are ready to give it a try!

### Running a Program

To run a program in the Driver Hub, first check it's connected to the intended Control Hub. The name will appear on the Driver Station App as seen here:

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2F84WWVMJGDfpv2JeONPpK%2Fimage.png?alt=media&#x26;token=3807c0fd-c168-4562-b349-8bd01f305769" alt=""><figcaption></figcaption></figure>

Then select the program from the dropdown menu. We will be sticking to TeleOp programs stored in the right menu.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FrjZWl8KMqsx5laUWMZGt%2Fimage.png?alt=media&#x26;token=de75f91a-3aad-4d85-a1fe-535b254ff480" alt=""><figcaption></figcaption></figure>

Select the OpMode from the list.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FliWLCuYIMZlwoTiiOI7P%2FScreenshot_20260319-161808.png?alt=media&#x26;token=1c122c39-393e-4065-8073-4e7acb3d409c" alt=""><figcaption></figcaption></figure>

Now we can click **Initialize**, which let the robot run any set up code we made.&#x20;

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2Fg2rwrEae1RACp1w6yVm3%2Fimage.png?alt=media&#x26;token=9eb7608f-3e86-4361-adb2-3d0a402fe5db" alt=""><figcaption></figcaption></figure>

And press **Play** when ready.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FeDqJBz2PlupxiLxZbN2m%2Fimage.png?alt=media&#x26;token=6ab0d2bc-5421-48a5-849e-8cb7adf2ce75" alt=""><figcaption></figcaption></figure>

In this example, we can see the message on the right! Pressing **Stop** will halt the code at any time.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FMCGoaE165SbbS3OTDzTI%2Fimage.png?alt=media&#x26;token=e7a82318-9694-47bf-bd1f-9f660a21a4ca" alt=""><figcaption></figcaption></figure>

### What happens if the code is not in the loop?

Let's briefly look at what happens if our telemetry block is not in the loop of our OpMode. Try dragging it to be below "Put initialization blocks here" and test it out after saving!

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FolP5O9UGeDubWzFrwzvI%2Fimage.png?alt=media&#x26;token=506a47ee-2e94-4a62-a13a-605c9daf51c0" alt=""><figcaption></figcaption></figure>

<details>

<summary>What happened?</summary>

Likely when you pressed to initialize the code on the Driver Hub, nothing appeared to happen differently. The message continued to only appear after pressing play.

</details>

Now try moving the call to update telemetry with it in the initialization area.

<figure><img src="https://3023198990-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnKoHwxC0Q1NBpLowZUmu%2Fuploads%2FzRgDXsJJ42XZuxclyXen%2Fimage.png?alt=media&#x26;token=b8a68fb5-d1e2-4a28-b42d-e3175e1a9773" alt=""><figcaption></figcaption></figure>

<details>

<summary>What happened?</summary>

Since the call to update telemetry is now before the call to waitForStart, our message will appear after initialize has been pressed, but will continue to be present after hitting play since no other command has been given.

When using telemetry, a call to update block is key to allow the information to be continually reported back, accurate, and available on the Driver Hub.&#x20;

</details>

In the next section, we'll get the actuators on the testbed spinning!

[^1]: Comments are annotations intended to help organize and communicate within a program. The robot does not do anything when it reads them so they are purely for our us as programmers!

[^2]: A **variable** is a storage location with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value

[^3]: Functions aid with organizing code. In Blocks they allow chunks of code to be broken out and represented by a call in the main loop. This helps keep the program from becoming one long, hard to read strand.

[^4]: In programming "call" means to execute. This might be used to activate a specific line of code, function, or reference collected information.

[^5]: Repeated sections of code
