Skip to content

Instantly share code, notes, and snippets.

@mandyRae
Created November 12, 2015 14:26
Show Gist options
  • Select an option

  • Save mandyRae/983a2547cf8239c88567 to your computer and use it in GitHub Desktop.

Select an option

Save mandyRae/983a2547cf8239c88567 to your computer and use it in GitHub Desktop.
'''
Five-Button Melody Recorder with Raspberry Pi
2015 Amanda on wordpress.electrothoughts.com
Please reuse this code to your heart's content!
This circuit records a melody using buttons and plays the melody
back through a buzzer or 8 ohm speaker. Inspired by the game 'Music Machine'
on old electronic Merlin toy.
Components:
6 1K ohm resistors
5 470 ohm resistors
6 NO pushbuttons
assorted wires
Piezo buzzer or speaker
5 CA RGB LEDs
Wire five pull-up resistor buttons next to a corresponding RGB with the green
and red pins wired. Wire a sixth pull-down button as the play button. Wire up buzzer.
Alternatively, you can use seperate red and green LEDs instead of RGBs, or even just one
set of LEDs.
Future improvements include:
-button for 'rest', or silence
-button for reset melody
-button to save melody onto computer
-potentiometer to adjust buzzer volume
'''
import RPi.GPIO as GPIO, time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#The order of the items in the following is important because the indices must match
#Fill in the pin numbers you're using from left to right.
red_leds = [19, 6, 22, 17, 3] #SCL = 3, this list consists of the red leads of the RGBs
green_leds = [13, 5, 27, 4, 2] #SDA = 2, this list consists of the green leads of the RGBs
buttons = [21, 20, 16, 12, 25]
tones = [262, 294, 330, 344, 392] #This is a lists of frequencies for the buzzer, notes of the C major scale
playB = 24 #This is the button that plays back what's been recorded
buzzer = 18
pressed = False #Pull-up resistor button wiring
#Setup lines, note that you'll have to use a for loops for the LED
#and button lists if your using a version of Raspbian before Dec 2014
GPIO.setup(red_leds, GPIO.OUT, initial = 0)
GPIO.setup(green_leds, GPIO.OUT, initial = 0)
GPIO.setup(buzzer, GPIO.OUT)
GPIO.setup(buttons, GPIO.IN)
GPIO.setup(playB, GPIO.IN)
#This is a function to make a tone with buzzer or speaker
def buzz(pitch, duration):
period = 1.0 / pitch
delay = period/2
cycles = int(duration * pitch)
for i in range(cycles):
GPIO.output(buzzer, True)
time.sleep(delay)
GPIO.output(buzzer, False)
time.sleep(delay)
#This list holds the sequence of notes
sequence = []
print("Welcome to the Melody Recorder! \n Press buttons to make a melody.")
try:
while True:
for i in buttons: #This loop repeats infinitely to check if a button is pressed
if GPIO.input(i)== pressed: #When the button is pressed,
note = buttons.index(i) #the note variable stores the index
sequence.append(note) #of the button and adds it to the sequence.
GPIO.output(red_leds[note], True) #The corresponding LED lights up red when
buzz(tones[note], .5) #when pressed and the buzzer plays the tone.
GPIO.output(red_leds[note], False)
##print(sequence) This is useful for debugging
if GPIO.input(playB) == pressed: #This loop runs infinitely as well; when the play button is pressed,
for i in sequence: #the notes in the sequence list are played through the buzzer, and
GPIO.output(green_leds[i], True) #the corresponding green LED turns on. After the melody is finished playing,
buzz(tones[i], .3) #the program keeps running and recording button presses.
GPIO.output(green_leds[i], False)
except KeyboardInterrupt: #If ctrl+c is pressed on keyboard, the program will exit to the command prompt
print("Exiting to prompt...")
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment