Microbit programmieren: Lösungen zu den Aufgaben

A1

from microbit import *
display.show(Image.DUCK)

A2

from microbit import *
 
myImages = [Image.SAD, Image.MEH, Image.YES, Image.NO, Image.COW]
 
display.show(myImages, delay = 1000, loop = True)

A3

from microbit import *
 
all_1 = Image("11111:"
              "11111:"
              "11111:"
              "11111:"
              "11111")
 
all_3 = Image("33333:"
              "33333:"
              "33333:"
              "33333:"
              "33333")
 
all_5 = Image("55555:"
              "55555:"
              "55555:"
              "55555:"
              "55555")
 
all_7 = Image("77777:"
              "77777:"
              "77777:"
              "77777:"
              "77777")
 
all_9 = Image("99999:"
              "99999:"
              "99999:"
              "99999:"
              "99999")
 
myImages = [all_1, all_3, all_5, all_7, all_9]
 
display.show(myImages, delay = 100, loop = True)

A4

from microbit import *
 
while True:
    for brightness in range(1,9,2):
        sleep(200)
        for column in range(5):
            for row in range (5):
                display.set_pixel(column, row, brightness)

B1

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

from microbit import *
 
while True:
    if button_a.is_pressed():
        display.scroll("Taste_A")
    if button_b.is_pressed():
        display.show(Image.RABBIT)
    if pin_logo.is_touched():
        display.clear()

B3

from microbit import *
 
while True:
    if button_a.is_pressed():
        display.scroll("Taste_A", wait=False)
    if button_b.is_pressed():
        display.show(Image.RABBIT)
    if pin_logo.is_touched():
        display.clear()

B4

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

from microbit import *
 
while True:
    if pin_logo.is_touched():
        display.scroll(str(button_a.get_presses()-button_b.get_presses()))

C1

from microbit import *
 
while True:
    if accelerometer.was_gesture("shake"):
        display.show(Image.DUCK)
    elif accelerometer.was_gesture("face up"):
        display.show(Image.HOUSE)

C2

C3

from microbit import *
import random
 
while True:
    if accelerometer.was_gesture("shake"):
        display.show(random.randint(1,6))
        sleep(1000)
        display.clear()

C4

from microbit import *
import random
 
while True:
    if accelerometer.was_gesture("face up"):
        display.show(random.randint(1,6))
        sleep(1000)
    if pin_logo.is_touched():
        display.clear()

C5 – Variante A

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

from microbit import *
 
myImages = []
 
for i in range(0, 9, 1):
    myImg = Image()
    myImg.fill(i)
    myImages.append(myImg)
 
display.show(myImages, delay = 200, loop = True)

C6

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

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])

D1

from microbit import*
import music
 
# Frère Jacques:
melodyJacques = ['c4:4', 'd4:4', 'e4:4', 'c4:4','c4:4', 'd4:4', 'e4:4', 'c4:4',
                 'e4:4', 'f4:4', 'g4:8', 'e4:4', 'f4:4', 'g4:8',
                 'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
                 'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
                 'c4:4', 'g3:4', 'c4:8', 'c4:4', 'g3:4', 'c4:8']
 
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

from microbit import*
import music
 
# Frère Jacques:
melodyJacques = ['c4:4', 'd4:4', 'e4:4', 'c4:4','c4:4', 'd4:4', 'e4:4', 'c4:4',
                 'e4:4', 'f4:4', 'g4:8', 'e4:4', 'f4:4', 'g4:8',
                 'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
                 'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
                 'c4:4', 'g3:4', 'c4:8', 'c4:4', 'g3:4', 'c4:8']
 
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

from microbit import*
import music
 
# Frère Jacques:
melodyJacques = ['c4:4', 'd4:4', 'e4:4', 'c4:4','c4:4', 'd4:4', 'e4:4', 'c4:4',
                 'e4:4', 'f4:4', 'g4:8', 'e4:4', 'f4:4', 'g4:8',
                 'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
                 'g4:2', 'a4:2', 'g4:2', 'f4:2', 'e4:4', 'c4:4',
                 'c4:4', 'g3:4', 'c4:8', 'c4:4', 'g3:4', 'c4:8']
 
