# Hummingbird Duo and MakeCode (Part 2)

#### Module 7: While Loops with the Distance Sensor

The Hummingbird distance sensor emits infrared waves. If there is an object near the sensor, infrared waves bounce off the object and return to the sensor. The angle of reflection is used to measure how far away the object is (more info here).

#### Exercise 7.1

Attach the distance sensor to the Hummingbird board and use it to control a single color LED.  How does the brightness of the LED change as you move an object closer to the distance sensor?

#### Important Note

The distance sensor can detect objects roughly 8-100 cm from the sensor; it works best in the range of 20-60 cm.

#### Exercise 7.2

Write a script that turns on a vibration motor when an object is close to the distance sensor. Otherwise, the vibration motor should be off. What is an appropriate threshold for the distance sensor? You may need to experiment to find a good threshold.

An if then else uses a Boolean block to choose one of two possibilities. You can also use Boolean blocks in loops, such as the while do block (Loops menu) in MakeCode. The while do block uses a Boolean block to determine how many times the blocks inside it will be executed. The blocks inside the loop will be repeated as long as the Boolean block is true. When the Boolean block is false, the program will move on to whatever block is below the while loop.

For example, the loop below will blink a tri-color LED as long as nothing is close to the distance sensor. When the value of the sensor is greater than or equal to 20, the LED will stop blinking and turn green.

#### Exercise 7.3

What will happen when you run the code shown below? Make a hypothesis, and then test it.

#### Exercise 7.4

Create an alarm to detect people (or cats) approaching your computer! When a someone is nearby, use lights and motors to alert you to their presence. The lights and movement should continue until you turn the knob sensor clockwise. You will need to reset the knob sensor to the counterclockwise position before using the alarm again.

#### Exercise 7.5

Create a simple version of whack-a-mole. Use two servos as your “moles.” Each servo will correspond to one sensor: servo 1 to the distance sensor and servo 2 to the light sensor. The program should randomly select a servo and turn it to 90° to make the mole pop up. The mole should stay up until the user triggers the corresponding sensor. For example, the light sensor servo should stay at 90° until the user covers the sensor. Then the servo should move back to 0°. After the servo returns to 0°, add a random delay of 1-3 seconds before another mole pops up. After you have this basic game working, how can you add more moles? What other features can you add?

#### Important Note

For this section, you will need two micro:bits.

If you have two or more micro:bits, they can communicate with each other! This means that you can create robots that interact with one another. To do this, you will use the blocks in the Radio menu.

To send radio messages to one another, two (or more) micro:bits must be set to the same radio group. In the on start block for each micro:bit, use the radio set group block to assign both micro:bits the same radio number. Here, the radio group number is 120.

A micro:bit can send a message using the radio send string block or the radio send value block. Assume that for the script below, the Hummingbird light sensor is attached to port 1. When the light sensor detects that it is dark, the micro:bit sends the string “dark.” Otherwise, it sends the string “light.”

A second micro:bit can listen for the messages sent by the first one. If the first micro:bit is using radio send string, then the second should use the on radio received receivedString to listen for messages. When it receives a string, it can check to see which of the two possible messages it has received. The script below turns on an LED when it receives the message “dark.” The LED turns off if it receives the message “bright.”

#### Exercise 8.1

Try out the scripts shown above with your micro:bits. The block with the quotation marks can be found on the Text menu under Advanced. The receivedString block is on the Variables menu. If you have two micro:bits but not two Hummingbirds, just move on to the next exercise.

#### Exercise 8.2

Program one micro:bit to send a different radio message when each combination of its buttons (A, B, A+B) is pressed. Program a second micro:bit to respond to these messages. Each radio message should cause a servo connected to the second Hummingbird to move to a different angle.

#### Exercise 8.3

Use the accelerometer in one micro:bit to control the speeds of two gear motors attached to Hummingbird connected to a second micro:bit. You can use the code below as a starting point.

• The first micro:bit is sending two accelerometer values. Because it is sending two values, it calls them “X” and “Y” so the robot that receives them knows which is which.
• The second micro:bit should make the two motors move in the same direction when it receives an “X” value and in different directions when it receives a “Y” value. The on radio received block shown received both the name and the value sent by the first micro:bit.
• You can use your work to make a simple rover that is controlled by the first micro:bit! This tutorial may be helpful for building a rover.