Course 2 / Lecture 4:

Understanding events

Modern programming is more about processing events then about writing algorithms. In this lecture you will learn how to detect events, and how to react to them. You will see how your robot can react to changes detected by sensors, and how you can react to events coming from the user interface, such as a keyboard key press. You will write different programs in this lecture, for example a program to control a robot form the keyboard, and another one, that reacts to events by displaying emoji icons on the LCD display.

Figure 1 - Detect events and react them


Task #1:

What is an event?

Event - In programming, an event is an action that occurs as a result of the user or another source, such as a mouse being clicked, or a key being pressed.

Task #2:

What is an event handler?

Event handler - It is a routine that is used to deal with the event, allowing a programmer to write code that will be executed when the event occurs.

Task #3:

Discover event handler blocks in SNAP!

In the previous lectures you have used the 'when green flag clicked' block. That block is an event handler because it handles the green flag clicked event. So the blocks attached to it will be executed when the green flag event occurs. To trigger this event, you need to click on the green flag on the top right corner of the GUI. As you did before, you could attach other blocks to the 'when green flag clicked' block. Due to this, you could create programs that were executed when you have clicked on the green flag button. There are other event handler blocks. Their names begin with 'when' prefix. Let's look at some of them! The second one is the 'when [key] key pressed' block that handles the key presses events. You need to select a key for its key field and then it will be executed when the selected key event occurs. To trigger this event, you just need to press the selected key on your keyboard. For example, if you choose the 'space' key for the block's key field, the block will active when you press the space key. The 'when I am [clicked]' block executes when the sprite on the drawing board is clicked. The 'when [condition]' block activates when the condition in its condition field becames true. Snap the blocks as can be seen on Figure 2 and try them out!

Figure 2 - Event handler blocks in SNAP

Task #4:

Write program to display different emojis on the Brick's screen by key presses!

You need to write a program that changes the faces on the Brick's screen by key presses. If you press the 'h' key of your keyboard, a happy face should appear on the Brick's screen. If you press the 'n' key, a neutral face should appear and if you press the 's' key, a sad face should appear on the Brick's LCD. You should use the 'when [key] key pressed' block from Control category to solve this task. Using this block, you can detect different key presses. You just need to attach the 'Display [smiley] on LCD' blocks to the 'when [key] key pressed' blocks like on Figure 3 then try it out!

Figure 3 - Displaying emojis on the screen

Task #5:

Create a drawing program that you control using your keyboard!

You need to write a program to be able to draw lines and shapes on the drawing board. If you press the up arrow on your keyboard, the sprite should point up and move 10 steps. If you press the down arrow, the sprite should point down and move 10 steps. If you press the rigth arrow, the sprite should point right and move 10 steps. Finally, if you press the left arrow, the the sprite should point left and move 10 steps on the draw board. To accomplish these, you will need 3 different blocks. First, you should detect the key presses. For this, you need to use a 'when [key] key pressed' control block. Then you should draw a line on the drawing board to a specified direction depending on the key pressed. To achieve this, you need 'point in direction [direction]' and 'move [number] steps' blocks from Motion category. Using the first one, you can change the direction of the cursor and with the second one, you can move it. At this point, you have everything to control the cursor using your keyboard. To be able to draw, you need to put the pen down. So you need to create an entry point that puts the pen down by pressing the 'd' (like down) key on your keyboard. Upgrade the prgram to have options to put the pen up and clear the drawing board. Look at Figure 4 for help.

Figure 4 - Drawing shapes with the pen

Task #6:

Write a program that displays a text on the Brick's LCD if the Lego Touch sensor is pressed!

Let the task be to display the 'Touch sensor pressed!' text on the Brick's display if the Lego Touch sensor is pressed. You can find a 'Button on [connection] ?' block in Lego category that returns true if the button is pressed on the connected touch sensor. To detect this event, you need to put this block into the condition field of a 'when [condition]' block. Due to this, the 'when' block will be activated when you press the touch sensor. Before displaying the text on the Brick's LCD, you need to delete its currently content and set the font size to medium. You can do these by using the 'Clean LCD' and 'LCD font size [size]' blocks found in Lego category. To set the text to be displayed, you need to use the 'LCD text [text] position x: [x_value] y: [y_value]' block. Type the 'Touch sensor pressed!' text into the text field and enter 5 for x_value and 60 for y_value to display the text in the middle of the screen. Finally, use the 'Update LCD' to update the screen to display the set text. There was nothing left to try out the program! The complete program can be seen on Figure 5.

Figure 5 - Displaying the text 'Touch sensor pressed!'

Program codes

More information


Copyright © 2000- |Ozeki Ltd | |
Page: 6194 | | Login