-
Notifications
You must be signed in to change notification settings - Fork 14
/
midictrl.ino
83 lines (66 loc) · 1.92 KB
/
midictrl.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
#include <midi_serialization.h>
#include <usbmidi.h>
// See midictrl.png in the example folder for the wiring diagram,
// as well as README.md.
void sendCC(uint8_t channel, uint8_t control, uint8_t value) {
USBMIDI.write(0xB0 | (channel & 0xf));
USBMIDI.write(control & 0x7f);
USBMIDI.write(value & 0x7f);
}
void sendNote(uint8_t channel, uint8_t note, uint8_t velocity) {
USBMIDI.write((velocity != 0 ? 0x90 : 0x80) | (channel & 0xf));
USBMIDI.write(note & 0x7f);
USBMIDI.write(velocity &0x7f);
}
const int ANALOG_PIN_COUNT = 4;
const int BUTTON_PIN_COUNT = 2;
// Change the order of the pins to change the ctrl or note order.
int analogPins[ANALOG_PIN_COUNT] = { A3, A2, A1, A0 };
int buttonPins[BUTTON_PIN_COUNT] = { 9, 8 };
int ccValues[ANALOG_PIN_COUNT];
int buttonDown[BUTTON_PIN_COUNT];
int readCC(int pin) {
// Convert from 10bit value to 7bit.
return analogRead(pin) >> 3;
}
int isButtonDown(int pin) {
return digitalRead(pin) == 0;
}
void setup() {
// Initialize initial values.
for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
pinMode(analogPins[i], INPUT);
ccValues[i] = readCC(analogPins[i]);
}
for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
pinMode(buttonPins[i], INPUT);
digitalWrite(buttonPins[i], HIGH);
buttonDown[i] = isButtonDown(buttonPins[i]);
}
}
void loop() {
//Handle USB communication
USBMIDI.poll();
while (USBMIDI.available()) {
// We must read entire available data, so in case we receive incoming
// MIDI data, the host wouldn't get stuck.
u8 b = USBMIDI.read();
}
for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
int value = readCC(analogPins[i]);
// Send CC only if th has changed.
if (ccValues[i] != value) {
sendCC(0, i, value);
ccValues[i] = value;
}
}
for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
int down = isButtonDown(buttonPins[i]);
if (down != buttonDown[i]) {
sendNote(0, 64 + i, down ? 127 : 0);
buttonDown[i] = down;
}
}
// Flush the output.
USBMIDI.flush();
}