Course 2 / Lecture 1:

My first 'hello robot' program

This lecture shows you how to start robot programming. You will learn the features of the programming environment. You will get familiar with the graphical user interface. You will learn the most important terms, such as script, block, etc. and you will create your first Hello robot program using drag-and-drop. After completing this lecture you will be able to display a smile face on the Lego brick's LCD screen and you will be able to play a beep sound on the Lego brick's speaker.

Figure 1 - The first 'hello robot' program


Task #1:

Take a quick tour of SNAP and discover its functionalities!

Please look at Figure 2 and let's see what you can do in SNAP. In the 1st rectangle, you can find two buttons. If you click on the first one, you can create, open, save, import and export projects. If you click on the second one you can find settings to change the language, zoom blocks etc. In the 2nd rectangle, you can find the blocks. You will write your programs with them. In the 3rd rectangle, there are 'Scripts', 'Costumes' and 'Sounds' fields. You can drop and attach the blocks into the 'Scripts' field to make your program. In the 'Costumes' tab, you can paint or import a new costume. In 'Sounds', you can import or record new sounds. In the 4th rectangle, you can see everything that you draw with the pen. This is the GUI which is used to show the sprites. Looking at the 5th rectangle, you can turn on the 'visible stepping' by clicking on the first button. You can use it for checking which block is being executed in real time. The following two ones responsibe for enlarging the canvas or setting it to full browser. You can start your program by pressing the green flag. You also can pause or stop the program's execution by pressing the yellow or red button.

Figure 2 - The functions you can find on SNAP's interface

Task #2:

Discover the Motion category in the GUI!

In this task, you will discover the Motion category where you will find blocks about sprite motions. The sprite is situated on the draw board, the right top side of SNAP. If you would like to move steps with the sprite, set its direction by given degrees or change its position by given coordinates. You can do these using the blocks from the Motion category. For example if you use the 'move [10] steps' block the sprite will move 10 steps on the draw board. Click on the block and you will see that you can change the number of steps by modifying the value of the block's field. You can also try to 'turn [15] degrees' block that can change your sprite's direction on the draw board (Figure 3). You can check it if you click on the block. You will see the sprite turns 15 degrees to the right.

Figure 3 - Some blocks from Motion category

Task #3:

Discover the Looks category in the GUI!

Let's discover the Looks category! In this one you will find blocks that can change the sprite's look. You can set effects to the sprite like 'fisheye', 'whirl' etc. You can also modify the size of the sprite and can hide and show the sprite on the draw board. All in all if you would like to modify your sprite's look you can find the right blocks here for that. For example the 'set [pixelate] effect to [25]' block will show your sprite with pixels as can be seen on Figure 4. Click on the block to try it. Don't worry you can delete the set effect by clicking on the 'clear graphic effects' block. You can try all effects if you click on the 'pixelate' field and choose another effect. The 'change size by [10]' block will change your sprite's size to the given value. You can zoom in and out the sprite using the block.

Figure 4 - Some blocks from Looks category

Task #4:

Discover the Sound category in the GUI!

In this task you will discover the Sound category. You can find blocks for playing imported sounds and notes, setting the instrument and changing the tempo. For example you can play a song easily if you check the sheet music of your favorite song and set the right notes and beats using 'play note [note] for [beats] beats' blocks. If you click on the note parameter you will see a piano where you can select the note that you would like to play as you can see on Figure 5. You can also set the beats if you click on its field and type another value. The 'set tempo to [60] bpm' block changes the tempo of the sounds and notes to 60 bpm. To set the tempo to another bpm, just click on the number and type the value you would like to set. Finally you can play sounds using the 'play sound [sound]' block. Do not forget to import the song you want to play on the Sounds field! There are some predefined sounds in SNAP. To import them click on the documents icon then select the 'Sounds...' option.

Figure 5 - Some blocks from Sound category

Task #5:

Discover the Pen category in the GUI!

