-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIDI_glockenspiel.ino
267 lines (248 loc) · 6.81 KB
/
MIDI_glockenspiel.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
/*
* Larry Bonnette 04/23/2021
* fire a solenoid when a certain MIDI note is recieved
* The solenoid is used to strike a note on a glockenspiel
*/
#include <Wire.h>
#include "Adafruit_MCP23017.h" //MCP23017 - i2c 16 input/output port expander
#include <MIDI.h> // Add Midi Library
#define LED 13 // Arduino Board LED is on Pin 13
void ProgRel(); //Declare so it compiles
//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();
int period = 13; // Part of non-blocking delay to turn off solenoid
unsigned long time_now = 0; // Part of non-blocking delay
// Create mcp object for first MCP23017 board
Adafruit_MCP23017 mcp;
// Create mcp1 object for second MCP23017 board
Adafruit_MCP23017 mcp1;
int pinRelease = 99;
int pinRelease1 = 99;
void setup() {
pinMode (LED, OUTPUT); // Set Arduino board LED pin 13 to output
MIDI.begin(MIDI_CHANNEL_OMNI); // Initialize the Midi Library.
// OMNI sets it to listen to all channels.. MIDI.begin(2) would set it
// to respond to notes on channel 2 only.
mcp.begin(); // Initialize the MCP23017 library to use default address 0
mcp1.begin(1); // Initialize the second MCP23017 library to use address 21
Serial.begin(115200); // Initialize the serial port to 115200 baud rate. This will be used for MIDI communication
// setting multiple pins as OUTPUT
int outMin = 0; // Lowest output pin
int outMax = 15; // Highest output pin
for(int i=outMin; i<=outMax; i++)
{
mcp.pinMode(i, OUTPUT);
mcp1.pinMode(i,OUTPUT);
}
MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
// tells the Midi Library which function you want to call when a NOTE ON command
// is received. In this case it's "MyHandleNoteOn".
MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library
// to call "MyHandleNoteOff" when a NOTE OFF command is received.
}
void loop() { // Main loop
if (millis() >= time_now + period) // Part of non-blocking delay
{
time_now = 0;
ProgRel(); //turns off the solenoid after the non-blocking delay
}
MIDI.read(); // Continuously check if Midi data has been received.
}
// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
switch (pitch) {
case 53:
mcp.digitalWrite(0,HIGH);
time_now = millis();
pinRelease = 0;
break;
case 54:
mcp.digitalWrite(1,HIGH);
time_now = millis();
pinRelease = 1;
break;
case 55:
mcp.digitalWrite(2,HIGH);
time_now = millis();
pinRelease = 2;
break;
case 56:
mcp.digitalWrite(3,HIGH);
time_now = millis();
pinRelease = 3;
break;
case 57:
mcp.digitalWrite(4,HIGH);
time_now = millis();
pinRelease = 4;
break;
case 58:
mcp.digitalWrite(5,HIGH);
time_now = millis();
pinRelease = 5;
break;
case 59:
mcp.digitalWrite(6,HIGH);
time_now = millis();
pinRelease = 6;
break;
case 60:
mcp.digitalWrite(7,HIGH);
time_now = millis();
pinRelease = 7;
break;
case 61:
mcp.digitalWrite(8,HIGH);
time_now = millis();
pinRelease = 8;
break;
case 62:
mcp.digitalWrite(9,HIGH);
time_now = millis();
pinRelease = 9;
break;
case 63:
mcp.digitalWrite(10,HIGH);
time_now = millis();
pinRelease = 10;
break;
case 64:
mcp.digitalWrite(11,HIGH);
time_now = millis();
pinRelease = 11;
break;
case 65:
mcp.digitalWrite(12,HIGH);
time_now = millis();
pinRelease = 12;
break;
case 66:
mcp.digitalWrite(13,HIGH);
time_now = millis();
pinRelease = 13;
break;
case 67:
mcp.digitalWrite(14,HIGH);
time_now = millis();
pinRelease = 14;
break;
case 68:
mcp.digitalWrite(15,HIGH);
time_now = millis();
pinRelease = 15;
break;
case 69:
mcp1.digitalWrite(0,HIGH); //Start accessing the second MCP23017
time_now = millis();
pinRelease1 = 0;
break;
case 70:
mcp1.digitalWrite(1,HIGH);
time_now = millis();
pinRelease1 = 1;
break;
case 71:
mcp1.digitalWrite(2,HIGH);
time_now = millis();
pinRelease1 = 2;
break;
case 72:
mcp1.digitalWrite(3,HIGH);
time_now = millis();
pinRelease1 = 3;
break;
case 73:
mcp1.digitalWrite(4,HIGH);
time_now = millis();
pinRelease1 = 4;
break;
case 74:
mcp1.digitalWrite(5,HIGH);
time_now = millis();
pinRelease1 = 5;
break;
case 75:
mcp1.digitalWrite(6,HIGH);
time_now = millis();
pinRelease1 = 6;
break;
case 76:
mcp1.digitalWrite(7,HIGH);
time_now = millis();
pinRelease1 = 7;
break;
case 77:
mcp1.digitalWrite(8,HIGH);
time_now = millis();
pinRelease1 = 8;
break;
case 78:
mcp1.digitalWrite(9,HIGH);
time_now = millis();
pinRelease1 = 9;
break;
case 79:
mcp1.digitalWrite(10,HIGH);
time_now = millis();
pinRelease1 = 10;
break;
case 80:
mcp1.digitalWrite(11,HIGH);
time_now = millis();
pinRelease1 = 11;
break;
case 81:
mcp1.digitalWrite(12,HIGH);
time_now = millis();
pinRelease1 = 12;
break;
case 82:
mcp1.digitalWrite(13,HIGH);
time_now = millis();
pinRelease1 = 13;
break;
case 83:
mcp1.digitalWrite(14,HIGH);
time_now = millis();
pinRelease1 = 14;
break;
case 84:
mcp1.digitalWrite(15,HIGH);
time_now = millis();
pinRelease1 = 15;
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
digitalWrite(LED,HIGH); //Turn LED on
}
// MyHandleNoteOff is the function that will be called by the Midi Library
// when a MIDI NOTE Off message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
// This routine is used to fulfill the note off sent to us. The "ProgRel" routine is the real solenoid release routine
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) {
/* if (pinRelease != 99){
mcp.digitalWrite(pinRelease,LOW);
pinRelease = 99;
}else
if (pinRelease1 != 99) {
mcp1.digitalWrite(pinRelease1,LOW);
pinRelease1 = 99;
}*/
digitalWrite(LED,LOW); //Turn LED off
}
void ProgRel() {
if (pinRelease != 99){
mcp.digitalWrite(pinRelease,LOW);
pinRelease = 99;
}else
if (pinRelease1 != 99) {
mcp1.digitalWrite(pinRelease1,LOW);
pinRelease1 = 99;
}
digitalWrite(LED,LOW); //Turn LED off
}