In this lesson, we will use LED and sound sensors to judge if the ambient noise is too high.
After creating a new project, click on the Simulation icon on the left side of the page to enter the Simulation page.
Connect the LED module to D0, the Sound Sensor to A0, and the VCC and GND to 3.3V and GND respectively. The sound sensor can sense the noise level in the environment. When the reading value is higher than the threshold, the LED will light up.
Click the Create variable button under Variables category to create two variables: threshold and soVal.
Read the value of the sound sensor and assign it to the variable soVal, then print it in Debug Monitor.
Connect the Bluetooth and click on Flash button to compile and download the code. Remember the value under normal conditions, then play a piece of music or slap the microphone, and you can see that the value of sound sensor decreases with the louder sound.
Now, set a value slightly less than the current environment based on the value read in the previous step.
A judgment condition is set by using the if do block and the Comparison Operator ( = ) block to judge if the soVal is less than the threshold.
If the value of the sound sensor (soVal) is less than the threshold set, set the LED module to 1 to lighten it.
Otherwise, set the LED module to 0 to turn it off.
Drag the slider to simulate the value of sound sensor and you can find when the value read is less than 1500, the LED light up; otherwise, it will turn off. Now save your project, connect the Bluetooth and click Flash to compile and download the code.