-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathpotentiometers.cpp
More file actions
128 lines (116 loc) · 3.89 KB
/
Copy pathpotentiometers.cpp
File metadata and controls
128 lines (116 loc) · 3.89 KB
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
/*
Moura's Keyboard Scanner: turn you broken (or unused) keyboard in a MIDI controller
Copyright (C) 2017 Daniel Moura <oxesoft@gmail.com>
This code is originally hosted at https://github.com/oxesoft/keyboardscanner
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "globals.h"
#define POTS_RESOLUTION_MICROSECONDS 5000
#define POTS_THRESHOLD_VALUE 8 // 1024 divided by 128
#define POTS_PB_CENTER_DEADZONE 4
#define POTS_NUMBER 2
#define POT_TYPE_PITCHBEND 0xE000
#define POT_TYPE_MODWHEEL 0xB001
#define POT_TYPE_VOLUME 0xB007
#define POT_TYPE_PAN 0xB00A
#define POT_TYPE_EXPRESSION 0xB00B
#define POT_TYPE_RESONANCE 0xB047
#define POT_TYPE_FILTER 0xB04A
#define POT_TYPE_REVERB 0xB05B
#define POT_TYPE_CHORUS 0xB05D
const int POTS_ANALOG_PINS[POTS_NUMBER] = {
A0,
A1
};
const int POTS_TYPES[POTS_NUMBER] = {
POT_TYPE_PITCHBEND,
POT_TYPE_MODWHEEL
};
/*
5V ────┬────────────
│
[ POT ] ← linear 10 kΩ
│
└────── A0
│
GND ────┴────────────
TIPS:
- Use linear potentiometers (B-taper) of 10 kΩ
- Avoid logarithmic/audio (A-taper)
- For extra precision, add 100 nF from A0 to GND
- If this module is enabled and there is no potentiometers connected, connects the configured inputs to GND
*/
int analogRawValues[POTS_NUMBER] = {0}; // 10-bit ADC (0–1023)
int midiValues[POTS_NUMBER] = {0};
unsigned long lastReadingTime = 0;
void potentiometersSetup()
{
for (int i = 0; i < POTS_NUMBER; i++)
{
if (POTS_TYPES[i] == POT_TYPE_PITCHBEND)
{
midiValues[i] = 8192;
}
}
}
void potentiometersLoop()
{
unsigned long currentTime = micros();
if (currentTime - lastReadingTime < POTS_RESOLUTION_MICROSECONDS)
{
return;
}
for (int i = 0; i < POTS_NUMBER; i++)
{
int raw = analogRead(POTS_ANALOG_PINS[i]);
if (POTS_TYPES[i] == POT_TYPE_PITCHBEND)
{
const int CENTER = 512;
int value;
if (abs(raw - CENTER) <= POTS_PB_CENTER_DEADZONE)
{
value = 8192;
}
else if (raw < CENTER)
{
value = map(raw, 0, CENTER - POTS_PB_CENTER_DEADZONE - 1, 0, 8191);
}
else
{
value = map(raw, CENTER + POTS_PB_CENTER_DEADZONE + 1, 1023, 8192, 16383);
}
if (midiValues[i] == value)
{
continue;
}
byte status = (POTS_TYPES[i] & 0xFF00) >> 8;
midiValues[i] = value;
byte lsb = value & 0x7F;
byte msb = value >> 7;
sendMidiEvent(status, lsb, msb);
}
else
{
int lastRaw = analogRawValues[i];
if (abs(raw - lastRaw) < POTS_THRESHOLD_VALUE)
{
continue;
}
analogRawValues[i] = raw;
byte status = (POTS_TYPES[i] & 0xFF00) >> 8;
byte cc = POTS_TYPES[i] & 0x00FF;
byte value = raw >> 3; // scale to MIDI range (0–127)
sendMidiEvent(status, cc, value);
}
}
lastReadingTime = currentTime;
}