| Deutsch English |
![]()
YOU LEARN HERE... |
how to use the two micro:bit buttons to develop interactive programmes. You will also learn how to play sound with your robot. |
HOW THE BUTTONS WORK ? |
|
EXAMPLES |
The commands for the buttons are in the microbit module, which you must also import. Example 2: Sound is played when a button is clicked A short melody should be played when button A or B is clicked. The button_a.was_pressed() function returns True if button a has been pressed briefly.
from microbit import * from mbrobot import * #from mbrobot_plusV2 import * from music import * repeat: if button_a.was_pressed(): play(JUMP_UP) if button_b.was_pressed(): play(JUMP_DOWN) Das Modul music kann nur im Realmodus importiert werden. Zum Testen Im Simulationmodus kannst du diese Importzeile auskommentieren und den Befehl playTone(440, 1000) verwenden (440 ist di Tonfrequenz, 1000 die Abspieldauer in ms) Examplel 3: A programme with a cancel button
from microbit import * from mbrobot import * #from mbrobot_plusV2 import * setSpeed(15) forward() while not button_a.was_pressed(): d = getDistance() if d < 10: backward() delay(1000) forward() delay(200) stop() Example 4: Control the robot with buttons
from microbit import * from mbrobot import * #from mbrobot_plusV2 import * RobotContext.useBackground("sprites/bg.gif") RobotContext.setStartPosition(310, 460) forward() repeat: v = irLeft.read_digital() if v == 0: backward() delay(500) stop() if button_a.was_pressed(): left() delay(550) forward() elif button_b.was_pressed(): right() delay(550) forward() sleep(10) |
REMEMBER... |
You can use buttons to develop interactive programmes. The function is_pressed() returns True when the button is pressed. The function was_pressed() returns True if the button has been clicked at any point since the programme started or since the last call. |
TO SOLVE YOURSELF |
|
![]()
Melodies that are built into the music module (real mode only):
ADADADUM - Eröffnung von Beethoven 5. Sinfonie in C Moll
ENTERTAINER - Scott Joplin Ragtime Klassiker
PRELUDE -J.S.Bach 48 Preludien und Fugen
ODE - Ode an Joy Thema aus Beethoven 9. Sinfonie in D Moll
NYAN - das Nyan Cat Thema
RINGTONE - ein Klingelton
FUNK - ein Geräusch für Geheimagente
n
BLUES - ein Boogie-Woogie Blues
BIRTHDAY - Happy Birthday to You...
WEDDING - der Chorus des Bräutigams aus Wagner Oper Lohengrin
FUNERAL - der Trauerzug, Chopin Klaviersonate
PUNCHLINE - a lustiger Tonclip, nachdem ein Witz gemacht wurde
PYTHON - aus Monty Python Flying Circus
BADDY - Filmclip aus The Baddy
CHASE - Filmclick aus einer Jagdszene
BA_DING - ein Signalton, der darauf hinweist, dass etwas geschehen ist
WAWAWAWAA - ein trauriger Posaunenklang
JUMP_UP - für Spiele, um auf eine Aufwärtsbewegung hinzuweisen
JUMP_DOWN - für Spiele, um auf eine Abwärtsbewegung hinzuweisen
POWER_UP - ein Fanfarenklang
POWER_DOWN - ein trauriger Fanfarenklang, wenn etwas verloren gegangen ist