-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMidiHack.ino
181 lines (132 loc) · 3.34 KB
/
MidiHack.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
/*
MIDI Hack
*/
/* ===== Buttons ===== */
#define NUM_BUTTONS 4
#define BUTTON_BASE 4
int buttons[NUM_BUTTONS];
int newButtons[NUM_BUTTONS];
/* ===== Sonar ===== */
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
// Maximum sensor distance is rated at 400-500cm.
#define MAX_DISTANCE 40 // Maximum distance we want to ping for (in centimeters).
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int midiValDist = 0;
int sentMidiValDist = 0;
int sonarDelay = 0;
/* ===== Joystick ===== */
int midiValX = 0;
int midiValY = 0;
int sentMidiValX = 0;
int sentMidiValY = 0;
/* ===== Midi ===== */
// #define PRINT_MONITOR
#define WRITE_MIDI
// Standard Midi values
#define NOTE_OFF_CH0 0x80
#define NOTE_ON_CH0 0x90
#define CC_CH0 0xB0
/* ===== Debug ===== */
// #define BUTTON_DEBUG
// #define SONAR_DEBUG
// #define STICK_DEBUG
void setup() {
for (int i=0; i < BUTTON_BASE; i++) {
pinMode(BUTTON_BASE + i, INPUT);
}
Serial.begin(9600);
}
void loop() {
sonarDelay++;
readJoystick(midiValX, midiValY);
if (sonarDelay == 100) {
midiValDist = readSonar();
sonarDelay = 0;
}
readButtons(newButtons);
if (sentMidiValX != midiValX) {
sendCommand(0x10, midiValX);
sentMidiValX = midiValX;
}
if (sentMidiValY != midiValY) {
sendCommand(0x11, midiValY);
sentMidiValY = midiValY;
}
if (sentMidiValDist != midiValDist) {
sendCommand(0x12, midiValDist);
sentMidiValDist = midiValDist;
}
for (int i=0; i < NUM_BUTTONS; i++) {
if (buttons[i] != newButtons[i]) {
if (newButtons[i] == 0) {
noteOff(36 + i, 127);
} else {
noteOn(36 + i, 127);
}
#ifdef BUTTON_DEBUG
Serial.print(i);
Serial.print(" button: ");
Serial.println(newButtons[i]);
#endif
buttons[i] = newButtons[i];
}
}
}
void readButtons(int *btns) {
for (int i=0; i < NUM_BUTTONS; i++) {
btns[i] = !digitalRead(BUTTON_BASE + i);
}
}
int readSonar() {
unsigned int pingVal = 0;
byte result = 0;
pingVal = sonar.ping_median(5);
pingVal = sonar.convert_cm(pingVal);
// Remove offset and re-scale
pingVal = 3 * (max(pingVal - 7, 0));
result = min(pingVal, 127);
result = 127 - result;
#ifdef SONAR_DEBUG
Serial.println(result);
#endif
}
void readJoystick(int &horiz, int &vert) {
int horizVal = 0;
int horizPin = A0;
int vertVal = 0;
int vertPin = A1;
horizVal = analogRead(horizPin);
horiz = horizVal / 8;
vertVal = analogRead(vertPin);
vert = vertVal / 8;
#ifdef STICK_DEBUG
Serial.print(vert);
Serial.print(" - ");
Serial.println(horiz);
#endif
}
void sendCommand(int cc, int value) {
writeMidi(CC_CH0, cc, value);
}
// Plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(int pitch, int velocity) {
writeMidi(NOTE_ON_CH0, pitch, velocity);
}
void noteOff(int pitch, int velocity) {
writeMidi(NOTE_OFF_CH0, pitch, velocity);
}
void writeMidi(int byte1, int byte2, int byte3) {
#ifdef PRINT_MONITOR
Serial.println(byte1);
Serial.println(byte2);
Serial.println(byte3);
#endif
#ifdef WRITE_MIDI
Serial.write(byte1);
Serial.write(byte2);
Serial.write(byte3);
#endif
}