Vasile Enachi
Published © MIT

My Little Piano

A web application using Raspberry Pi which provides an interactive way with song samples to play the piano using a buzzer and RGB led

IntermediateFull instructions provided1,068
My Little Piano

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Breadboard (generic)
Breadboard (generic)
×1
Buzzer
Buzzer
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Male/Male Jumper Wires
×8
Male/Female Jumper Wires
Male/Female Jumper Wires
×9
Through Hole Resistor, 150 ohm
Through Hole Resistor, 150 ohm
×3
LED, RGB
LED, RGB
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×3

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

Visualisation using Fritzing

Schematics

Schema for the circuit

Code

my_little_piano_app.py

Python
This is the file containing the main function. In order to run the application run:
python3 my_little_piano_app.py
"""
 * In HTML and using JavaScript we will call special functions that
 * will play a specific note on the buzzer when any key of the piano
 * is pressed. Also we have 3 pages on the web, one for single octave piano,
 * another for three octave piano, and last one are for the sample songs.
 * You can enable those songs using the 3 inputs of the breadboard.
 * Before you run the code, do not forget to start the pigpiod daemon
 * using "sudo pigpiod".

 """
import threading

from samples.coffin_dance import CoffinDance
from samples.sweet_dreams import SweetDreams
from samples.smells_like_teen_spirit import SmellsLikeTennSpirit
from utilities.utilities import load_piano_octaves, get_piano_octaves_json
from web_services import web_services as web
from raspberrypi_configurations import raspberrypi_configurations as rasp

running_flag = True


def local_input_piano_samples():
    global running_flag, web_song
    try:
        data = get_piano_octaves_json()
        sweet_dreams = SweetDreams(data['octave_4'], data['octave_5'], 3)
        smells_like_teen_spirits = SmellsLikeTennSpirit(data['octave_4'], data['octave_5'], data['octave_6'], 1.2)
        coffin_dance = CoffinDance(data['octave_4'], data['octave_5'], 1.2)
        print("Loaded piano json for local input. Thread: {}".format(threading.current_thread()))
        while running_flag:
            # if the first button was pressed, play the first song
            if (rasp.GPIO.input(rasp.button1) == rasp.GPIO.LOW or web.web_song == 1) and rasp.is_buzzer_off:
                rasp.is_buzzer_off = False
                rasp.is_buzzer_used_locally = True
                coffin_dance.play()
                rasp.is_buzzer_off = True
                rasp.is_buzzer_used_locally = False
                web.web_song = 0

            # if the second button was pressed, play the second song
            if (rasp.GPIO.input(rasp.button2) == rasp.GPIO.LOW or web.web_song == 2) and rasp.is_buzzer_off:
                rasp.is_buzzer_off = False
                rasp.is_buzzer_used_locally = True
                smells_like_teen_spirits.play()
                rasp.is_buzzer_off = True
                rasp.is_buzzer_used_locally = False
                web.web_song = 0

            # if the third button was pressed, play the third song
            if (rasp.GPIO.input(rasp.button3) == rasp.GPIO.LOW or web.web_song == 3) and rasp.is_buzzer_off:
                rasp.is_buzzer_off = False
                rasp.is_buzzer_used_locally = True
                sweet_dreams.play()
                rasp.is_buzzer_off = True
                rasp.is_buzzer_used_locally = False
                web.web_song = 0
    except (RuntimeError, OSError, AttributeError, KeyboardInterrupt):
        print("Unexpected event in the main thread. Exiting thread {}".format(threading.current_thread()))
    finally:
        print("Thread {} stopped.".format(threading.current_thread()))


def main():
    global running_flag
    web.octave_array = load_piano_octaves()

    if web.octave_array is None:
        return

    try:
        # we will run the app on port 2014
        # run(host='0.0.0.0', port='2014')
        local_input_thread = threading.Thread(target=local_input_piano_samples)
        local_input_thread.daemon = True
        local_input_thread.start()

        web.app.debug = False
        web.app.use_reloader = False
        web.app.run(host='0.0.0.0', port=2014)

    except KeyboardInterrupt:
        pass
    finally:
        #stop the local thread
        running_flag = False

        # stop the pwm signal
        rasp.pi.hardware_PWM(web.buzzer, 0, 0)
        rasp.rgb_led_configure()
        rasp.GPIO.cleanup()

        # stop the connection with the daemon
        rasp.pi.stop()


if __name__ == '__main__':
    main()

My Little Piano

Repository on github

Credits

Vasile Enachi

Vasile Enachi

1 project • 1 follower
Thanks to Plusivo.

Comments