pxt-calliope/docs/getting-started/buttons.md

1.8 KiB

Button A and button B

~avatar avatar

Buttons are great to build games!

~

This program will show the word ANTEATER on the LED screen when you press button A.

input.onButtonPressed(Button.A, () => {
    basic.showString("ANTEATER");
});

~hint

The showString block can show letters, numbers, and punctuation on the micro:bit screen.

~

Now try to unscramble these blocks in the editor so that the micro:bit shows BANANA when you press button B.

input.onButtonPressed(Button.B, () => {
    basic.showString("BANANA");
});

~hint

You can find the letter B by clicking the letter A on the onButtonPressed block.

~

Click Download to move your program to the BBC micro:bit!

Your turn!

Can you combine these blocks so your program shows your real name instead of ANTEATER when you press A, but your secret agent name instead of BANANA when you press B?

Pins

You can also use the pins as buttons. (The pins are the holes in the metal stripe at the bottom of the micro:bit board.) For example, hold the GND button with one hand and touch the 0 pin (called P0) with your other hand to tell the micro:bit you're pressing it.

Unscramble the blocks in the editor to show a heart when you touch pin P0.

input.onPinPressed(TouchPin.P0, () => {
    basic.showLeds(`
. # . # .
# . # . #
# . . . #
. # . # .
. . # . .`);
});

Click Download to move your program to the BBC micro:bit!

~hint

Try this experiment: find a friend and hold hands. Touch the GND pin while your friend presses the P0 pin. You should see the heart! The electric current is going through your bodies and across your handshake to make it happen!

~

~button /getting-started/shake

NEXT: SHAKE

~