Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| gf_informatik:microbit_programmieren_loesungen [2021-09-22 07:55] – [Aufgaben F] hof | gf_informatik:microbit_programmieren_loesungen [2022-08-10 11:24] (aktuell) – gelöscht sca | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== Microbit programmieren: | ||
| - | ==== Aufgaben A ==== | ||
| - | |||
| - | === A1 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | display.show(Image.DUCK) | ||
| - | </ | ||
| - | |||
| - | === A2 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | myImages = [Image.SAD, Image.MEH, Image.YES, Image.NO, Image.COW] | ||
| - | |||
| - | display.show(myImages, | ||
| - | </ | ||
| - | |||
| - | === A3 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | all_1 = Image(" | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | |||
| - | all_3 = Image(" | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | |||
| - | all_5 = Image(" | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | |||
| - | all_7 = Image(" | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | |||
| - | all_9 = Image(" | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | |||
| - | myImages = [all_1, all_3, all_5, all_7, all_9] | ||
| - | |||
| - | display.show(myImages, | ||
| - | </ | ||
| - | |||
| - | === A4 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | while True: | ||
| - | for brightness in range(1, | ||
| - | sleep(200) | ||
| - | for column in range(5): | ||
| - | for row in range (5): | ||
| - | display.set_pixel(column, | ||
| - | </ | ||
| - | |||
| - | ==== Aufgaben B ==== | ||
| - | |||
| - | === B1 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | while True: | ||
| - | if button_a.is_pressed(): | ||
| - | display.show(Image.GHOST) | ||
| - | if button_b.is_pressed(): | ||
| - | display.show(Image.RABBIT) | ||
| - | if pin_logo.is_touched(): | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | === B2 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | while True: | ||
| - | if button_a.is_pressed(): | ||
| - | display.scroll(" | ||
| - | if button_b.is_pressed(): | ||
| - | display.show(Image.RABBIT) | ||
| - | if pin_logo.is_touched(): | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | === B3 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | while True: | ||
| - | if button_a.is_pressed(): | ||
| - | display.scroll(" | ||
| - | if button_b.is_pressed(): | ||
| - | display.show(Image.RABBIT) | ||
| - | if pin_logo.is_touched(): | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | === B4 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | while True: | ||
| - | if button_a.is_pressed(): | ||
| - | display.show(Image.HAPPY) | ||
| - | elif button_b.is_pressed(): | ||
| - | display.show(Image.SAD) | ||
| - | elif pin_logo.is_touched(): | ||
| - | display.show(Image.HEART) | ||
| - | else: | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | === B5 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | while True: | ||
| - | if pin_logo.is_touched(): | ||
| - | display.scroll(str(button_a.get_presses()-button_b.get_presses())) | ||
| - | </ | ||
| - | |||
| - | ==== Aufgaben C ==== | ||
| - | |||
| - | === C1 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | while True: | ||
| - | if accelerometer.was_gesture(" | ||
| - | display.show(Image.DUCK) | ||
| - | elif accelerometer.was_gesture(" | ||
| - | display.show(Image.HOUSE) | ||
| - | </ | ||
| - | |||
| - | === C2 === | ||
| - | [[https:// | ||
| - | |||
| - | === C3 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | import random | ||
| - | |||
| - | while True: | ||
| - | if accelerometer.was_gesture(" | ||
| - | display.show(random.randint(1, | ||
| - | sleep(1000) | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | === C4 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | import random | ||
| - | |||
| - | while True: | ||
| - | if accelerometer.was_gesture(" | ||
| - | display.show(random.randint(1, | ||
| - | sleep(1000) | ||
| - | if pin_logo.is_touched(): | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | === C5 – Variante A === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | myImages = [] | ||
| - | |||
| - | for i in range(0, 9, 1): | ||
| - | myImg = Image() | ||
| - | myImg.fill(i) | ||
| - | myImages.append(myImg) | ||
| - | |||
| - | myPos = 0 | ||
| - | |||
| - | while True: | ||
| - | sleep(200) | ||
| - | if myPos < len(myImages) - 1: | ||
| - | myPos += 1 | ||
| - | else: | ||
| - | myPos = 0 | ||
| - | display.show(myImages[myPos]) | ||
| - | </ | ||
| - | |||
| - | === C5 – Variante B === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | myImages = [] | ||
| - | |||
| - | for i in range(0, 9, 1): | ||
| - | myImg = Image() | ||
| - | myImg.fill(i) | ||
| - | myImages.append(myImg) | ||
| - | |||
| - | display.show(myImages, | ||
| - | </ | ||
| - | |||
| - | === C6 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | myImages = [] | ||
| - | myPos = 0 | ||
| - | |||
| - | for i in range(0, 9, 1): | ||
| - | myImg = Image() | ||
| - | myImg.fill(i) | ||
| - | myImages.append(myImg) | ||
| - | |||
| - | while True: | ||
| - | if(button_a.is_pressed()): | ||
| - | sleep(100) | ||
| - | if myPos < len(myImages) - 1: | ||
| - | myPos += 1 | ||
| - | else: | ||
| - | myPos = 0 | ||
| - | display.show(myImages[myPos]) | ||
| - | </ | ||
| - | |||
| - | === C7 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | myImages = [] | ||
| - | myPos = 0 | ||
| - | |||
| - | for i in range(0, 10, 1): | ||
| - | myImg = Image() | ||
| - | myImg.fill(i) | ||
| - | myImages.append(myImg) | ||
| - | |||
| - | while True: | ||
| - | xAcceleration = accelerometer.get_x() | ||
| - | # xAcceleration von +/-2040 nach 0...9 umrechnen: | ||
| - | myPos = abs(xAcceleration) // 205 | ||
| - | print(myPos) | ||
| - | display.show(myImages[myPos]) | ||
| - | </ | ||
| - | |||
| - | ==== Aufgaben D ==== | ||
| - | |||
| - | === D1 === | ||
| - | <code python> | ||
| - | from microbit import* | ||
| - | import music | ||
| - | |||
| - | # Frère Jacques: | ||
| - | melodyJacques = [' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | |||
| - | note = 0 | ||
| - | while True: | ||
| - | if(button_a.is_pressed()): | ||
| - | music.play(melodyJacques[note]) | ||
| - | sleep(30) | ||
| - | if(note < len(melodyJacques)-1): | ||
| - | note += 1 | ||
| - | else: | ||
| - | note = 0 | ||
| - | </ | ||
| - | |||
| - | === D2 === | ||
| - | <code python> | ||
| - | from microbit import* | ||
| - | import music | ||
| - | |||
| - | # Frère Jacques: | ||
| - | melodyJacques = [' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | |||
| - | note = 0 | ||
| - | while True: | ||
| - | if(button_a.is_pressed()): | ||
| - | display.show(Image.HAPPY) | ||
| - | music.play(melodyJacques[note]) | ||
| - | sleep(30) | ||
| - | if(note < len(melodyJacques)-1): | ||
| - | note += 1 | ||
| - | else: | ||
| - | note = 0 | ||
| - | else: | ||
| - | display.show(Image.ARROW_W) | ||
| - | </ | ||
| - | |||
| - | === D3 === | ||
| - | <code python> | ||
| - | from microbit import* | ||
| - | import music | ||
| - | |||
| - | # Frère Jacques: | ||
| - | melodyJacques = [' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | |||
| - | # Popcorn: | ||
| - | melodyPop = | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | |||
| - | noteA = 0 | ||
| - | noteB = 0 | ||
| - | while True: | ||
| - | if(button_a.is_pressed()): | ||
| - | music.set_tempo(bpm = 120) # Diese Melodie normal spielen | ||
| - | display.show(Image.ASLEEP) | ||
| - | music.play(melodyJacques[noteA]) | ||
| - | sleep(30) | ||
| - | if(noteA < len(melodyJacques)-1): | ||
| - | noteA += 1 | ||
| - | else: | ||
| - | noteA = 0 | ||
| - | |||
| - | elif(button_b.is_pressed()): | ||
| - | music.set_tempo(bpm = 200) # Diese Melodie schneller spielen | ||
| - | display.show(Image.MUSIC_CROTCHET) | ||
| - | music.play(melodyPop[noteB]) | ||
| - | sleep(30) | ||
| - | if(noteB < len(melodyPop)-1): | ||
| - | noteB += 1 | ||
| - | else: | ||
| - | noteB = 0 | ||
| - | |||
| - | else: | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | === D4 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | import music | ||
| - | |||
| - | while True: | ||
| - | for freq in range(330, 770, 10): | ||
| - | music.pitch(freq, | ||
| - | for freq in range(770, 330, -10): | ||
| - | music.pitch(freq, | ||
| - | </ | ||
| - | |||
| - | === D5 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | import music | ||
| - | |||
| - | freq = 440 # A4 | ||
| - | while True: | ||
| - | if button_a.is_pressed(): | ||
| - | if freq < 4000: | ||
| - | freq += 100 | ||
| - | music.pitch(freq, | ||
| - | if button_b.is_pressed(): | ||
| - | if freq > 100: | ||
| - | freq -= 100 | ||
| - | music.pitch(freq, | ||
| - | </ | ||
| - | |||
| - | === D6 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | import music | ||
| - | |||
| - | while True: | ||
| - | if accelerometer.was_gesture(" | ||
| - | display.show(Image.HAPPY) | ||
| - | music.play(music.JUMP_UP) | ||
| - | elif accelerometer.was_gesture(" | ||
| - | display.show(Image.SAD) | ||
| - | music.play(music.JUMP_DOWN) | ||
| - | else: | ||
| - | display.clear() | ||
| - | </ | ||
| - | |||
| - | ==== Aufgaben F ==== | ||
| - | |||
| - | === F1 === | ||
| - | <code python> | ||
| - | from microbit import * | ||
| - | |||
| - | i2cAddr = 16 | ||
| - | while i2cAddr not in i2c.scan(): | ||
| - | display.show(Image.SAD) | ||
| - | display.show(Image.HAPPY) | ||
| - | |||
| - | def motor_run(motors=0, | ||
| - | # direction: 0 = forward, 1 = backward | ||
| - | # speed range: 0...255 | ||
| - | i2cBuf = bytearray([motors, | ||
| - | if motors == 0: # left motor | ||
| - | i2cBuf[0] = 0x00 | ||
| - | i2c.write(i2cAddr, | ||
| - | if motors == 1: # right motor | ||
| - | i2cBuf[0] = 0x02 | ||
| - | i2c.write(i2cAddr, | ||
| - | if motors == 2: # both motors | ||
| - | i2cBuf[0] = 0x00 | ||
| - | i2c.write(i2cAddr, | ||
| - | i2cBuf[0] = 0x02 | ||
| - | i2c.write(i2cAddr, | ||
| - | |||
| - | while True: | ||
| - | if button_a.is_pressed(): | ||
| - | motor_run(0, | ||
| - | sleep(1000) | ||
| - | motor_run(0, | ||
| - | </ | ||
| - | |||
| - | === F2 – Nur die Funktion turn() === | ||
| - | <code python> | ||
| - | def turn(angle = 90): | ||
| - | factor = 6.3 | ||
| - | driveTime = abs(angle) * factor | ||
| - | if(angle> | ||
| - | motor_run(0, | ||
| - | else: # negativer Winkle: drehe linksrum | ||
| - | motor_run(1, | ||
| - | sleep(driveTime) | ||
| - | motor_run(2, | ||
| - | </ | ||
| - | |||
| - | === F3 – Nur die Funktion drive() === | ||
| - | <code python> | ||
| - | def drive(distance = 10): | ||
| - | factor = 60 # factor und... | ||
| - | speed = 100 # ...speed aufeinander abstimmen (ausprobieren) | ||
| - | # je grösser die Distanz, desto länger fahren: | ||
| - | driveTime = abs(distance) * factor | ||
| - | if(distance> | ||
| - | motor_run(2, | ||
| - | else: # negativ: fahre rückwärts | ||
| - | motor_run(2, | ||
| - | sleep(driveTime) | ||
| - | motor_run(2, | ||
| - | </ | ||
| - | |||
| - | === F4 – Nur die Funktions-Aufrufe=== | ||
| - | Zum Beispiel so: | ||
| - | <code python> | ||
| - | while True: | ||
| - | if button_a.is_pressed(): | ||
| - | drive(11) | ||
| - | turn(60) | ||
| - | drive(22) | ||
| - | turn(-100) | ||
| - | drive(22) | ||
| - | turn(120) | ||
| - | drive(15) | ||
| - | turn(-120) | ||
| - | drive(8) | ||
| - | </ | ||
| - | |||
| - | === Aufgaben G === | ||
| - | |||
| - | Vorlage: schreiben Sie eine Funktion " | ||
| - | |||
| - | <code python hoover.py> | ||
| - | from microbit import * | ||
| - | import utime | ||
| - | import machine | ||
| - | import math | ||
| - | |||
| - | |||
| - | i2cAddr = 16 # Adresse des Motor-Controllers | ||
| - | while i2cAddr not in i2c.scan(): | ||
| - | display.show(Image.SAD) | ||
| - | display.show(Image.HAPPY) | ||
| - | |||
| - | def motor_run(motors=0, | ||
| - | # direction: 0 = forward, 1 = backward | ||
| - | # speed range: 0...255 | ||
| - | i2cBuf = bytearray([motors, | ||
| - | if motors == 0: # left motor | ||
| - | i2cBuf[0] = 0x00 | ||
| - | i2c.write(i2cAddr, | ||
| - | if motors == 1: # right motor | ||
| - | i2cBuf[0] = 0x02 | ||
| - | i2c.write(i2cAddr, | ||
| - | if motors == 2: # both motors | ||
| - | i2cBuf[0] = 0x00 | ||
| - | i2c.write(i2cAddr, | ||
| - | i2cBuf[0] = 0x02 | ||
| - | i2c.write(i2cAddr, | ||
| - | |||
| - | |||
| - | def stop(): | ||
| - | motor_run(2, | ||
| - | |||
| - | def drive(distance): | ||
| - | stop() # Stop all other movement | ||
| - | ms_per_cm = 60 | ||
| - | if distance > 0: | ||
| - | ms = ms_per_cm * distance | ||
| - | direction = 0 | ||
| - | else: | ||
| - | ms = -1 * ms_per_cm * distance | ||
| - | direction = 1 | ||
| - | motor_run(2, | ||
| - | sleep(ms) | ||
| - | stop() | ||
| - | |||
| - | def turn(degrees): | ||
| - | stop() # Stop all other movement | ||
| - | ms_per_degree = 13 | ||
| - | if degrees > 0: | ||
| - | fast = 0 # turn right, left motor fast | ||
| - | slow = 1 # ... and right motor slow | ||
| - | ms = ms_per_degree * degrees | ||
| - | else: | ||
| - | fast = 1 # turn left, right motor fast | ||
| - | slow = 0 # ... and left motor slow | ||
| - | ms = ms_per_degree * degrees * -1 | ||
| - | motor_run(fast, | ||
| - | motor_run(slow, | ||
| - | sleep(ms) | ||
| - | stop() | ||
| - | |||
| - | |||
| - | def getDistance(): | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | # | ||
| - | | ||
| - | |||
| - | |||
| - | |||
| - | </ | ||