The receiver code contains a few more blocks than the transmitter. We'll go over things to see why.
Download
Download the Receiver code into the second micro:bit per the same method used on the previous page to load the transmitter code.
Click Edit in the upper right corner of the code display to get into the MakeCode editor. You can then save the program. Save it as Receiver.
What the Program is Doing
This code has two new sets of blocks:
-
strip
blocks work with NeoPixel LED strips via the NeoPixel Extension -
crickit
blocks work with the Adafruit Crickit robotics board via the Crickit Extension
If you load the code via the green button above, MakeCode will load those extensions for you. They provide two new block groups, Crickit and NeoPixel, which provide robotics and light blocks respectively.
In on start
, the code sets things up. It sets the radio group
just like the transmit code. It also sets up a NeoPixel strip with 30 LEDs. Pin P16 is the pin which does NeoPixels for micro:bit on Crickit. We set the whole strip to black which turns all the LEDs off. Then we set two variables, position
and light
, for later use.
The other two loops work with each other to process your radio code:
- the
forever
loop acts on the values ofposition
andlight
to move a servo or toggle the NeoPixels. - the
on radio received
loop toggles the variablesposition
andlight
if the right radio message is received.
The on radio received
loop looks for the button press messages 0 and 1 placed in the value receivedNumber
by MakeCode. If it receives them, the loop will toggle the value of position
(for button A presses) and light
(for button B presses). If one of those variables is 1
, it will set it to 0
and if the value is 0
it will set it to 1
.
The forever
loop does the work for us. It acts on the values of position
and light
. If position
is 0, the angle of the servo is set to zero else it is set to 100 degrees. If light
is set to 1 the NeoPixel strip is set to a rainbow pattern, otherwise it is turned off.
If you want to add actions, you can have the on radio received
block look for other radio commands and set a variable. Then add an action in forever
that acts on the variable.
Next to construct our circuit.
Page last edited March 08, 2024
Text editor powered by tinymce.