-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiano_Code.ino
281 lines (240 loc) · 4.5 KB
/
Piano_Code.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "sounds.h"
//Port Inputs
#define Key1 16
#define Key2 17
#define Key3 18
#define Key4 19
#define Key5 20
#define Key6 21
#define Key7 22
#define Key8 23
#define Key1Base 55
#define Key2Base 55
#define Key3Base 55
#define Key4Base 55
#define Key5Base 55
#define Key6Base 60
#define Key7Base 55
#define Key8Base 55
#define Key1Low 25
#define Key2Low 35
#define Key3Low 33
#define Key4Low 30
#define Key5Low 38
#define Key6Low 40
#define Key7Low 30
#define Key8Low 25
int currentNote = -1;
int KeyPressed[] = {-1, -1, -1, -1,
-1, -1, -1, -1};
int arrayFilled = 0;
//Powers the Port Inputs
#define Power 4
//Speaker Port Number
#define AudioDriver 5
void setup()
{
Serial.begin(9600);
//setup Key's to receive input
pinMode(Key1, INPUT);
pinMode(Key2, INPUT);
pinMode(Key3, INPUT);
pinMode(Key4, INPUT);
pinMode(Key5, INPUT);
pinMode(Key6, INPUT);
pinMode(Key7, INPUT);
pinMode(Key8, INPUT);
//set drivers
pinMode(Power, OUTPUT);
digitalWrite(Power, HIGH);
}
void printReads()
{
Serial.print("1: ");
Serial.print(analogRead(Key1));
Serial.print(" 2: ");
Serial.print(analogRead(Key2));
Serial.print(" 3: ");
Serial.print(analogRead(Key3));
Serial.print(" 4: ");
Serial.print(analogRead(Key4));
Serial.print(" 5: ");
Serial.print(analogRead(Key5));
Serial.print(" 6: ");
Serial.print(analogRead(Key6));
Serial.print(" 7: ");
Serial.print(analogRead(Key7));
Serial.print(" 8: ");
Serial.println(analogRead(Key8));
}
void printArrayValues()
{
for(int i = 0; i < 8; i++)
{
Serial.print(i);
Serial.print(": ");
Serial.print(KeyPressed[i]);
Serial.print(" ");
}
Serial.println(" ");
}
void loop()
{
delay(50);
printReads();
if (analogRead(Key1) < Key1Low)
{
addToArray(Key1);
}
if(analogRead(Key2) < Key2Low)
{
addToArray(Key2);
}
if (analogRead(Key3) < Key3Low)
{
addToArray(Key3);
}
if(analogRead(Key4) < Key4Low)
{
addToArray(Key4);
}
if (analogRead(Key5) < Key5Low)
{
// addToArray(Key5);
}
if(analogRead(Key6) < Key6Low)
{
addToArray(Key6);
}
if (analogRead(Key7) < Key7Low)
{
addToArray(Key7);
}
if(analogRead(Key8) < Key8Low)
{
addToArray(Key8);
}
if(analogRead(Key1) > Key1Base)
{
removeFromArray(Key1);
}
if(analogRead(Key2) > Key2Base)
{
removeFromArray(Key2);
}
if(analogRead(Key3) > Key3Base)
{
removeFromArray(Key3);
}
if(analogRead(Key4) > Key4Base)
{
removeFromArray(Key4);
}
if(analogRead(Key5) > Key5Base)
{
removeFromArray(Key5);
}
if(analogRead(Key6) > Key6Base)
{
removeFromArray(Key6);
}
if(analogRead(Key7) > Key7Base)
{
removeFromArray(Key7);
}
if(analogRead(Key8) > Key8Base)
{
removeFromArray(Key8);
}
playHighestNote();
}
void playHighestNote()
{
if(KeyPressed[0] != currentNote)
{
Serial.print("Current Note Changed to: ");
Serial.println(KeyPressed[0]);
currentNote = KeyPressed[0];
if(currentNote == -1)
{
UnpressKey();
}
else
{
Serial.print("Attempting to Play: ");
Serial.println(getFrequency(currentNote));
setCurrentSound(currentNote);
}
}
}
void addToArray(int keyNum)
{
if(arrayFilled == 8)
{
return;
}
for(int i = 0; i < 8; i++)
{
if(KeyPressed[i] == keyNum)
{
return;
}
}
for(int i = 6; i >= 0; i--)
{
KeyPressed[i+1] = KeyPressed[i];
}
Serial.print(keyNum);
Serial.println(" was pressed!");
KeyPressed[0] = keyNum;
printArrayValues();
arrayFilled++;
}
void removeFromArray(int keyNum)
{
if(arrayFilled <= 0)
return;
int index = 9;
for(int i = 0; i < 7; i++)
if(KeyPressed[i] == keyNum)
index = i;
if(index == 9)
return;
for(; index < 7; index++)
KeyPressed[index] = KeyPressed[index+1];
KeyPressed[7] = -1;
arrayFilled--;
}
int getFrequency(int note)
{
Serial.print("note val: ");
Serial.println(note);
switch(note)
{
case Key1:
return NOTE_C4;
case Key2:
return NOTE_D4;
case Key3:
return NOTE_E4;
case Key4:
return NOTE_F4;
case Key5:
return NOTE_G4;
case Key6:
return NOTE_A4;
case Key7:
return NOTE_B4;
case Key8:
return NOTE_C5;
}
return 0;
}
void setCurrentSound(int note)
{
tone(AudioDriver, getFrequency(note));
}
void UnpressKey ()
{
noTone(AudioDriver);
}