Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.3 kB
2
Indexable
Never
import RPi.GPIO as GPIO
import time

Buzzer = 17

CL = [0, 131, 147, 165, 175, 196, 211, 248]        # Frequency of Low C notes
CM = [0, 262, 294, 330, 350, 393, 441, 495]        # Frequency of Middle C notes
CH = [1, 525, 589, 661, 700, 786, 882, 990]        # Frequency of High C notes

song = [    CH[5], CH[2], CM[6], CH[2], CH[3], CH[6],CH[0], CH[3], # Notes of song
            CH[5], CH[3], CM[6], CH[2],CH[0]]

beat = [    1,1,1,1,1,1,2,1,1,1,1,1,3    ]

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(Buzzer, GPIO.OUT)
    global Buzz



def loop():
    while True:
        print ('\n    Playing song...')
        for i in range(1, len(song)):
            if  song[i] == 1 :
                time.sleep(beat[i] *0.25)
            else:
                Buzz = GPIO.PWM(Buzzer, song[i])
                Buzz.start(50)
                time.sleep(beat[i] * 0.25)
                Buzz.stop()
        time.sleep(1)             # Wait a second for next song.

def destory():
    Buzz.stop()
    GPIO.output(Buzzer, LOW)
    GPIO.cleanup()

if __name__ == '__main__':        # Program start from here
    setup()
    try:
        loop()
    except KeyboardInterrupt:     # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        destory()
Leave a Comment