Tutorial: Using the buttons on a LEGO™ robot

This tutorial helps explain the use of the brick's buttons in Quorum.

Using the Buttons of the Brick in Quorum

In this tutorial, we will discuss the buttons located on the brick and how they can be used in a program. Recall that the brick is the main component of every robot, where everything connects to. In addition, we will also go over the light emitting diode (LED) surrounding the buttons and its uses. Specifically, we will cover:

  • The buttons of the brick
  • The LED surrounding the buttons

The Buttons of the Brick

In total, there are six buttons located below the screen. For the purposes of this tutorial, all buttons are referred to from the perspective of a person looking at the brick; for example, the right button refers to the right-most button when we are facing the brick's buttons. Five of the buttons are grouped in the center of the brick, referred to as the up, down, left, right, and center buttons. The outlying button is above and to the left of the rest, and referred to as the escape button, due to its behavior of going back when in menus.

As mentioned in the Getting Started tutorial, a program can be stopped at any time by pressing the center and down buttons simultaneously. However, all of the buttons can be used in a program as well. By checking if a button is pressed in, we can have the robot respond to our presses and change what it is currently doing.

Another feature of the Button class is that we can have a program wait, meaning the program will stay on a certain line of code and not move to the next line, until one of the buttons is pressed. This means that if the robot is in a situation where it needs a person to tell it what to do next, based on a button press, the program can sit and wait for our input. Just remember that if something is already in progress that does not stop without the program telling it to, like the motor's RotateForward action, such behavior will continue.

The six buttons have the following actions associated with them:

  • IsButtonPressed
  • WaitForButtonPress

There are also the following class constants that can be used to refer to a specific button:

  • UP_BUTTON
  • DOWN_BUTTON
  • LEFT_BUTTON
  • RIGHT_BUTTON
  • CENTER_BUTTON
  • ESCAPE_BUTTON

Below is an example of how the button actions and constants can be used in a program:

use Libraries.Robots.Lego.Button
use Libraries.Robots.Lego.Sound

Button button
Sound sound

repeat until button:IsButtonPressed(button:ESCAPE_BUTTON)   //the loop ends if the escape key is pressed in
    button:WaitForButtonPress()                             //waits until a button is pressed
    if button:IsButtonPressed(button:UP_BUTTON)             //play a sound based on the button pressed
        sound:BeepSequenceUp()
    elseif button:IsButtonPressed(button:DOWN_BUTTON)
        sound:BeepSequenceDown()
    elseif button:IsButtonPressed(button:LEFT_BUTTON)
        sound:Beep()
    elseif button:IsButtonPressed(button:RIGHT_BUTTON)
        sound:BeepTwice()
    elseif button:IsButtonPressed(button:CENTER_BUTTON)
        sound:Buzz()
    end
end

The LED Surrounding the Buttons

Surrounding the buttons is a thin transparent border that allows the LED inside of the brick to shine through. The LED can display three different colors: green, red, and orange. Using these colors, it also has three display modes, also called display patterns. The first pattern is a solid light, which is a constant output of color. The second pattern is a flashing pattern, in which the light quickly blinks in short intervals. The last pattern is the pulsing pattern, which is similar to a flashing pattern, but blinks twice on each interval, similar to that of a heartbeat.

The LED has only one function: to display light. As such, there is one action in the Button class to use with it:

  • SetLightPattern

There are class constants to represent each of the patterns that the LED is capable of displaying:

  • NO_LIGHT = 0
  • SOLID_GREEN_LIGHT = 1
  • SOLID_RED_LIGHT = 2
  • SOLID_ORANGE_LIGHT = 3
  • FLASHING_GREEN_LIGHT = 4
  • FLASHING_RED_LIGHT = 5
  • FLASHING_ORANGE_LIGHT = 6
  • PULSING_GREEN_LIGHT = 7
  • PULSING_RED_LIGHT = 8
  • PULSING_ORANGE_LIGHT = 9

Below is the previous example program with the addition of the SetLightPattern action:

use Libraries.Robots.Lego.Button
use Libraries.Robots.Lego.Sound

Button button
Sound sound

repeat until button:IsButtonPressed(button:ESCAPE_BUTTON)   //the loop ends if the escape key is pressed in
    button:WaitForButtonPress()                             //waits until a button is pressed
    if button:IsButtonPressed(button:UP_BUTTON)             //play a sound and display a light pattern based on the button pressed
        button:SetLightPattern(button:SOLID_GREEN_LIGHT)
        sound:BeepSequenceUp()
    elseif button:IsButtonPressed(button:DOWN_BUTTON)
        button:SetLightPattern(button:FLASHING_GREEN_LIGHT)
        sound:BeepSequenceDown()
    elseif button:IsButtonPressed(button:LEFT_BUTTON)
        button:SetLightPattern(button:FLASHING_ORANGE_LIGHT)
        sound:Beep()
    elseif button:IsButtonPressed(button:RIGHT_BUTTON)
        button:SetLightPattern(button:PULSING_ORANGE_LIGHT)
        sound:BeepTwice()
    elseif button:IsButtonPressed(button:CENTER_BUTTON)
        button:SetLightPattern(button:SOLID_RED_LIGHT)
        sound:Buzz()
    end
end

Additional Information

We use class constants in this tutorial, however they are not manditory. For example, if we have a Button object called button, then the code button:IsButtonPressed(button:ESCAPE_BUTTON) is the same thing as button:IsButtonPressed("escape"). Refer to the Button class documentation for alternatives to the class constants.

Next Tutorial

In the next tutorial, we will discuss Sound, which describes how to use sounds with lego robots.