Beyond Basics, Ex. 1-6

1. Multitasking

The interest of the exercise is of course that it demonstrates multitasking i.e. the ability of an EV3 program to run multiple branches or 'threads' simultaneously. This program splits into two branches after the start block, so both branches will run simultaneously.

The lower branch contains a 'sound' block in 'play file' mode. The block header indicates that the sound file 'Motor idle' has been selected. The volume has been set to 100% and the 'play type' has been set to 'repeat' (option 2) so the sound will play continuously until another block tells it to stop.

The upper branch contains a 'move steering' block in 'rotations' mode. Steering is set to zero (go straight), power is set to 50%, the number of rotations is set to two and the brake option is on. The upper branch also contains a 'sound' block in mode 'stop' which simply stops any sound which is currently being played.

So this program will continuously play the 'motor idle' sound until the robot has moved forward two rotations of the wheel.

2. Loop

Pay attention! Loops are important! This program starts (after the start block) with a loop block inside which there is a 'brick status light' block in 'on' mode with colour set to red (option 2) and pulsing turned off. The loop itself is in 'time' mode with the number of seconds set to 2, so the loop will repeat (and the brick light will pulse red) for two seconds. Then the brick status light is turned off by a brick status light block in 'reset' mode.

Now the program spits into two branches or 'threads' which will run simultaneously. The upper branch contains a loop block (loop 02) inside which is another loop block (loop 03). Inside loop 3 is a 'sound' block in 'play file' mode. The block header indicates that the sound file 'click' has been selected. The volume has been set to 100% and the 'play type' has been set to 'wait for completion' (option 0) which means that the sound will play once and that the program will wait for the sound to complete before continuing. Loop 03 also contains a wait block in 'time' mode with the number of seconds set to 1, so there will be a one second pause after the click sound stops. The loop 03 block itself is in 'count' mode with the number of counts set to 2, so the contents of the loop will be run twice and the robot will go 'click, pause, click, pause'. Once the program has exited loop 03 a move steering block in 'rotations' mode will make the robot move straight with 50% power for 1 rotation of the wheels. Loop 02 is in 'unlimited' mode meaning it will repeat until stopped by some other block.

Let's not forget about the lower branch or 'thread', which is running simultaneously with the loop above. The lower thread contains a wait block in mode 'touch sensor>compare>state'. The 'state' parameter is set to 'pressed' (option 1) so the block will continuously compare the actual state of the touch sensor with the 'pressed' state, ending the wait when they are equal, in other words when the actual state of the switch is (equal to) 'pressed'. The block also contains a 'measured value' output which is not used here. The last block is a 'loop interrupt' block which has only one mode and which will interrupt the loop named in the block's header (loop 02 in this case).

To summarize, this program first makes the brick's status light pulse red for two seconds, then clicks twice and moves forwards one wheel rotation, repeatedly clicking and moving until the touch sensor is pressed.

3. Switch

Pay attention! Switches are important! This program contains a loop block which is set to repeat forever (until the program is stopped by the user pressing the 'back' button on the brick). Within the loop block is a switch block in mode 'colour sensor>compare>reflected light intensity'. The 'compare type' is set to 'less than' (option 4) and the threshold value is set to 50%. So the switch block will test whether the measured reflected light intensity is less than 50%. If that condition is met or 'true' then the top branch of the switch will be run (notice the check mark above this branch). The top branch has a 'large motor' block in 'off' mode, which will switch off motor C (the right motor). The second large motor block will turn on motor B (the left motor) at 50% power, making the robot turn medium right. Since the loop is set to repeat forever the robot will turn right as long as the reflected light intensity is less than 50%, such as when the sensor is over a black line. The lower branch of the switch loop does the opposite, making the robot turn medium left if the sensor detects strong reflected light, such as when the sensor is over a white surface.

This program is in fact a simple form of 'line follower' program. The intention is that the robot will follow a black line that has been drawn on a white mat. This is a little difficult to do using only a single colour sensor. Imagine that the robot is moving along the line with the sensor over the black line, then the sensor will detect very little light being reflected. If the robot wanders off the line then the sensor will detect a strong reflected light intensity, but how useful is that information? The robot won't know whether it has wandered off the left side of the line or the right, so it won't know how to turn. So the trick is not to try to follow 'the line', it is to follow one edge of the black line, let's say the right edge. Now if the sensor detects that it is over white then we know the robot should turn left to find the edge again and if the sensor detects that it is over black then the robot should turn right to find the edge again.