In this task, you will discover the Pen category where you can find blocks about the pen to be able to draw colored lines and shapes. It is a really useful thing to track the cursor. To draw a line, first you need to put the pen down using the 'pen down' block. You can set the pen color and size using the 'set pen color to [color]' and 'set pen size to [value]' blocks like on Figure 6. These blocks are responsible for the color and size of the line to be drawn. Finally you just need to move the cursor to start drawing. To move it, use the 'move [steps] steps' block from Motion category. You can stop drawing by using the 'pen up' block and can also delete all drawn lines using the 'clear' block.

Figure 6 - Some blocks from Pen category to draw a lines

Task #6:

Discover the Ozeki category in the GUI!

Let's discover the Ozeki category! In this one, you can find blocks that have the same functionality like the chat window of the connections detail's page in the Control Panel. If you would like to send a message to a certain connection, you need to use the 'To [connection], message: [command]' block. Type the command message into the 'command' field of the block and select the connection you would like to send the command message to. You can also subscribe for the incoming messages from the specified connection and unsubsrcibe using 'Subscribe' and 'Unsubscribe' blocks. These blocks can be seen on Figure 7. You can also set incoming message value to variable as int, float or string. You can check where the message comes from or check if the message is available or not.

Figure 7 - Some blocks from Sound category to send and receive messages

Task #7:

Discover Lego category in the GUI!

In the Lego category, you can find blocks to get information about measured values of connected sensors or do something with the connected Lego devices included the EV3 Brick itself. You can check the measured distance in both cm or inch of the ultrasonic sensor, read the button status of the IR remote controller etc. You can also change the Brick's LED color, display emojis on the Brick's LCD screen and play sounds through the Brick's speaker. For example the 'Display [emoji] on LCD' block can show the selected emoji face on the Brick's screen. The 'play [C5] note for [500] ms' block plays the 'C5' note throgh the Brick's speaker for 500 ms. Finally the 'Remote button [connection]' block is used to check which button is pressed on the IR beacon. Select the IR remote controller for the connection field then check the state by clicking on it as Figure 8 demonstrates.

Figure 8 - Checking the object proximity using a block from Lego category

Task #8:

Discover the Control category in SNAP!

In this task you will discover the Control category. In this category, you can find blocks about the start points for the programs and you can find programming tools like loops to repeat tasks, if-else to conditional execution etc. If you would like to write a program you will need a block that can start your program. There are blocks for that in the Control category. These blocks' name start with a 'when' prefix as you can see on Figure 9. Using the 'when green flag clicked' block, the blocks attached to it execute if the green flag icon on top right corner has pressed. You can also find block to execute the blocks attached to it when you press a specified key on the keyboard. The loop block types are 'forever', 'repeat', 'repeat until' etc. They repeat the blocks inside them. For example the 'repeat [10]' block repeats the blocks inside for 10 times. You change the repeat time by chaning the number field of the block. The 'forever' block repeats the blocks inside until you stop the execution of the program by pressing the red stop button on the top right corner of the form.

Figure 9 - Some blocks from Control category

Task #9:

Discover the Sensing category in the GUI!

In this task, you will discover the Sensing category. In this category you can find blocks that can detect if a key is being pressed, measure direction or distance to mouse-pointer, set turbo mode and you can get the current date here. There is also a timer you can use to measure the time. If you turn on the mentioned turbo mode, it makes scripts run faster by updating the screen less often, so that sprite motion does not appear immediately. Let's see some examples! The 'key [key] pressed?' predicate type of block becomes true if you press the given key on your keyboard. You can set any key for the 'key' field of the block. You can get user input by using the 'ask [question] and wait' block. It asks and waits for the user as well as you can see on the draw board (Figure 10). You can get the given answare by getting the value of the 'answare' variable. Finally, there is a 'reset timer' block that resets the timer. The current value of the stopwatch is stored in the 'timer' variable.

Figure 10 - The 'key [space] pressed?' block is false and you can see many other sensing blocks

Task #10:

Discover the Operators category in the GUI!

