-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ino
141 lines (140 loc) · 3.39 KB
/
app.ino
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#define PIN_PIEZO 3
#define PIN_LED_G 5
#define PIN_LED_R 6
#define PIN_SERVO 9
#include <Servo.h>
int8_t tone_pins[] = {-1, -1, -1, -1, 13, 12, 11, 10, -1};
char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D'};
int tones[] = {261, 294, 329, 369, 392, 440, 493, 523, 587};
int number_of_tones = 8;
char *sequence = "CCbgag";
int length = 6;
uint8_t next_index = 0;
char current_one = -1;
uint8_t pressed_one = -1;
boolean was_playing = false;
char *success_chime = "ddedgfddedagddDbggfeCCbgag";
uint8_t success_chime_durs[] = {8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 8, 8, 4, 4, 8, 8, 4, 4, 4, 2};
uint8_t success_chime_length = 26;
char *failure_chime = 'gfe';
uint8_t failure_chime_durs[] = {4, 4, 2};
uint8_t failure_chime_length = 3;
uint16_t tempo = 1200;
boolean was_correct = false;
boolean is_closed = false;
Servo opener;
void moveServoTo(int degrees)
{
opener.attach(PIN_SERVO);
opener.write(degrees);
delay(1500);
opener.detach();
}
void setup()
{
Serial.begin(9600);
Serial.println("Started");
moveServoTo(90);
is_closed = true;
}
void playChime(char *chime_names, uint8_t *durs, uint8_t length)
{
for (int i = 0; i < length; i++)
{
int frequency = -1;
for (int j = 0; j < number_of_tones; j++)
{
if (chime_names[i] == names[j])
{
frequency = tones[j];
break;
}
}
uint16_t duration = tempo / durs[i];
tone(PIN_PIEZO, frequency, duration);
delay(duration);
}
}
void loop()
{
int index = -1;
for (int i = 0; i < number_of_tones; i++)
{
if (tone_pins[i] != -1 && digitalRead(tone_pins[i]))
{
index = i;
Serial.print("Pressed ");
Serial.println(index);
break;
}
}
if (index != -1)
{
tone(PIN_PIEZO, tones[index], tempo / 4);
}
if (was_playing)
{
if (index == -1 && was_correct)
{
//stopped pressing
next_index++;
Serial.print("Got next tone. Now at ");
Serial.println(next_index);
if (length == next_index)
{
next_index = 0;
//success
digitalWrite(PIN_LED_G, 1);
playChime(success_chime, success_chime_durs, success_chime_length);
digitalWrite(PIN_LED_G, 0);
moveServoTo(0);
is_closed = false;
}
}
else
{
//still pressing
// do nothing
}
}
else
{
if (index == -1)
{
//does nothing
}
else
{
//started pressing
if (sequence[next_index] == names[index])
{
was_correct = true;
// correct
digitalWrite(PIN_LED_G, 1);
}
else
{
next_index = 0;
was_correct = false;
//incorrect
digitalWrite(PIN_LED_R, 1);
}
}
}
if (index != -1)
{
was_playing = true;
delay(tempo / 4);
digitalWrite(PIN_LED_G, 0);
digitalWrite(PIN_LED_R, 0);
if (!is_closed)
{
moveServoTo(90);
is_closed = true;
}
}
else
{
was_playing = false;
}
}