-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (72 loc) · 2.68 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import RPi.GPIO as GPIO #import the GPIO library
import time #import the time library
import random
class Buzzer(object):
def __init__(self):
GPIO.setmode(GPIO.BCM)
self.buzzer_pin = 5 #set to GPIO pin 5
GPIO.setup(self.buzzer_pin, GPIO.IN)
GPIO.setup(self.buzzer_pin, GPIO.OUT)
print("buzzer ready")
def __del__(self):
class_name = self.__class__.__name__
print (class_name, "finished")
def buzz(self,pitch, duration): #create the function “buzz” and feed it the pitch and duration)
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(self.buzzer_pin, True) #set pin 18 to high
time.sleep(delay) #wait with pin 18 high
GPIO.output(self.buzzer_pin, False) #set pin 18 to low
time.sleep(delay) #wait with pin 18 low
def play(self, tune):
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.buzzer_pin, GPIO.OUT)
x=0
print("Playing tune ",tune)
if(tune==1):
pitches=[262,294,330,349,392,440,494,523, 587, 659,698,784,880,988,1047]
duration=0.1
for p in pitches:
self.buzz(p, duration) #feed the pitch and duration to the function, “buzz”
time.sleep(duration *0.5)
for p in reversed(pitches):
self.buzz(p, duration)
time.sleep(duration *0.5)
elif(tune==2):
pitches=[262,330,392,523,1047]
duration=[0.2,0.2,0.2,0.2,0.2,0,5]
for p in pitches:
self.buzz(p, duration[x]) #feed the pitch and duration to the function, “buzz”
time.sleep(duration[x] *0.5)
x+=1
elif(tune==3):
pitches=[392,294,0,392,294,0,392,0,392,392,392,0,1047,262]
duration=[0.2,0.2,0.2,0.2,0.2,0.2,0.1,0.1,0.1,0.1,0.1,0.1,0.8,0.4]
for p in pitches:
self.buzz(p, duration[x]) #feed the pitch and duration to the func$
time.sleep(duration[x] *0.5)
x+=1
elif(tune==4):
pitches=[1047, 988,659]
duration=[0.1,0.1,0.2]
for p in pitches:
self.buzz(p, duration[x]) #feed the pitch and duration to the func$
time.sleep(duration[x] *0.5)
x+=1
elif(tune==5):
pitches=[1047, 988,523]
duration=[0.1,0.1,0.2]
for p in pitches:
self.buzz(p, duration[x]) #feed the pitch and duration to the func$
time.sleep(duration[x] *0.5)
x+=1
GPIO.setup(self.buzzer_pin, GPIO.IN)
if __name__ == "__main__":
a = random.randint(1, 5)
buzzer = Buzzer()
buzzer.play(int(a))