# -*- coding: utf-8 -*- buzzer_pin = 19 notes = {"C":256, "D",288,"E":324,"F":345,"G":385,"A":432,"B":485,"CC":513} def buzz(pitch, duration): #create the function “buzz” and feed it the pitch and duration) global buzzer_pin if pitch == 0: time.sleep(duration) return period = 1.0 / pitch #in physics, the period (sec/cyc) is the inverse of the frequency (cyc/sec) delay = period / 2 #calcuate the time for half of the wave cycles = int(duration * pitch) #the number of waves to produce is the duration times the frequency for i in range(cycles): #start a loop from 0 to the variable “cycles” calculated above GPIO.output(buzzer_pin, True) #set pin 18 to high time.sleep(delay) #wait with pin 18 high GPIO.output(buzzer_pin, False) #set pin 18 to low time.sleep(delay) #wait with pin 18 low def play(note, beats): global notes tone = notes[note] duration = .5 * beats buzz(tone, duration) play("C",4) play("E",4) play("G",4) play("CC",4)