Beyond Basics Ex 1-6

To see how each of these exercises can be solved in the standard Lego EV3 programming environment (EV3-G) click HERE. To see how each of these exercises can be solved in EV3 Python click HERE.

Exercise 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. A thread is a part of program code that can run independently and at the same time as other parts of the program. You can learn more about threads in the EV3 Basic manual.

Objective: 

The robot will continuously play the Lego 'Motor idle' sound until the robot has moved straight forward for two wheel rotations at speed 50.

Solution:

A good way to make the standard Lego EV3 images and sounds available to EV3 Basic is described on this page. Assuming you have followed those instructions and therefore have downloaded all the standard sound files into a folder called 'Sounds' inside the 'prjs' folder on the brick (which is like the 'home' folder), then you can easily incorporate these sounds into your EV3 Basic programs.

Thread.Run = PLAYSOUND

Sub PLAYSOUND

    While "true"

        Speaker.Play(100, "Sounds/Motor idle") 

        'brick file/folder names are case-sensitive!

        Speaker.Wait ()

    EndWhile

EndSub

Motor.Move("BC", 50, 720, "True")

Speaker.Stop ()

Notes:

Exercise 2: Loop

Objective:

The 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.

Solution:

EV3.SetLEDColor ("RED", "NORMAL")

Program.Delay(2000)

EV3.SetLEDColor ("OFF", "NORMAL")

Thread.Run=CHECKFORPRESS  'Launch the thread CHECKFORPRESS

Sub CHECKFORPRESS  'this subprogram will be run as a thread

    While Sensor.ReadPercent(1)=0  'touch sensor on port 1

        Program.Delay(10)

    EndWhile

    Program.End()  'stops the program

EndSub

While "True"

    For i = 1 To 2

        Speaker.Play(100, "Sounds/Click")  'this sound file must exist here

        Speaker.Wait ()   'wait for the sound to finish playing

        Program.Delay(1000)

    EndFor

    Motor.Move("BC",50,360,"True")

EndWhile

Notes:

Exercise 3: IF structure (called 'Switch' in the Lego software)

Objective:

This will be a simple 'line follower' program using a single color sensor. 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 color 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. In other words...

Solution:

'Set the color sensor on port 3 to mode 0: 'reflected light'

Sensor.SetMode(3,0)

While "True"

    If Sensor.ReadPercent(3)<30 Then   'weak reflection so over black line

        'medium turn right

        Motor.StartSync("BC",50,0)

    Else   'strong reflection (>=30) so over white surface

        'medium turn left

        Motor.StartSync("BC",0,50)

    EndIf

EndWhile

Notes:

Exercise 4: Multiple Switch

Note: this exercise assumes that the color sensor on port 3 is aligned horizontally or pointing upwards so you can present differently colored sides of the 'multi-color cuboid' to it.

Objective:

Solution:

Sensor.SetMode(3,2)  'set color sensor on port 3 to mode 2: detect color

While "True"

    code=Sensor.ReadRawValue(3, 0)

    If code =4 Then  'yellow

        Motor.StartSync("BC", 40,10)

    ElseIf code =2 Then  'blue

        Motor.StartSync("BC",10, 40)

    Else  'no color detected or a color other than blue or yellow

        Motor.Start("BC",40)

    EndIf 

EndWhile

Notes:

Exercise 5: 'Data wires'

In the icon-based Lego EV3 programming environment it is often necessary to use 'data wires' to transfer data between programming blocks. Fortunately, this is not meaningful or necessary in a textual programming environment like EV3 Basic.

Objective:

In this program the robot does not move and it is assumed that an object such as your hand will be brought towards the ultrasound sensor on the robot (on port 4). A loop continually measures the distance to an object (assumed to be initially more than 8 cm away), and 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.

Solution:

This program assumes that have you have downloaded all the standard Lego sound and image files to the brick in accordance with the instructions on this page.

Sensor.SetMode(4,0)  'will set the US sensor on port 4 

'to mode 0: measure distance in mm (not cm)

While Sensor.ReadRawValue(4, 0) > 80   'cm converted to mm

    LCD.StopUpdate()   'eliminate screen flicker

    LCD.Clear()

    LCD.Text(1,30,60,2,Sensor.ReadRawValue(4, 0)/10 + " cm")

    LCD.Update()

    Program.Delay(100)  'so the display doesn't change too frequently

EndWhile

LCD.BmpFile(1,0,0,"Images/Up")  'display in color 1 (black) at (0,0)

Program.Delay(2000)

Exercise 6: Random

Objective:

The program will first generate a random integer between -70 and 70 inclusive. The robot should drive forward at this speed for one second, then pause for one second, then generate a new random number and repeat forever.

Solution:

Small Basic has a function Math.GetRandomNumber(maxNumber) which generates a random integer between 1 and the specified maxNumber (inclusive).

While "True"

    'generate a random integer between 1 and 141 inclusive

    rand=Math.GetRandomNumber(141)  

    rand=rand-71   'the random number is now in the range -70 to 70

    Motor.Start("BC", rand)

    Program.Delay(1000)

    Motor.Stop("BC","True")

    Program.Delay(1000)  

EndWhile

Notes:

Let's compare this solution with the official EV3-G solution to the same problem. The official EV3-G solution has no comments so before we make the comparison let's get rid of the comments and make the program as short as possible:

While "True"

    Motor.Start("BC", Math.GetRandomNumber(141)-71)

    Program.Delay(1000)

    Motor.Stop("BC","True")

    Program.Delay(1000)  

EndWhile

Here's the official EV3-G solution from the EV3 Education software:

So, which would you say is the neatest-looking solution??

You are now ready to tackle Beyond Basics exercises 7-11.