Sample programs

We will now construct a number of programs in the visual programming language to demonstrate the functionality of the various blocks and how to correctly set them up in sequences.

Multiple segments

We will construct a program with multiple segments. We want the EV3 to say Hello, pause for a little while, say Goodbye (completely, without clipping the sound), and then end. Here is the sequence required:

Multiple segments

For each block, the value in parentheses is the value that you must configure for that block. Setting the sound block to the value 2 makes it play the Goodbye clip and setting clock to 5.0 makes it wait for 5 seconds. Wait blocks are italicized.

When you run the program, EV3 executes the blocks in the following order:

  • Sound block: EV3 starts playing the audio clip for the word Hello as specified by the value of 1 configured in the block. Execution is immediately passed on to the next block, since the sound block is an action block.
  • Clock: This is configured to wait for 5 seconds. This allows the Hello audio clip to finish playing. Once the 5 seconds pass, execution resumes and moves on to the next block.
  • Sound block: This is configured to play audio clip 2 that plays the word Goodbye. This is an action block so execution immediately moves on to the next block.
  • Clock: This is configured to wait for 2 seconds.

After the 2 seconds have passed, the execution reaches the end block, which terminates the program.

Simultaneous actions

In our second example, we modify the first one slightly by adding LED blocks after the sound blocks. Our aim is to make the LEDs flash orange and display an image of a tick mark when the EV3 says Hello and make it continue to flash orange and display an image of an X when the EV3 says Goodbye. Here is the program sequence (read it left to right and top to bottom, such that the Image (6) block comes after the Sound (2) block):

Simultaneous actions

Since sound is an action block, once the sound begins playing, execution immediately jumps to the next block that lights the LEDs with the color orange. LED is also an action block, so execution once again jumps to the next block, which is an image block configured to show image number 5 (a tick). The image block is also an action block, so execution jumps to the clock, which pauses execution for 5.0 seconds. While execution is paused, the audio clip (Hello in this case) plays to the end, the LEDs continue to flash on and off with an orange light, and the image of the tick mark is continuously displayed.

In fact, the LEDs will continue to flash orange until either the program reaches its end or another LED block is inserted that overrides it. This is a crucial feature of the visual programming language of EV3. Action blocks start actions that are meant to run forever unless superseded by another block of the exact same type. For example, the first image block displays an image of the tick mark until the second image block overrides it to display the image of an X.

After the first clock delay is completed, execution moves on to two action blocks, which play the Goodbye sound and display the X mark respectively. Meanwhile, the LEDs continue to flash orange because they have not been overridden by another LED block.

Button-activated greeting

Now, we construct a program that performs an action triggered by an input, in this case, the pressing of the Enter button. We use the button block, which is a wait block that pauses execution until the specified button is pressed. The program sequence is short and sweet.

Button-activated greeting

The program starts execution and immediately arrives upon the button block, which pauses execution until the Enter button is pressed. The program will wait forever if need be (well until the battery runs out) until the Enter button is pressed (or Back is pressed to exit the program prematurely).

Once Enter is pressed, execution resumes and moves on to the sound block, which plays Hello, made possible by the 1 second pause introduced by the clock immediately after it.

Sensor-activated greeting

The natural next step is to have an action triggered by a sensor. All of these examples will have the same format. When the program is executed, the LEDs start to flash green. The program will then wait for Enter to be pressed to proceed. It will then wait for an event on a single sensor, after which it will play the Hello sound and exit.

The touch sensor

We start with the simplest sensor, which is the touch sensor. The program sequence is as follows:

The touch sensor

As indicated by the touch block, the touch sensor must be attached to port 1 for this program to work. Run the program. The program will wait for Enter to be pressed. Once pressed, it will wait for the touch sensor button to be pressed and released; at which point, it will play Hello and exit.

Note that before Enter is pressed, one can press the touch sensor repeatedly without it having any impact. It is only when the touch block is executed that the EV3 starts waiting for the specified event (pressing and releasing the button in this case).

The color sensor (the REFLECT mode)