Let's talk about the Operators category! The blocks that can be found in this category is use to compare values or make calculations with constants or predefined variables. You can use addition, subtraction, multiplication and division blocks to calculate the result and you can get random numbers too. You can get the square root of a number as well, but also the length of a word or a character in a word. You can also get a unicode of a letter. Let's take some example! The 'pick random [number] to [number]' block can generate random numbers between the numbers you have set. The '[number] x [number]' block can multiplicate the numbers that you have entered. Finally the '[number] < [number]' block returns true if the number of the block's left side smaller then the right side's one. Click on the blocks to show the result as can be shown on Figure 11.

Figure 11 - Some blocks from Operators

Task #11:

Discover the Variables category in the GUI!

In this task you will discover the Variables category where you can create variables. The variables are used to store numbers and strings. In this category you can find blocks to set, change or increase the variable values. You can show and hide the variables by checking the checkbox before the variable name on the left-side panel. You can use lists if you would like to store more numbers, texts or variables in one place. You can find blocks to create list, check if something is in a list, delete an item from a list and replace an item in a list. Let's take some example to create a variable, click on the 'Make a variable' button then type a name for it (e.g. list_variable) to be created. After creating the made variable shows on the left-side panel as you can see on Figure 12. To set a list to the variable, please use the 'set [variable] to [value]' block. Select the 'list_variable' for the block's 'variable' field then drag a 'list [values]' block into the 'value' field of the block. To check if the list contains a certain value, you can use the '[list] contains [value]' predicate type of block. Finally add an item to the list by using 'add [value] to [list]' block.

Figure 12 - Some blocks from Variables category

Task #12:

Discover the Gamepad category in the GUI!

In this task, you will discover the 'Gamepad' category. In this category, you can find blocks that can check the connected gamepad states. If you would like to control your robot using the gamepad, these blocks are right for you to check if a specified button has pressed on it. There are also blocks to check the value of the specified axis. For example the 'gamepad [gamepad] button [button] pressed?' block is 'true' if the gamepad's button has pressed as you can see it on Figure 13.

Figure 13 - How to check if a button has been pressed on your gamepad

Task #13:

Discover the Lego motors category in the GUI!

Let's discover the Lego motors category! The blocks here you can use to control the Lego large and medium motors. You can drive them forward and backward, you set how many degrees and how much time you want to rotate them. If two motors are connected to the Brick, you can drive the robot left and right. You can also set the motor's speed in percentage. Look at Figure 14 to see some examples! The first two blocks are the simplest ones. You can use the 'Start motors [B+C]' block to start the selected motors at 100% speed. To stop them, you should use the 'Stop motor' block. The third one is the 'Drive [B+C] [forward] [3] sec at [100] %' block. It turns the motors connected to the B and C ports of the Brick forward for 3 seconds at 100% speed. The fourth one is the 'Rotate [B] by 360°' block that turns the robot's B motors forward by 360 degree.

Figure 14 - Some Lego motor blocks

Task #14:

Discover the Database category in the GUI!

In this task you will discover the Database category. The blocks here use to make changes on a database and get values from it. First you need to choose the database you want to work on. You can use the 'Use database [database]' block (Figure 15) for it. Then you need to select the right database from the drop-down list for the block's database field. If your list is empty, it is because there is no database connection installed in Ozeki 10. In Course 9, you will learn how to set up a database connection and how to use the blocks to make changes on the selected database.

Figure 15 - How to select the database that you would like to use

Task #15:

Write program to display a smile on the Brick's display and play a note through its speaker!

If you have learned the SNAP basics, let's write a simple 'hello robot' program! The program should display a smile on the EV3 Brick's LCD screen and play a C5 note using its speaker if the green flag on the top right corner of the GUI has clicked. First you need to place the 'when green flag clicked' block from the Control category for the entry point of the program. It actives and all the attached blocks to it will execute when you click on the green flag. To display a smile, you need to use the 'Display [smiley] on LCD' block found in Lego category. Finally you should use the 'Play [note] note for [time] sec' block. Attach them to the previously placed control block like on Figure 16 then try it by clicking on the green flag button on top right corner.

Figure 16 - Display a smiley and play a note

Program code

Figure 17 - The program code displays a smiley and plays a tone

More information


Copyright © 2000- |Ozeki Ltd | |
Page: 6189 | | Login
Thank you for visiting this page