LEDs

Hardware

The Romi has three small LED lights that you can control with your code. These LED’s are considered digital channels, specifically digital outputs. These means that you can set them to one of two states: true or false.

The FTC Romi app already provides names for these devices to use in your code. The green LED is "dio_1", the red is "dio_2", and the yellow is "dio_3".

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 outputs, we have to configure them as such in the init section of our code. We can do this with setMode:

../../_images/output.png

Turning the Lights On

The LED devices are digital outputs, meaning that you can set them to one of two states: true or false. As you may expect, setting the channel to true turns the LED on, and setting it to false turns it off. We can control a digital output’s state by using setState

By default, all the channels are set to false (LED off). Let’s check that the LED devices are working by setting all their states to true in the loop section of our code:

../../_images/true.png

Running this code should cause the three LED’s to turn light up on the Romi!

Light Show

Now we can do something more complicated with the LED’s. Let’s make it so that each LED turns on individually, with a short delay in between.

We can use the sleep(ms) function to create a delay in our code. ms is the number of milliseconds we should delay for. 1000 milliseconds = 1 second, so if you wanted to delay for a half second for example, you would write sleep(500).

Replace your loop code with the following code:

You can grab the sleep block from the LinearOpMode tab of the toolbox.

../../_images/light_show.png

Full Code

../../_images/code2.png