In the REFLECT mode, the color sensor waits until it is triggered by the strength of the reflected light (0-100) rising or falling below a specified level. The easiest way to test this is to connect the color sensor to port 3, place it in front of a mirror, and change its distance to the mirror. The closer the sensor is to the mirror, the more light is reflected back into it and the higher the measured value will be.

In the following first program sequence, the color sensor is set to trigger when the strength of the reflected light exceeds 50. In this case, moving the sensor closer to the mirror (from initially far away) will eventually trigger the event causing EV3 to say Hello.

The color sensor (the REFLECT mode)

The second program is set up to trigger the sound once the reflected light level falls below 50. To test this, place the color sensor right up to a mirror and slowly move it away.

The color sensor (the REFLECT mode)

The color sensor (the COLOR mode)

In the COLOR mode, the color sensor waits until it detects a specified color. For the following program sequence, the Hello sound plays once the sensor is brought close to a red colored object:

The color sensor (the COLOR mode)

The IR sensor (the PROX mode)

In the proximity mode, the IR sensor waits until the distance to an object (as measured by the sensor) rises or falls below a specified value. The easiest way to test this is to put your hand in front of the sensor and move it about. To say Hello when an object gets close, the program sequence is as follows:

The IR sensor (the PROX mode)

Conversely, to trigger the action when an object moves away, the program sequence is as follows:

The IR sensor (the PROX mode)

Note that if the condition is already met when execution arrives at the wait block, the execution is not paused. So, in the last program, if the sensor is placed such that there is no object near it (the sensor is reporting a value greater than 50), then when the program is run, the EV3 will say Hello the moment Enter is pressed. This is because the condition on the IR PROX block (that the value be greater than 50) will be true immediately and so the block will pass execution onto the next block.

In this mode of functioning, the wait block is behaving like a WAIT UNTIL statement. The block tests a condition and pauses the execution until the condition is met.

Motor control

Now that we are comfortable incorporating sensors into brick programs, it is time to do the same with motors. The principles are the same. In our first example, we use the medium motor block of type action, where we specify the direction of the rotation and the speed (five settings: zero, slow, medium, fast, and ultra-fast). We want to wait for Enter to be pressed; after which, the motor should turn at slow speed for two seconds. The program sequence for achieving this is as follows:

Motor control

Additional motor blocks (action) override the previous ones. So, if you want the motor to spin forward (CW for clockwise) for two seconds and then spin backwards (CCW for counter-clockwise) for the next two seconds, use the following program sequence:

Motor control

The large motor action block behaves identically to the medium motor action block with the exception that the large motor is connected to port D.

The motor rotation event

The medium motor has a wait block as well that pauses the execution when the motor angle reaches a specified angle. Of course, this only works if the motor is already rotating; otherwise, this block will pause indefinitely.

The program sequence to rotate the motor by 270 degrees clockwise is as follows:

The motor rotation event

The motor action block sets it rotating while the motor wait block waits for the rotation to exceed 270 degrees before it halts the action. This is a straightforward mechanism for getting the motor to rotate by a specified angle. The only downside is that there are only a few angles to choose from and none of them exceed 360 degrees. The motor cannot be rotated by more than one rotation using this technique. In most cases where larger rotations are required, one must sacrifice precision of the rotation and use the clock to specify how long the rotation should continue.

Detecting obstacles

It is now time to integrate the sensors, motors, and various other outputs in a single example of decent complexity. Here are the objectives:

  1. The program starts when Enter is pressed.
  2. The LED turns green.
  3. The large motor starts rotating at fast speed.
  4. When an obstacle/object is detected by the IR sensor, the motor stops.
  5. The LEDs turn red.
  6. A sound file is played that says Stop to indicate the obstacle.
  7. Finally, the LEDs turn orange.
  8. The motor spins backwards at a slow speed for 2 seconds.
  9. At the end of the 2 seconds, the program exits.

We are essentially simulating a robot that moves forward until it reaches an obstacle. It stops, signals the obstacle, reverses slightly, and then comes to a final stop. Study the program carefully to understand how the blocks are placed to achieve the objectives stated previously:

Detecting obstacles
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.190.232