Welcome to Tiny:bit Robot Car repository
当製品の日本語版マニュアル資料は、こちらからご取得ください。
8.Voice control light
Learning goal:
In this lesson, we will learn how to read sound sensor data, and switch the light color based on the detected data through Python programming.
Introduction of Sound sensor principle:
The role of the sound sensor is equivalent to a microphone, which can be used to receive sound wave vibration, so as to detect changes in the current ambient sound, but it cannot measure the intensity of noise. Built-in condenser cylindrical microphone that is sensitive to sound, sound waves can vibrate electret films in microphones, causing the capacitance to change, thereby generating a tiny voltage corresponding to it.
The position of the Voice sensor module in the robot.
! ! ! Note: In this experiment, we need to install the jumper cap in the position shown below.
Code:
Please use the MU software to open the Voice control light.py file we provided.
1) Import the library needed for this lesson from micro:bit, display is used for dot matrix display, Image calls the built-in image, pin12 is the pin of the body colorful lights, neopixel drives the body colorful lights, and tinybit controls the car.
display.show (Image.HAPPY): Display a smile on micro:bit dot matrix.
np = neopixel.NeoPixel (pin12, 2): Initialize the body colorful lights. The first parameter is the pins connected to the lights, and the second parameter is the total number of RGB lights.
np.clear (): Clear the body colorful lights.
tinybit.car_HeadRGB (0, 0, 0): Clear the head RGB searchlight.
item = 0: Declare item as a global variable, and initialize it to 0.
Code as shown below:
2) voice = tinybit.getVoicedata():Read the data of the sound sensor and save it to the variable voice.
If the variable voice> 100, it means that the light needs to be switched. First, a random number of 1 to 6 is generated and stored in the variable item. Then the color of the front RGB searchlight and the body colorful light are randomly set.
Code as shown below:
3) Determine the value of item. Different values display different patterns on the micro:bit dot matrix.
Code as shown below:
Programming and downloading:
1.You should open the Mu software, and enter the code in the edit window, , as shown below.
Note! All English and symbols should be entered in English, and the last line must be a space.
2.You can click the “Check” button to check if our code has an error. If a line appears with a cursor or an underscore, the program indicating this line is wrong.
3.Click “REPL” button,check whether the tinybit library has been downloaded. If not, please refer to the [preparation before class]---> [Python programming]
4.Click the “Flash” button to download the program to micro:bit board.
If the program is wrong or the experimental phenomenon is wrong after downloading, please confirm whether you have downloaded the Buildingbit libraryhex file we provided to the micro: bit board.
For the specific method of adding library files, please refer to 【1.Preparation before class】---【Python programming】
Experimental phenomena:
After download is complete, open the power switch. Micro:bit will display a smile on dot matrix, we need to put robot car on the desk.When we Shooting table, the lights of the robot will be change color randomly.
