This project makes a fun sound level bar meter by using the LED screen on Micro:bit, it monitors the environment sound, indicates the sound level by showing 0-5 levels, the challenge part makes it more interesting by turning it into a dynamic noise alarm, using adaptive max level in real time. Goals:
Array/list concept and basic usage
Learn using "images array" to make simple animations as a useful visualization of data level
Concept and practice of "data scaling and mapping"
START A BIT > Projects Library > This page
This is the demo for the basic functions. Please go to the Challenge chapter for the demo of challenging result.
This project uses 1 Micro:bit board with a USB cable, nothing else needed.
When powering up, the Micro:bit starts monitoring the environment sound level continuously, and show the level bar on LED screen, indicating how much level of the sound, the monitoring and visualization is almost real time, should not have obvious delay or mismatching.
[ ] Simple: 30-60 minutes
[x] Moderate: 1-2 hours
[ ] Challenging: more than 2 hours
The estimation bases on average situation without unexpected troubleshooting, assuming that the student meets prerequisites. It only indicates rough time needed to complete this project, but not about technical difficulty, not covering discussion and sharing time. Here is a possible breakdown:
5 minutes: Understand requirement and solution, collect materials
10-15 minutes: Task 1: Monitor Environment Sound Level
10-15 minutes: Task 2: Make a List of Images, Show a Specific Image by Button A/B
30-45 minutes: Task 3: Scale "Sound Level" Values to Map Valid Index of Array
The challenge topic is not included in effort estimation, it could take extra 45-60 minutes for that part.
One should have prior knowledge
Micro:bit V2 board: Know what they are, better to have prior experience with it
MakeCode by Microsoft, the programming tool, better to have prior experience using it with Micro:bit
1x Micro:bit V2 board , with USB cable
The result of this block is a number between 0 (low sound) and 255 (loud sound) which tells how loud the sounds are that the microphone hears.
Input ->
Use "Serial Write" to visualize the real time changing of sound level.
Adding a small delay helps slowing down a little bit of the data, so to observe clearer.
Solution 1
Solution 2: Use "Loops" block
After "Downloading", click "Show data Device" to visualize the real time data.
Try to shout, how big value can you catch?
Use this to quickly create an array with the default name as "list", put this to "on start".
Then fill in the array using "create image", add more elements of array by clicking the "+" icon.
Arrays ->
Images ->
Click "+" to add more elements
When >3, it becomes vertical
The array of 5 images
The array has index starting from 0, use the valid index to find the element of the array, please be careful to keep the index as valid! Don't go "out of range".
Arrays ->
Use this to "show image", you need to fill in an "image", but not the array!
Images ->
It's smart to create a variable "index", and temporarily, we use button A/B to change the "index" value for testing this task. And it's good to add "index" to "serial write" for debugging.
What do you find? What happens if "index" goes "out of range"?
This is the last step of this project, we want to set the index according to the sound levels. Basically, this is what it looks like, for junior students, it's OK to just copy this implementation and make it work.
The images array could be changed in its size, it's smart and more professional to do like this:
How does it work? Think and discuss, it's challenging, but not so hard to understand the basic logic.
As you might see, although in theory, the max value of "sound level" is 255, but even if we shout loud, it's very hard to see higher values than 200.
If we always map from "low 0 high 255", it makes the bar leveling jumping not so actively, how can we make it more "sensitive"?
Try out a smaller "high" value, download and test
What differs in actual?
Any "out of range" risks?
Based on the basic requirement, let's make the sound sensitivity more intesting and useful
Add adaptive max_sound_level, we will expand the "high" end of mapping to be a new "max" level of sound that we listen from the environment after 10 times of continuous hitting the old "max"
Press "Logo" to reset the max_sound_level, i.e. we reset the sound sensitivity
Below are some hints to make it happen, but not in every detail, and not as step by step, you need to figure out the full working program.
In this challenge, the sound_level is referred multiple times, and there is parallel handling of this value, it's risky to capture the sound level again and again in different places, because whenever you read the sound level, it's always the latest
It could bring "sync" problem for parallel threads
It stresses the system by unnecessary extra reading from sensor
It makes confusing logic in the programming
We need to remember the last max level, so to judge if current sound level is bigger, so to update the new "high" value in mapping.
It makes better sense to update the max_level only after some amount of tolerance, but not change it immediately, to reduce interference from strange sudden noise.
The main logic is
If "sound_level > max_level" and "hitting_count" reaches a threshold, say 10, then we update the "max_level"
It's smart to add some debugging information by "serial write"
Click to find the copy if you need
If you are careful, there is a new "constrain" used before "map", why? What happens if we don't add this?
Stand in front of the class, share with your group what you have made, please cover important points
Tell what it is your product
Demonstrate how it works
Explain how it works
Share with your group about
What troubles you met
How you analyze
How you get help
How you solve it
What you have learnt
Find out working copy as a MakeCode shared project.