## Python & een BBC micro:bit

Bert Wikkerink

We overlopen hoe, in Python, de ingebouwde apparaten van een BBC micro:bit aan te sturen en te lezen: patronen op de 5x5 LED-matrix, een deuntje afspelen, lezen van de waarden van de temperatuur-sensor, acceleratiemeter, ... En hoe met externe Grove-sensoren en NeoPixels aan de slag?

De combinatie van TI-technologie en micro:bit leidt tot unieke programmeeractiviteiten zoals een kansexperiment simuleren via communcaties tussen micro:bits onderling of objecten op het scherm van een handheld of computer aansturen met een micro:bit.

Hieronder een overzicht van Python codes die gecombineerd worden in programmeerprojecten.

## Display

• Display Basics:  tns

• Slang:  tns
from microbit import *
display.clear()

x=[0,1,2,3,4,4,4,4,4,3,2,1,0,0,0,0]
y=[0,0,0,0,0,1,2,3,4,4,4,4,4,3,2,1]

i=0; k=1
while get_key() != "esc":
display.set_pixel(x[i],y[i],9*k)
i = (i+1)%16
if i==0:
k=(k+1)%2

## Geluid

• Audio:  tns
• Sirene:  tns

• Muziek:  tns
• Bach:  tns
from microbit import *

prelude2=["C5:1","D","Eb","G4","D5","F4",
"Eb","D","C","Eb","G","C5","F4","Eb5",
"D","F","Ab","C","B4","A","G","B","D5",
"F","Eb","C","G4","C5","E","C","F","C",
"G","C","Ab","C","D","Bb4","F","Bb",
"D5","Bb4","Eb5","Bb4","F5","Bb4","G5",
"Bb4","C5","Ab4","G","Ab","C5","Eb",
"Bb4","G","F","G","Bb","Eb5","Ab4","G",
"F","G","Ab","Eb","D","Eb","F","D",
"Bb3","Ab","G","Bb","Eb4","Bb3","G",
"Bb","A","Eb4","Bb3","Eb4","C","Eb",
"Bb3","Eb4","C","Eb","Db","Eb","C:2",
"Eb","Ab:4","F:2","D5:4","Bb4:2",
"G5:3","F:1","Eb","D","C","D","Eb:2"]

set_volume(255); music.set_tempo(1,270)
music.play(prelude2, wait=False)

## Ingebouwde sensoren

• Buttons & Logo:  tns
• Bal besturen:  tns
• Dobbelen:  tns

• Geluidsniveau:  tns
• Kompas:  tns
• Lichtsterkte:  tns
from microbit import *

niv0 = Image(
"00000:"
"00000:"
"00000:"
"00000:"
"00000:")
niv3 = Image(
"00000:"
"00000:"
"99999:"
"99999:"
"99999:")

niv1 = Image(
"00000:"
"00000:"
"00000:"
"00000:"
"99999:")
niv4 = Image(
"00000:"
"99999:"
"99999:"
"99999:"
"99999:")

niv2 = Image(
"00000:"
"00000:"
"00000:"
"99999:"
"99999:")
niv5 = Image(
"99999:"
"99999:"
"99999:"
"99999:"
"99999:")
niveau=[niv0, niv1, niv2, niv3, niv4, niv5]

while get_key() != "esc":
m=microphone.sound_level()
if m>60:
i=5
elif m>30:
i=4
elif m>20:
i=3
elif m>10:
i=2
elif m>4:
i=1
else:
i=0
display.show(niveau[i])

## Externe sensoren

• Ranger:  tns

• Temperatuur:  tns
from microbit import *
tijd=[]; temp=[]
store_list("tijd",tijd)
store_list("temperatuur",temp)
i=0; clear_history()

while get_key() != "esc":
tijd.append(i)
temp.append(t)
i+=1
store_list("tijd",tijd)
store_list("temperatuur",temp)
sleep(1000)

## Neopixels

• Regenboog:  tns

from microbit import *

r=(255,0,0); o=(255,127,0); ge=(255,255,0)
gr=(0,128,0); b=(0,0,255); i=(75,0,130)
v=(148,0,211); kleuren=[r,o,ge,gr,b,i,v]

np=NeoPixel(pin0,16)

n=0
while get_key() != "esc":
kleur=kleuren[n%7]
for i in range(16):
np[i]=kleur
np.show()
n+=1

np.clear()

• Verstuur:  tns

• Ontvang:  tns
from microbit import *

while get_key() != "esc":
a=input("tekst: ")
if a=="esc":
break
sleep(1000)
from microbit import *

while get_key() != "esc":
if a!=None:
print(a)
sleep(1000)

## Schakelingen

• Stoplicht:  tns
• 7-Segment Digit:  tns

• 4-Digits Klok:  tns
from microbit import *

tabel = [[1,1,1,1,1,1,0],
[0,1,1,0,0,0,0],
[1,1,0,1,1,0,1],
[1,1,1,1,0,0,1],
[0,1,1,0,0,1,1],
[1,0,1,1,0,1,1],
[1,0,1,1,1,1,1],
[1,1,1,0,0,0,0],
[1,1,1,1,1,1,1],
[1,1,1,1,0,1,1]]

pinnen = [0,1,2,12,13,14,15]

def cijfer(n):
rij=tabel[n]
for i in range(7):
p=pin(pinnen[i])
if rij[i]==1:
p.write_digital(1)
else:
p.write_digital(0)

for i in range(10):
cijfer(9-i)
sleep(1000)

get_key(1)
for i in pinnen:
pin(i).write_digital(0)

## Games

• Pong:  tns
• Verstappen:  tns

• Vluchtsimulator:  tns

tns = TI-Nspire CX II-T document met Python-code

Gelijkaardige code kan geprogrammeerd worden voor de TI-84 Plus CE-T Python Edition.

Leerkrachten/docenten kunnen de TI-Nspire CX Premium Teacher Software gratis aanvragen om de Python-code uit te proberen.
Klik hier voor de TI-Nspire CX Premium Teacher Software; als ook voor de TI-SmartView CE-T.

micro:bit-module voor TI-Nspire CX II-T technologie en voor de TI-84 Plus CE-T Python Edition.

Meer over programmeren in Python
TI Python BootCamp >