The receiver code contains a few more blocks than the transmitter. We'll go over things to see why.
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.
This code has two new sets of blocks:
stripblocks work with NeoPixel LED strips via the NeoPixel Extension
crickitblocks 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.
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,
light, for later use.
The other two loops work with each other to process your radio code:
foreverloop acts on the values of
lightto move a servo or toggle the NeoPixels.
on radio receivedloop toggles the variables
lightif the right radio message is received.
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
forever loop does the work for us. It acts on the values of
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.