Download Print this page

Sparkfun Electronics Inventor's Kit Manual page 31

For micro:bit

Advertisement

Note: The full sized breadboard power rails have a break down the middle. If you end up using the
lower half of the power rail you will need to jump between the upper end and lower end.
Running Your Script
Either copy and paste, or re-create the following code into your own MakeCode editor by clicking the
open icon in the upper right-hand corner of the editor. You can also just download this example by
clicking the download button in the lower right-hand corner of the code window.
Note: You may need to disable your ad/pop-up blocker to interact with the MakeCode programming
environment and simulated circuit!
Code to Note
Let's take a look at the code blocks in this experiment.
If you are having a hard time viewing this code, click on the image above to get a better look!
On Start
In previous experiments you have only used the
The
block is a block of code that only runs once at the very beginning of your program. In
On start
this program we use it to set a calibration value once, and then compare the changing value in the
forever loop. This is a great spot for code that you only want to run a single time.
is a calibration variable. Your micro:bit takes a single reading of the light sensor in
calibrationVal
the
block of code and uses this value to compare against the
on start
the
loop. This value doesn't change in the
forever
To update this value you can press the RESET button on the back of your micro:bit or power cycle
the board.
If/Else
If the light value variable that is constantly being updated in the
calibration value minus 50, it is dark and the LED should turn on. The (-50) portion of the
a sensitivity value. The higher the value, the less sensitive the circuit will be; the lower the value, the
more sensitive it will be to lighting conditions.
The
block is a logical structure. If the logical statement that is attached to it (
if
) is true, then it will execute the code blocks inside of the
calibrationVal ‐50
false, it will execute the
micro:bit will turn on the LED on pin 16; else (if the room is bright), it will turn the LED off using
a
block.
digital write
Note: The logical operator blocks and the math block used to build the logical statement. You can
find them under the Logic and Math blocks respectively.
blocks. In this case if the statement is true (the room is dark), then the
else
block, which loops your code forever.
forever
block, as it is set in the
forever
forever
variable in
calibrationVal
on start
block is less than the
block is
if
item < 
. If that statement is
if
block.

Advertisement

loading
Need help?

Need help?

Do you have a question about the Inventor's Kit and is the answer not in the manual?