Code
Let’s build the code that, when the user presses the button A
on a micro:bit, will send an impulse over a wire to the receiving micro:bit and turn an LED on there.
Make sure that the sending and receiving wires run symmetrically across: pin P1
on one micro:bit is connected to pin P2
on the other, and vice versa, as shown on the pictures in the “Make” section. This way we can use the same code on both micro:bits .
Step 1
We start with a block that digitally writes high (value 1
) to the sending micro:bit’s pin P1
. This block can be found in Pins drawer of the Advanced section.
pins.digitalWritePin(DigitalPin.P1, 1)
Step 2
To show what we are sending, we add a block to turn on an LED in the centre of the LED display (2,2) using plot x, y :
pins.digitalWritePin(DigitalPin.P1, 1)
led.plot(2, 2)
Step 3
Now that we know how to send the signal, we only want to be doing it while the button A
is pressed.
Pick an if block from the Logic drawer (you’ll need the version with else part that will remain empty for now). Add a check whether the button A
is pressed from the Input drawer and move the blocks from the previous step into then part :
if (input.buttonIsPressed(Button.A)) {
pins.digitalWritePin(DigitalPin.P1, 1)
led.plot(2, 2)
} else { }
Step 4
For the else branch (while the button A is not pressed) we want to do the opposite of what we did in the then branch: take the value of pin P1
to low (0) and unplot the corresponding LED on the sending micro:bit :
if (input.buttonIsPressed(Button.A)) {
pins.digitalWritePin(DigitalPin.P1, 1)
led.plot(2, 2)
} else {
pins.digitalWritePin(DigitalPin.P1, 0)
led.unplot(2, 2)
}
Step 5
Let’s wrap it all in a forever loop so this code runs in the background, forever checking the button A
and sending the appropriate signal to the receiver.
Modify your code so that your code looks like this. Download the code into one of the micro:bits, press and release button A
a few times.
basic.forever(() => {
if (input.buttonIsPressed(Button.A)) {
pins.digitalWritePin(DigitalPin.P1, 1)
led.plot(2, 2)
} else {
pins.digitalWritePin(DigitalPin.P1, 0)
led.unplot(2, 2)
}
})
The sending part is done, so we are going to add the receiving part.
Step 6
The receiver needs to digitally read from the pin to which the sending micro:bit will be writing (P2
) over the wire. Let’s start by going to the Pin drawer, adding digital read pin P0
and changing the pin value to P2
.
Now we want to examine the value read from P2
and check whether it is high (value 1
) or not. Go to the Logic drawer, first pick an if block, then come back for a comparison operator (=). Plug in our digital read block as one operand and the value 1
as the other.
We shall turn the LED in the bottom right corner (4,4) on to indicate that we received high and turn it off otherwise.
Your code should look as follows:
basic.forever(() => {
if (input.buttonIsPressed(Button.A)) {
pins.digitalWritePin(DigitalPin.P1, 1);
led.plot(2, 2);
} else {
pins.digitalWritePin(DigitalPin.P1, 0);
basic.clearScreen();
}
if (pins.digitalReadPin(DigitalPin.P2) == 1) {
led.plot(4, 4);
} else {
led.unplot(4, 4);
}
});
Your telegraph is ready!
Step 7
- Connect the first micro:bit to your computer using your USB cable and download the telegraph script to it.
- Connect the second micro:bit to your computer using your USB cable and download the telegraph script to it.
- The first person and second person can take turns pressing button A on their micro:bits to play the telegraph game!