User manual

Heute im Adventskalender
1 Downloadcode
Weihnachtsmusik auf dem Klavier
Mit dem Programm des 24. Tags entlocken Sie dem Klavier über den Pie-
zo-Summer Töne. Zusätzlich zeigen LEDs die gedrückte Taste an. Da dieser
Adventskalender nur sechs LEDs enthält, verwenden wir zusätzlich von ei-
ner RGB-LED nur die blaue Farbe. Für die schwarzen Tasten leuchten jeweils
die beiden LEDs, die die benachbarten weißen Tasten darstellen.
Bauteile
1 Steckbrett
1 LED lila mit eingebautem Vorwiderstand
1 LED rot mit eingebautem Vorwiderstand
1 LED orange mit eingebautem Vorwiderstand
1 LED gelb mit eingebautem Vorwiderstand
1 LED grün mit eingebautem Vorwiderstand
1 LED blau mit eingebautem Vorwiderstand
1 RGB-LED mit eingebauten Vorwiderständen
1 Piezo-Summer
9 GPIO-Verbindungskabel
Das Programm
Das Programm 24mc_klavier03.py macht Musik mit dem Klavier. Stel-
len Sie die Spielfigur am besten auf die weißen Klaviertasten und bewegen
Sie sie mit den Tasten [A] und [D] nach links oder rechts. Schlagen Sie mit
dem Schwert mithilfe der rechten Maustaste auf die Tasten, um Töne zu er-
zeugen. Das Programm geht davon aus, dass das Klavier mit dem Programm
des 23. Tags gebaut wurde.
#!/usr/bin/python
import mcpi.minecraft as minecraft
import mcpi.block as block
import RPi.GPIO as GPIO
import time
LED = [12,7,8,25,24,23,18]
pi = 21
TON = [261,277,293,311,329,349,369,391,415,440,
466,493]
x = -22
z = 30
we = 0
sw = 15
GPIO.setmode(GPIO.BCM)
for i in LED:
GPIO.setup(i, GPIO.OUT, initial=False)
GPIO.setup(pi, GPIO.OUT, initial=False)
p = GPIO.PWM(pi, 1)
def ton(t):
p.ChangeFrequency(TON[t])
p.start(1)
time.sleep(0.2)
p.stop()
mc = minecraft.Minecraft.create()
try:
while True:
for hit in mc.events.pollBlockHits():
bl = mc.getBlockWithData(hit.pos.x, hit.
pos.y, hit.pos.z)
if bl.data == we and hit.pos.z >= z and
hit.pos.z <= z+2:
GPIO.output(LED[0],True)
ton(0)
GPIO.output(LED[0],False)
if bl.data == sw and hit.pos.z >= z+2 and
hit.pos.z <= z+4:
GPIO.output(LED[0],True)
GPIO.output(LED[1],True)
ton(1)
GPIO.output(LED[0],False)
GPIO.output(LED[1],False)
if bl.data == we and hit.pos.z >= z+4 and
hit.pos.z <= z+6:
GPIO.output(LED[1],True)
ton(2)
GPIO.output(LED[1],False)
if bl.data == sw and hit.pos.z >= z+6 and
hit.pos.z <= z+8:
GPIO.output(LED[1],True)
GPIO.output(LED[2],True)
ton(3)
GPIO.output(LED[1],False)
GPIO.output(LED[2],False)
if bl.data == we and hit.pos.z >= z+8 and
hit.pos.z <= z+10:
GPIO.output(LED[2],True)
ton(4)
GPIO.output(LED[2],False)
if bl.data == we and hit.pos.z >= z+12 and
hit.pos.z <= z+14:
GPIO.output(LED[3],True)
ton(5)
GPIO.output(LED[3],False)
if bl.data == sw and hit.pos.z >= z+14 and
hit.pos.z <= z+16:
GPIO.output(LED[3],True)
GPIO.output(LED[4],True)
ton(6)
GPIO.output(LED[3],False)
GPIO.output(LED[4],False)
if bl.data == we and hit.pos.z >= z+16 and
hit.pos.z <= z+18:
GPIO.output(LED[4],True)
ton(7)
GPIO.output(LED[4],False)
if bl.data == sw and hit.pos.z >= z+18 and
hit.pos.z <= z+20:
GPIO.output(LED[4],True)
Sieben LEDs zeigen die Töne des Klaviers an.