# Popcorn:
melodyPop =     ['b4:2', 'a4:2', 'b4:2', 'f4:2','d4:2', 'f4:2', 'b3:4',
                 'b4:2', 'a4:2', 'b4:2', 'f4:2','d4:2', 'f4:2', 'b3:4',
                 'b4:2', 'c#5:2', 'd5:2', 'c#5:2', 'd5:2', 'b4:2', 'c#5:2', 'b4:2', 'c#5:2', 'a4:2',
                 'b4:2', 'a4:2', 'b4:2', 'g#4:2', 'b4:4','r:4']
 
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

from microbit import *
import music
 
while True:
    for freq in range(330, 770, 10):
        music.pitch(freq, 100)
    for freq in range(770, 330, -10):
        music.pitch(freq, 100)

D5

from microbit import *
import music
 
freq = 440  # A4
while True:
    if button_a.is_pressed():
        if freq < 4000:
            freq += 100
        music.pitch(freq, 50)
    if button_b.is_pressed():
        if freq > 100:
            freq -= 100
        music.pitch(freq, 50)

D6

from microbit import *
import music
 
while True:
    if accelerometer.was_gesture("face up"):
        display.show(Image.HAPPY)
        music.play(music.JUMP_UP)
    elif accelerometer.was_gesture("face down"):
        display.show(Image.SAD)
        music.play(music.JUMP_DOWN)
    else:
        display.clear()

F1

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=0x00, speed=0):
    # direction: 0 = forward, 1 = backward
    # speed range: 0...255
    i2cBuf = bytearray([motors, direction, speed])
    if motors == 0:  # left motor
        i2cBuf[0] = 0x00
        i2c.write(i2cAddr, i2cBuf)
    if motors == 1:  # right motor
        i2cBuf[0] = 0x02
        i2c.write(i2cAddr, i2cBuf)
    if motors == 2:  # both motors
        i2cBuf[0] = 0x00
        i2c.write(i2cAddr, i2cBuf)
        i2cBuf[0] = 0x02
        i2c.write(i2cAddr, i2cBuf)
 
while True:
    if button_a.is_pressed():
        motor_run(0, 0, 255)
        sleep(1000)
        motor_run(0, 0, 0)

F2 – Nur die Funktion turn()

def turn(angle = 90):
    factor = 6.3
    driveTime = abs(angle) * factor
    if(angle>=0): # positiver Winkel: drehe rechtsrum
        motor_run(0, 0, 255)
    else:   # negativer Winkle: drehe linksrum
        motor_run(1, 0, 255)
    sleep(driveTime)
    motor_run(2, 0, 0)

F3 – Nur die Funktion drive()

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>=0): # positiv: fahre vorwärts
        motor_run(2, 0, speed)
    else: # negativ: fahre rückwärts
        motor_run(2, 1, speed)
    sleep(driveTime)
    motor_run(2, 0, 0) # beide Motoren stoppen

F4 – Nur die Funktions-Aufrufe

Zum Beispiel so:

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 „hoover()“ die den Roboter herumfahren lässt, ohne in eine Wand zu stossen.

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=0x00, speed=0):
    # direction: 0 = forward, 1 = backward
    # speed range: 0...255
    i2cBuf = bytearray([motors, direction, speed])
    if motors == 0:  # left motor
        i2cBuf[0] = 0x00
        i2c.write(i2cAddr, i2cBuf)
    if motors == 1:  # right motor
        i2cBuf[0] = 0x02
        i2c.write(i2cAddr, i2cBuf)
    if motors == 2:  # both motors
        i2cBuf[0] = 0x00
        i2c.write(i2cAddr, i2cBuf)
        i2cBuf[0] = 0x02
        i2c.write(i2cAddr, i2cBuf)
 
 
def stop():
    motor_run(2, 0, 0)
 
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, direction, 50)
    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, 0, 50)
    motor_run(slow, 0, 10)
    sleep(ms)
    stop()
 
 
def getDistance():
   pin1.write_digital(1)   # Pin 1 (Trigger) HIGH für...
   utime.sleep_us(10)      # ...10 µs...
   pin1.write_digital(0)   # ...und wieder LOW.
   echoPulse = machine.time_pulse_us(pin2, 1) # Messe, wie lange der Echo-Impuls an Pin 2 dauert.
   distance = echoPulse * 0.017 # Rechne Zeit in Distanz um.
   #display.scroll(int(distance))
   return distance
  • gf_informatik/microbit_programmieren_loesungen.1632297374.txt.gz
  • Zuletzt geändert: 2021-09-22 07:56
  • von hof