/
output_module.py
113 lines (95 loc) · 2.07 KB
/
output_module.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
servo = 4
servo2 = 12
ledPin = 27
push_button = 22
buzzer = 18
SPEED = 1
# List of tone-names with frequency
TONES = {"c6":1047,
"b5":988,
"a5":880,
"g5":784,
"f5":698,
"e5":659,
"eb5":622,
"d5":587,
"c5":523,
"b4":494,
"a4":440,
"ab4":415,
"g4":392,
"f4":349,
"e4":330,
"d4":294,
"c4":262}
CORRECT = [["a5", 32], ["c5", 16]]
WRONG = [["ab4", 3]]
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(buzzer, GPIO.OUT)
GPIO.setup(push_button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(servo, GPIO.OUT)
GPIO.setup(servo2, GPIO.OUT)
motor = GPIO.PWM(servo, 50)
motor2 = GPIO.PWM(servo2, 50)
# GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback)
def led_on():
GPIO.output(ledPin, GPIO.HIGH)
def led_off():
GPIO.output(ledPin, GPIO.LOW)
def buzzer_on():
GPIO.output(buzzer, 1)
def buzzer_off():
GPIO.output(buzzer, 0)
def buzzer_correct():
p = GPIO.PWM(buzzer, 440)
p.start(0.5)
for t in CORRECT:
playTone(p, t)
def buzzer_wrong():
p = GPIO.PWM(buzzer, 440)
p.start(0.5)
for t in WRONG:
playTone(p, t)
# def set_angle(fr, to, vec):
# for i in range(fr, to, vec):
# duty = 1./18. * i + 2
# # GPIO.output(servo, True)
# motor.ChangeDutyCycle(duty)
# time.sleep(0.02)
# # GPIO.output(servo, True)
# # motor.ChangeDutyCycle(0)
def set_angle(angle):
duty = 1.0/18.0 * angle + 2
motor.ChangeDutyCycle(duty)
motor2.ChangeDutyCycle(duty)
time.sleep(1)
motor.ChangeDutyCycle(0)
motor2.ChangeDutyCycle(0)
def playTone(p, tone):
# calculate duration based on speed and tone-length
duration = (1./(tone[1]*0.25*SPEED))
if tone[0] == "p": # p => pause
time.sleep(duration)
else: # let's rock
frequency = TONES[tone[0]]
p.ChangeFrequency(frequency)
p.start(0.5)
time.sleep(duration)
p.stop()
def clean_up():
GPIO.clean_up()
motor.start(0)
motor2.start(0)
# # set_angle(0, 180, 1)
# # set_angle(180, 0, -1)
# # motor.ChangeDutyCycle(12)
# # time.sleep(1);
# set_angle(90)
# sleep(3)
# set_angle(180)
# # set_angle(0)
# motor.stop()