sensor reading (
current_surface_reading
).
Set To
To store a value or piece of information in a variable you use the
set _______ to
block. This allows you to select
a variable and set it to a value or block of your choice. In this case, the line following sensor value connected to pin
P1
. We do this once in the
on start
block to get a base reading from the sensor so that our micro:bit remembers
the value of the black line. This calibrates the sensor for the micro:bot. Pressing button A while the program is
running will also save the line sensor reading again.
Analog Read Pin
To get that base reading from the line sensor we use the
analog read pin __
block to get an analog value (
0
-
1023
) from the line sensor.
1023
is totally black and
0
is totally saturated white. Your surface will be something
probably in between.
If Condition Statements
To compare the current reading of the line sensor to its known baseline value that was captured and stored in the
on start
block, we use an
statement block. The
if
block accepts a logical statement. If that statement
(
current_surface_reading ≥ black_line - 100
) is true, then the
if
block runs then "then" section of code. If
that statement is false, then the "else" section of the block is run.
You may be asking yourself why subtract
100
from the
surface
value. Well, that number is a sensitivity value.
The sensor reading fluctuates as the robot moves around and when the motors are running. By subtracting
100
from the original surface variable, we give the change in lighting a "wiggle room" or tolerance of
100
. If you want
your sensor to be more sensitive, you would make the number smaller. If you need it less sensitive, make it bigger.
To debug, we'll send serial to the USB before the condition statement and animate the LEDs of the micro:bit within
each condition statement
show icon _____
(or
show leds _____
) block to help visualize when the micro:bot
moves forward, back, or to the left.
What You Should See
Serial Output
Heads up!
To view the serial output using the Chrome browser and the MakeCode's serial plotter and
terminal, the micro:bit will need to have
0249, 0250 or higher
. To update, make sure to follow these
instructions from micro:bit's support to display live serial data MakeCode console.
You can pair, upload code to the micro:bit, and view the output on the MakeCode console without having to
drag and drop the file to the micro:bit after updating the firmware. For more information about using the
WebUSB feature on MakeCode, make sure to check out the instructions provided by micro:bit support.