Programming and note
Set to:
You can use this
set to
block to set the value for a variable that selected from a list
of the variables you create. You can create variables by click Variables
→
Make a Variable and
then naming it.
Analog write:
Just like the
analog read
block
,
the
analog write
block deals with a
range of values, but instead of reading a pin as an input, the
analog write
block outputs
an analog value to a pin. In this case, we use this block to set the brightness of the LED, but
it could be a tone for a buzzer, a motor speed and etc.
Map:
This is a mathematical method used to map values from one interval to values from
another interval.The mapped value range will be reduced or enlarged by a certain propor-
tion. Here we need to map the potentiometerValue(value between 0 and 1023) to value
(between 0 and 255) and write it to P1 port. For example, if the value of potentiometerVal-
ue is 240, then after mapping, we get the value is 60 and write it to port P1. The reason we
use this mapping function is that the LED has a brightness level of 256 (0-255). When the
brightness level is greater than 255, the brightness of the LED will not change.
Download the program to micro:bit to see what happens.
Toggle the potentiometer and you will find that the LED light will become
brighter or dimmer depending on the position of the potentiometer. When
you turn the potentiometer to the far left, the LED light goes out, and when
you turn to the right, the LED light is the brightest.
Result
Since the brightness of the lamp can be adjusted in this way, is the sound volume of the
stereo adjusted in this way?
24