notovny

Rick.py

Dec 11th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1.  
  2.  
  3. import RPi.GPIO as GPIO
  4. import time
  5. import os
  6.  
  7. pins = (13,26)
  8.  
  9.  
  10. score = 'Ab Bb Db Bb F F Eb ' \
  11.         'Ab Bb Db Bb Eb Eb Db C Bb '\
  12.         'Ab Bb Db Bb Db Eb C Bb Ab R Ab Eb Db R '\
  13.         'Ab Bb Db Bb F F Eb '\
  14.         'Ab Bb Db Bb Ab C Db C Bb '\
  15.         'Ab Bb Db Bb Db Eb C Bb Ab R Ab Eb Db Db'
  16.  
  17. octave = '0 0 1 0 1 1 1 ' \
  18.         '0 0 1 0 1 1 1 0 0 '\
  19.         '0 0 1 0 1 1 1 0 0 0 0 1 1 0 '\
  20.         '0 0 1 0 1 1 1 '\
  21.         '0 0 1 0 0 1 1 1 0 '\
  22.         '0 0 1 0 1 1 1 0 0 0 0 1 1 1'
  23.  
  24. # 1/16 =0.0625
  25.  
  26. dur = (0.0625,0.0625,0.0625,0.0625,3.0/16,3.0/16,3.0/8,
  27.     1/16.,1/16.,1/16.,1/16.,3/16.,3/16.,3/16.,1/16.,1/8.,
  28.     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.,
  29.     1/16.,1/16.,1/16.,1/16.,3/16.,3/16.,3/8.,
  30.     1/16.,1/16.,1/16.,1/16.,1/4.,1/8.,3/16.,1/16.,1/8.,
  31.     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.)
  32.  
  33.  
  34. heptatonic = {
  35.     "C":261.626
  36.     ,"C#":277.183
  37.     ,"Db":277.183
  38.     ,"D": 293.665
  39.     ,"D#":311.127
  40.     ,"Eb":311.127
  41.     ,"E": 329.628
  42.     ,"F": 349.228
  43.     ,"F#":369.994
  44.     ,"Gb":369.994
  45.     ,"G": 391.995
  46.     ,"G#":415.305
  47.     ,"Ab":415.305
  48.     ,"A": 440.000
  49.     ,"A#":466.164
  50.     ,"Bb":466.164
  51.     ,"B": 493.883
  52.     ,"R":8
  53. }
  54.  
  55. bpm = 114.0
  56.  
  57. def initializeGPIO():
  58.     GPIO.setmode(GPIO.BCM)
  59.     for pin in pins:
  60.         GPIO.setup(pin, GPIO.OUT)
  61.  
  62.  
  63. def start_buzzer(peizo):
  64.     #peizo = GPIO.PWM(pins[1],100)
  65.     for pin in pins:
  66.         GPIO.output(pin,True)
  67.  
  68.     time.sleep(60/bpm)
  69.     peizo.start(100)
  70.     peizo.ChangeDutyCycle(90)
  71.  
  72. def play_note(peizo,note):
  73.     frequency = heptatonic[note["pitch"]] * 2**note["octave"]
  74.     duration = note["duration"] * 60/bpm * 4
  75.     peizo.ChangeFrequency(frequency)
  76.     time.sleep(duration)
  77.  
  78. def stop_buzzer(peizo):
  79.     peizo.stop()
  80.  
  81. def main():
  82.     print('Note executed at ' + time.strftime("%c"))
  83.     print ('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
  84.     try:
  85.         initializeGPIO()
  86.         peizo = GPIO.PWM(pins[1],100)
  87.         start_buzzer(peizo)
  88.  
  89.         pitches = score.split()
  90.         octaves = octave.split()
  91.  
  92.         for i in range(0, len(pitches)):
  93.             note ={
  94.                 "pitch":pitches[i]
  95.                 ,"octave": int(octaves[i])
  96.                 ,"duration":dur[i]
  97.             }
  98.             print note["pitch"] + " " + str(note["duration"])
  99.             frequency = heptatonic[note["pitch"]] * 2**note["octave"]
  100.             duration = note["duration"] * 60/bpm * 4
  101.             peizo.ChangeFrequency(frequency)
  102.             time.sleep(duration)
  103.             #play_note(peizo,note)
  104.  
  105.         stop_buzzer(peizo)
  106.     except(KeyboardInterrupt, SystemExit):
  107.         print ('Note terminated at ' + time.strftime("%c"))
  108.     finally:
  109.         GPIO.cleanup()
  110.  
  111.  
  112.  
  113. if __name__ == '__main__':
  114.     main()
Advertisement
Add Comment
Please, Sign In to add comment