This is what the above program does, it makes the robot turn left if the sensor detects a bright reflection and right if the reflected light is weak. It's a line follower - try it! (It works, but it's pretty inefficient.)

4. Multiple Switch

Note: this exercise assumes that the colour sensor is aligned horizontally so you can present different sides of the 'multi-colour cuboid' to it.

This program contains a loop block which is set to repeat forever (until the program is stopped by the user pressing the 'back' button on the brick).

Within the loop block is a switch block in mode 'colour sensor>measure>colour'. In this mode the switch can have more than two branches - it starts with two but you can add more branches by clicking the little '+' sign in the block.

  • The top branch of this switch block tells the program what to do if the sensor detects 'no colour' - it will continuously move straight forward with a power of 40%.
  • The middle branch tells the program what to do if the sensor detects 'blue' - it will continuously turn gently left (with the left motor at 10% power and the right motor at 40% power).
  • The bottom branch tells the program what to do if the sensor detects 'yellow' - it will continuously turn gently right (with the left motor at 40% power and the right motor at 10% power).
  • What if the sensor detects some other colour, like red? The top branch will activate since this is the 'default' branch, as shown by the black dot at the top of that branch.

5. Data wires

In this program the robot does not move and it is assumed that an object such as your hand will be brought towards the robot. This program contains a loop, inside which there is an ultrasonic sensor block set to compare distances in centimeters. The 'compare type' is set to 'less than' (option 4) and the threshold value is set to 8 (cm). Thus the block is testing to see whether the measured distance between the sensor and the reflecting object is less than 8 cm. The next item is an 'output' that contains the result of the comparison (a logical value, either 'true' or 'false'). A green data wire connects the output from this block to the loop control input - more on that later. The block also has an output for the measured distance in cm. A data wire is attached to this and its yellow colour indicates that it is carrying a numeric value.

The next block is a 'text' block which has only one mode: merge. Its function is to 'merge' or 'join' or 'concatenate' two or three pieces of text together and to output the result. In this case the block will join together the number received through the yellow data wire (which represents the distance to the block) and the text string " cm". The text string is called a 'string' because it is a string of characters. Note that the text string contains a space as well as the two letters. It's OK for the text block to receive a number on one if its inputs even when it is expecting text because the block is smart enough to know how to convert that number into a text string. The text block outputs the joined-up text through a data wire that is coloured orange to indicate that it is carrying text.

The next block is a display block in 'text grid' mode. It receives the text from the previous block through the orange data wire, clears the screen, displays the text beginning in position (5,6) on the screen's text grid which is 22 cells wide in the x direction and 12 cells tall in the y direction. The top-left text cell has coordinates (0,0) and the bottom-right cell has coordinates (21,11). Thus the text will appear somewhere near the center of the grid. The text colour is set to 'false' meaning 'black' and the font is set to 'Large' (option 2). In the 'Large' font each character occupies 2 text cells horizontally and 2 text cells vertically, so in this font the screen can display 6 rows of 11 characters each.

The loop in this program is set to repeat continuously until a certain logical condition is met, or 'true'. The loop control input is connected to the green data wire which, as explained previously, will carry the value 'true' when the ultrasonic detector detects that the distance to the reflecting object is less than 8 cm.

Once the loop exits (because it became true that the distance to the object was less than 8 cm) a display block in image mode displays the chosen image file 'Up' which depicts two eyes looking up. The brick's screen is first cleared then the picture is displayed with its top left corner in position (0,0) in pixels, i.e. in the top-left of the screen.

The final 'wait' block makes the program wait two seconds before ending. Without this block the program would end before you have time to see the eyes displayed on the screen.

To summarize, this program contains a loop which continually measures the distance to an object (assumed to be initially more than 8 cm away), then displays the measured distance on the brick's screen. The loop exits when the program detects that the distance to the reflecting object has become less than 8 cm, and an image of two eyes is then displayed for two seconds.

6. Random

The random block is in numeric mode and generates random integers between -70 and 70. The generated integer is output through the yellow data wire to a 'move steering' block which drives in a straight line with a random power between -70% (backwards) and +70% (forwards), the random power setting coming from the random block. Each movement continues for one second, and then the program pauses for 1 second. The 'forever' loop ensures that the robot will repeat the random move and pause pattern until the program is forced to stop by a press on the brick's 'back' button.

Continue on to exercises 7-11