Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
talit:microbit [2024-04-21 13:23] – [Auftrag 1] sca | talit:microbit [2024-04-29 11:36] (aktuell) – [Lösungen Aufgaben] sca | ||
---|---|---|---|
Zeile 35: | Zeile 35: | ||
- | ===== - Micro:bit kennenlernen | + | ===== - Aufgaben |
Die hilfreichste Quelle ist das Wiki vom Grundlagenfach 2M: https:// | Die hilfreichste Quelle ist das Wiki vom Grundlagenfach 2M: https:// | ||
- | ==== Auftrag | + | ==== Aufgabe |
+ | |||
+ | Erstelle GitHub-Repo **microbit**, | ||
1. Das LED im Zentrum der $5 \times 5-$LED-Matrix soll leuchten. Drückt man den linken/ | 1. Das LED im Zentrum der $5 \times 5-$LED-Matrix soll leuchten. Drückt man den linken/ | ||
- | * Mit `display.set_pixel(1, | + | * Mit `display.set_pixel(1, |
- | | + | |
- | | + | |
- | | + | |
- | </WARP> | + | </WRAP> |
+ | 1. Wahrscheinlich gibt es jetzt eine Fehlermeldung, | ||
+ | |||
+ | ==== Aufgabe 2: Würfel ==== | ||
+ | |||
+ | Implementiere einen Würfel. Drückt man eine der beiden Tasten, soll eine Zufallszahl $1$ bis $6$ generiert (-> random-Modul) und das entsprechende Würfelsymbol angezeigt werden. Zeichne die Bilder mit `display.set_pixel(...)` selbst. | ||
+ | |||
+ | ==== Aufgabe 3: Misc ==== | ||
+ | |||
+ | 1. Fotoschau: Im micro:bit sind viele verschiedene Bilder vordefiniert. Speichere einige davon in einer Liste. Mit den beiden Knöpfen soll man die Bilder durchgehen können. | ||
+ | 1. Designe ein eigenes hübsches Bild und füge es der Fotoschau hinzu. | ||
+ | 1. Random Shake: Der micro:bit verfügt über einen Beschleunigungssensor. Mit diesem kann man u.a. bestimmen, ob der micro:bit gerade geschüttelt wird. Modifiziere deinen Code aus Aufgabe 2: Jedes Mal, wenn der micro:bit geschüttelt wird, wird eine neue Zahl gewürfelt. Das Schütteln kann man auch in Online-Editor simulieren. | ||
+ | |||
+ | Optional: | ||
+ | |||
+ | 1. Benötigt zwei micro:bits: Über BLE können mehrere micro:bit miteinander kommunizieren. Verbinde zwei micro:bit miteinander und modifiziere den Würfel-Code wie folgt: Auf beiden micro:bits wird immer der gleiche Würfel angezeigt. Drückt man auf einem der beiden micro:bits einen Knopf, wird eine neue Zahl gewürfelt und auf *beiden* angezeigt. | ||
+ | |||
+ | |||
+ | ==== Lösungen Aufgaben ==== | ||
+ | |||
+ | |||
+ | ++++Lösungen| | ||
+ | |||
+ | <nodisp 2> | ||
+ | |||
+ | <code python auftrag_1.py> | ||
+ | # Imports go at the top | ||
+ | from microbit import * | ||
+ | |||
+ | x = 2 | ||
+ | y = 2 | ||
+ | |||
+ | while True: | ||
+ | if button_a.get_presses(): | ||
+ | x -= 1 | ||
+ | if button_b.get_presses(): | ||
+ | x += 1 | ||
+ | if x < 0: x = 0 | ||
+ | elif x > 4: x = 4 | ||
+ | display.clear() | ||
+ | display.set_pixel(x, | ||
+ | sleep(100) | ||
+ | </ | ||
+ | |||
+ | <code python auftrag_2.py> | ||
+ | from microbit import * | ||
+ | import random | ||
+ | |||
+ | def dice(n): | ||
+ | display.clear() | ||
+ | if n == 1: | ||
+ | display.set_pixel(2, | ||
+ | elif n == 2: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 3: | ||
+ | display.set_pixel(2, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 4: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 5: | ||
+ | display.set_pixel(2, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 6: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | |||
+ | while True: | ||
+ | if button_a.get_presses() or button_b.get_presses(): | ||
+ | dice(random.randint(1, | ||
+ | sleep(100) | ||
+ | </ | ||
+ | |||
+ | Alternative Ideen für dice-Funktion: | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | ### Variante 1 | ||
+ | |||
+ | def dice(n): | ||
+ | display.clear() | ||
+ | if n == 1: | ||
+ | display.set_pixel(2, | ||
+ | elif n == 2: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 3: | ||
+ | display.set_pixel(2, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 4: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 5: | ||
+ | display.set_pixel(2, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | elif n == 6: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | |||
+ | ### Variante 2 | ||
+ | def dice(n): | ||
+ | dots = [ | ||
+ | [[2,2]], | ||
+ | [[0, | ||
+ | [[2, | ||
+ | [[0, | ||
+ | [[2, | ||
+ | [[0, | ||
+ | ] | ||
+ | display.clear() | ||
+ | for pixel in dots[n-1]: | ||
+ | display.set_pixel(pixel[0], | ||
+ | |||
+ | ### Variante 3 | ||
+ | |||
+ | def dice(n): | ||
+ | display.clear() | ||
+ | if n % 2 == 1: | ||
+ | display.set_pixel(2, | ||
+ | if n >= 2: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | if n >= 4: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | if n == 6: | ||
+ | display.set_pixel(0, | ||
+ | display.set_pixel(4, | ||
+ | </code> | ||
+ | <code python auftrag_3.py> | ||
+ | </ | ||
- | ==== Auftrag 2 ==== | ||
- | 1. **Buttons und LED-Matrix**: | + | </ |
- | 1. Micro:bit reagiert auf Tastendruck | + | |
- | 1. Bilder anzeigen auf LED-Matrix | + | |
- | 1. eigene Bilder designen und anzeigen | + | |
- | 1. Mit Knöpfen durch Liste mit Bildern gehen (Achtung: ev. kennen 1M Listen noch nicht) | + | |
- | 1. **Beschleunigungssensor**: | + | |
- | 1. Was macht dieser? Was bedeuten gemessene Werte? | + | |
- | 1. Bild auf LED-Matrix leuchtet heller, je höher Beschleunigung ist. | + | |
- | 1. **BLE**: | + | |
- | 1. Was ist BLE? Wozu ist es gut? | + | |
- | 1. Nachrichten zwischen zwei Micro:bits hin und her senden. | + | |
+ | ++++ | ||