Beyond Basics, Ex. 7-11

7. Sensor blocks

These two loops will run simultaneously since they both begin with a start block.

The top loop contains a colour sensor block in mode 'measure>ambient light intensity'. The measured light intensity will be output through the yellow data wire to the 'move steering' block. This block is in mode 'on' with steering set for a hard right turn. The power of the motors is determined by the output of the colour sensor block. So if the sensor does not receive much light then the robot will turn very slowly and if the sensor detects a lot of light (perhaps because you are shining a torch at it) then the robot will turn fast. The loop is set to run continuously, until interrupted by another block or by the forced termination of the program.

The lower loop is also set to run continuously. The touch sensor block is in 'compare>state' mode. The actual state of the sensor is to be compared with the 'pressed' state (option 1) and if the two states are equal (in other words, if the switch is pressed) then a 'true' value will be output through the 'compare result' output. The 'measured value' output is not used.

The switch block is in logic mode so it has two branches, one of which will be run according to whether the switch's logic input receives the value 'true' or the value 'false'.

  • If the touch sensor switch is pressed then the top branch will run and the motors will be stopped, overriding any instruction in the top loop to make the motors turn. The brick status light block is in 'on' mode and will make the light come on in red without pulsing.
  • If the touch sensor switch is NOT pressed then the bottom branch will run and the brick's status light will be reset, making it turn off, and then the program will pause for one second.

To summarize, when the touch sensor is NOT pressed the top loop is able to make the robot turn to the right at a speed that depends on the ambient light intensity. If the touch sensor IS pressed then the robot will stop moving for as long as the switch is pressed, and the status light will light up in red.

8. Text

This program is like program 5, but simpler. The ultrasonic sensor block has its mode set to measure distance in centimeters. The measured distance is output through a data wire to a text block which will merge (join) that number to the string " cm" and then output the resulting text string to a display block. The display block is in 'text grid' mode. It will clear the screen and display the input text string in grid position (5,6) which is roughly at the center of the text grid. The text will be displayed in black in the 'Large' font (option 2). Everything is enclosed in a 'forever' loop so the display will be continuously updated with the current distance reading of the sensor.

9. Range

The ultrasonic sensor block has its mode set to measure distance in centimeters. The measured distance is output through a data wire to a range block which has its mode set to 'inside'. The input 'test value' from the sensor is tested to determine whether it is within the range set by the block's 'lower bound' and 'upper bound' values. The result of the test (a logical value 'true' or 'false') is output to a switch block.

  • If the switch block receives the logical value 'true' (meaning that the distance measured by the sensor IS within the range 10-20 cm) then a 'move steering' block in mode 'on' will make the robot move forward in a straight line at 50% power.
  • If the switch block receives the logical value 'false' (meaning that the distance measured by the sensor is NOT within the range 10-20 cm) then the 'move steering' block in mode 'off' will make the robot stop moving.

In practice this program can be used to make the robot follow an object that is moving away from it - it will move forwards towards the object as long as the object is between 10 and 20 cm in front of it but will stop moving if it gets closer than 10 cm, thereby avoiding a collision. It will also stop moving if the leading object is more than 20 cm in front of the robot.

10. Maths – Basic

This program calculates how many wheel rotations are necessary to make the robot move forward 50 cm, then it makes the robot move, then it calculates the speed of the robot during the motion. Each time a robot wheel turns through one rotation the entire circumference of the wheel rolls along the ground. The diameter of the EV3 rubber tires is about 5.6 cm so the circumference is 5.6 cm * π = 5.6 cm * 3.14 = 17.6 cm. So to find the number of rotations necessary to move the robot forward 50 cm we must divide the 50 cm by the distance moved in each rotation of the wheel (equal to the circumference of the wheel) which is 17.6 cm. The math block is set to do the division for us and to output the result through a data wire to a 'move steering' block that will move the robot forward in a straight line at 40% power for the correct number of rotations. Before the robot moves, a timer block in reset mode starts timing the motion. It's possible to have up to 8 timers working at the same time so it's necessary to give each timer an ID number - our timer has ID '1'. Once the robot has finished moving a timer block in 'measure>time indicator' mode outputs the measured time to a math block in 'division 'mode which divides the distance (50 cm) by the time and outputs the result to a rounding block. This block is in 'to nearest' mode which will round the decimal number to the nearest integer. This integer is output to a text block which merges the number with the text string " cm/s" and the joined text is passed to a display block which displays the text in position (5,6) on the text grid, in black and in the 'Large' font.

11. Gyro - rate

The gyro sensor block is in 'measure>rate' mode. The measured rate of turn, in degrees per second, is output through a data wire to a display block. The display block, which is in 'text grid' mode, receives the turn rate number and displays it at position (8,6) on the text grid (near the center) in black in 'Large' font (option 2). The program pauses for 0.5 seconds before getting another reading. The forever loop ensures that the reading is continuously updated every 0.5 seconds.

Continue on to exercises 12-18