- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
15 October 2018
This project is called the JLL piano. This is a school project that my daughter created for her music homework. She needed to create a project that related to Jerry Lee Lewis and she created a mini piano that plays a short clip for a different song depending upon the key that is pressed.
It uses a Raspberry Pi with a Pimoroni Piano Hat.
You first need to setup the Piano Hat using the Pimoroni Piano Hat install instructions. Then create the following in a text editor.
The program will work in Python 2 or Python 3.
You can of course change the music clips to be any music you prefer.
from pygame import mixer
# Change key numbering (black keys = -1)
keys = [0,-1,1,-1,2,3,-1,4,-1,5,-1,6,7]
sound_files = [
# Change volume by amount specified 0 to 10 (+1, -1 etc.)
def change_volume (change):
#print ("Change volume "+str(change))
if change == 0 : return
# volume is 0 to 1 so divide by 10
delta = change / 10
new_volume = sounds.get_volume() + delta
if new_volume > 1:
new_volume = 1
if new_volume < 0:
new_volume = 0
for this_sound in sounds:
def handle_note(channel, pressed):
channel = keys[channel]
if (channel < 0):
mixer.init(22050, -16, 2, 512)
sounds = [mixer.Sound(sound_file) for sound_file in sound_files]