Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- import os
- pins = (13,26)
- score = 'Ab Bb Db Bb F F Eb ' \
- 'Ab Bb Db Bb Eb Eb Db C Bb '\
- 'Ab Bb Db Bb Db Eb C Bb Ab R Ab Eb Db R '\
- 'Ab Bb Db Bb F F Eb '\
- 'Ab Bb Db Bb Ab C Db C Bb '\
- 'Ab Bb Db Bb Db Eb C Bb Ab R Ab Eb Db Db'
- octave = '0 0 1 0 1 1 1 ' \
- '0 0 1 0 1 1 1 0 0 '\
- '0 0 1 0 1 1 1 0 0 0 0 1 1 0 '\
- '0 0 1 0 1 1 1 '\
- '0 0 1 0 0 1 1 1 0 '\
- '0 0 1 0 1 1 1 0 0 0 0 1 1 1'
- # 1/16 =0.0625
- dur = (0.0625,0.0625,0.0625,0.0625,3.0/16,3.0/16,3.0/8,
- 1/16.,1/16.,1/16.,1/16.,3/16.,3/16.,3/16.,1/16.,1/8.,
- 1/16.,1/16.,1/16.,1/16.,1/4.,1/8.,1/8.,1/8.,1/8.,1/8.,1/8.,1/4.,1/4.,1/4.,
- 1/16.,1/16.,1/16.,1/16.,3/16.,3/16.,3/8.,
- 1/16.,1/16.,1/16.,1/16.,1/4.,1/8.,3/16.,1/16.,1/8.,
- 1/16.,1/16.,1/16.,1/16.,1/4.,1/8.,3/16.,1/16.,1/8.,1/8.,1/8.,1/8.,1/8.,1/2.)
- heptatonic = {
- "C":261.626
- ,"C#":277.183
- ,"Db":277.183
- ,"D": 293.665
- ,"D#":311.127
- ,"Eb":311.127
- ,"E": 329.628
- ,"F": 349.228
- ,"F#":369.994
- ,"Gb":369.994
- ,"G": 391.995
- ,"G#":415.305
- ,"Ab":415.305
- ,"A": 440.000
- ,"A#":466.164
- ,"Bb":466.164
- ,"B": 493.883
- ,"R":8
- }
- bpm = 114.0
- def initializeGPIO():
- GPIO.setmode(GPIO.BCM)
- for pin in pins:
- GPIO.setup(pin, GPIO.OUT)
- def start_buzzer(peizo):
- #peizo = GPIO.PWM(pins[1],100)
- for pin in pins:
- GPIO.output(pin,True)
- time.sleep(60/bpm)
- peizo.start(100)
- peizo.ChangeDutyCycle(90)
- def play_note(peizo,note):
- frequency = heptatonic[note["pitch"]] * 2**note["octave"]
- duration = note["duration"] * 60/bpm * 4
- peizo.ChangeFrequency(frequency)
- time.sleep(duration)
- def stop_buzzer(peizo):
- peizo.stop()
- def main():
- print('Note executed at ' + time.strftime("%c"))
- print ('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
- try:
- initializeGPIO()
- peizo = GPIO.PWM(pins[1],100)
- start_buzzer(peizo)
- pitches = score.split()
- octaves = octave.split()
- for i in range(0, len(pitches)):
- note ={
- "pitch":pitches[i]
- ,"octave": int(octaves[i])
- ,"duration":dur[i]
- }
- print note["pitch"] + " " + str(note["duration"])
- frequency = heptatonic[note["pitch"]] * 2**note["octave"]
- duration = note["duration"] * 60/bpm * 4
- peizo.ChangeFrequency(frequency)
- time.sleep(duration)
- #play_note(peizo,note)
- stop_buzzer(peizo)
- except(KeyboardInterrupt, SystemExit):
- print ('Note terminated at ' + time.strftime("%c"))
- finally:
- GPIO.cleanup()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment