3.2.3
Dancing Car
Dancing Car
Figure 1. Self-driving vehicles from Waymo (upper left), BMW (upper right), and Tesla (bottom)
What to expect
Today we will create a vehicle that moves autonomously, without any user input. You have probably heard about autonomous, a.k.a. self-driving vehicles. Many companies are exploring the development of autonomous vehicles: Tesla, Waymo, BMW, General Motors, Ford, and many others.
Connections
Two weeks ago we built the MOVE motor car and programmed it to perform basic movements. After it was programmed, its movements were pre-determined and fixed. For example, If it had encountered an obstacle, it would not have been able to navigate around it. Last week we turned the MOVE motor car into a remote control vehicle, and you were able to remotely change its movement without re-programming the vehicle. This week we will program the car to receive an input signal (sound) and respond by moving autonomously, without any user input. For the next several weeks, we will explore additional ways to make the car move autonomously.
Materials
Kitronik MOVE motor car with micro:bit
USB cable
Computer
Something to play music (e.g. your phone)
Instructions
Today we will program the MOVE motor car to receive a sound input through its microphone and respond by changing the way it moves. This will require two parts. In Part 1, we will investigate the typical sound levels that the microphone on the micro:bit receives. In Part 2, we will use that information to program the micro:bit to respond by moving the wheels of the vehicle.
Part 1. Determine the typical range of sound levels with serial monitoring
Figure 2. Serial monitoring menu
Start a new Makecode project and call it “Dancing Car” or similar.
To start, we need to know the typical sound level that the car is “hearing” (receiving as input through the micro:bit’s microphone). Then we will use the sound level to program the car’s micro:bit. We can observe the input sound level using a feature called serial monitoring.
The serial monitoring blocks are located under the “Advanced” drop-down menu. Click on the Advanced drop-down menu to see the Serial icon, shown below.
Figure 3. Serial monitoring code to monitor sound level through the micro:bit’s microphone
2. Create a code that looks like the image below. The “serial write line” block is located under the Serial menu, and the “sound level” block is located under the Input menu.
Figure 4. Button to show live serial monitoring data
3. Connect the USB cable from your computer to your micro:bit, and set the micro:bit in front of you with the microphone side facing up. The microphone is labeled on the micro:bit. It is on the side opposite from the A and B buttons and the array of LEDs.
Download the code to your micro:bit. Do not disconnect the USB cable.
On the left side of the screen, click on the “Show data Device” button (not the Show data Simulator button).
Figure 5. Serial monitoring of sound level received by the micro:bit microphone
4.The screen will show a live readout of the sound levels received through the microphone on the micro:bit, as shown below. The graph at the top of the screen is a plot of sound levels over time, and the rapidly-changing numbers at the bottom of the screen show the most recent sound level. The micro:bit measures sound levels on a scale of 0 (quietest) to 255 (loudest).
5. Observe the sound level plot. What happens to the sound level when you whisper from several feet away? When you speak loudly from six inches away or clap your hands? When you play music from your phone? Try to determine the typical range (minimum and maximum) of sound levels when you play music from your phone from a few feet away. Make a note of this range, because you will use it to program your micro:bit in the next section.
6. When you are done with the sound level plot, click “Go Back” to return to the program screen. Keep the USB cable connected, because we will create a different program for the micro:bit in the next section.
Part II. Program the Dancing Car
In this section, we will make the car “dance” by making it move in different ways depending on the sound level that it “hears” as an input.
There are four different “dance moves” (in addition to “stop”) that our car can make: forward, reverse, turn left, and turn right. We will use these four moves and match them up with the sound levels that you observed in the previous section. The car will move in different ways depending on the input sound level.
Think about the range of sound input levels that you observed in the previous section, and decide how you will match up the sound levels with the car’s dance moves. For example, if you observed a typical sound level range from 50 to 90, you might decide to match the sound levels to the car moves like this:
Figure 6. MOVE Motor extension
If you don’t already have the “MOVE motor” extension in the MakeCode menu as shown below, add it by clicking on the gray Extensions button, then search for “MOVE Motor”, then click on the tile labeled “kitronik-move-motor”. You should now see the MOVE Motor extension in the menu.
Figure 7. Example dance code. Your sound levels and dance moves may vary. If you are stuck, here is a link to the code: https://makecode.microbit.org/_21x7hEibDiDP
2. Create a new program similar to the one below. Substitute your own sound level values and dance moves based on your choices from the previous steps. The teal-colored blocks are in the Logic menu, purple blocks are in the Input menu, and green blocks are in the MOVE Motor menu.
3. Download the code to your micro:bit, and unplug the USB cable.
4. Insert the micro:bit into the car with the A and B buttons facing forward.
5. Make sure the car has batteries installed. Then set the car on the floor, and turn the car’s power switch to ON.
6. Play some music on your phone (or just clap your hands) and watch your car dance!
7. If you’re not satisfied with the way your car is dancing, revise and re-download the code.
Optional Extension
You could use any of the micro:bit’s inputs to make the car dance. Try substituting light level for sound level in the code, and use your phone’s flashlight to control the light level input. The micro:bit uses the array of LEDs as its light sensor. Hint: we used serial monitoring to find the typical range of input sound levels. You might need to do the same with light levels.
Wrap-up
Today we tested the input signal received by the micro:bit’s microphone using serial monitoring, then programmed the micro:bit to respond to the input signals in specific ways. After it was programmed, the car used the input signals to move autonomously, with no further input from the user.
What are some practical uses for autonomous vehicles?
Would a real-world autonomous vehicle need to use sound as an input? What about light?
What other sensors would be useful for an autonomous vehicle?
Next Activity
Next week we will adapt the MOVE motor car to be autonomous in a different way. We will use the car’s ultrasonic sensor, a.k.a. distance sensor, to tell the car if it is about to run into an obstacle. The car will react by turning and driving in a different direction. The car will roam around the room while avoiding walls, furniture, other vehicles, and any other obstacles in front of it - much like a Roomba!