-
Notifications
You must be signed in to change notification settings - Fork 0
/
true-bypass-midi-pedal.ino
141 lines (122 loc) · 3.2 KB
/
true-bypass-midi-pedal.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
#include "MIDIUSB.h"
// script settings
#define CHANNEL 0
#define DEBUG 1
#define DEBOUNCE 300
// IO
#define SWITCH1 A0
#define SWITCH2 A1
#define BANKSWITCH A2
bool debug = DEBUG;
class Footswitch {
public:
Footswitch(uint8_t p) : pin(p) {}
// setup pin
void setup() {
pinMode(pin, INPUT_PULLUP);
}
// looking for changes in the state of the footswitch
void update(byte control) {
press(digitalRead(pin), control);
}
// returns state of the footswitch
bool isPressed() {
return pressed;
}
private:
const uint8_t pin;
const uint8_t channel = CHANNEL;
const long debounceTime = DEBOUNCE;
unsigned long lastPressed;
bool pressed = 0;
// sends midi signal on state change
void press(boolean state, byte control) {
if (state == pressed || (millis() - lastPressed <= debounceTime)) {
return;
}
state ? controlChange(channel, control, 127) : controlChange(channel, control, 0);
lastPressed = millis();
pressed = state;
if (debug) {
Serial.println();
Serial.print("control: ");
Serial.print(control);
Serial.println();
Serial.print("state: ");
Serial.print(state ? "on" : "off");
Serial.println();
}
}
//
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
MidiUSB.flush();
}
};
Footswitch footswitchs[] = {
SWITCH1,
SWITCH2,
};
const uint8_t NumSwitches = sizeof(footswitchs) / sizeof(Footswitch);
class BankSwitch {
public:
BankSwitch(uint8_t p) : pin(p) {}
// setup pin
void setup() {
pinMode(pin, INPUT_PULLUP);
}
// looking for state change
void update() {
flip(!digitalRead(pin));
}
// returns the current bank. Incriments by the number of footswitches
byte bankNum() {
return bank;
}
private:
const uint8_t pin;
const long debounceTime = DEBOUNCE;
unsigned long lastFlipped;
bool flipped = 0;
byte bank = 0;
// changes "bank" on state change
void flip(boolean state) {
// stops the changing of banks while an fx loop is enabled
// will run until loop is disabled. then will change bank as normal
for (byte i = 0; i < NumSwitches; i++) {
if (footswitchs[i].isPressed()) return;
}
if (state == flipped || (millis() - lastFlipped <= debounceTime)) {
return;
}
bank = state ? bank + NumSwitches : bank - NumSwitches;
lastFlipped = millis();
flipped = state;
if (debug) {
Serial.println();
Serial.print("bank: ");
Serial.print(bank);
Serial.println();
Serial.print("state: ");
Serial.print(state ? "on" : "off");
Serial.println();
}
}
};
BankSwitch bankswitch = BANKSWITCH;
void setup() {
if (debug) {
Serial.begin(9600);
}
for (byte i = 0; i < NumSwitches; i++) {
footswitchs[i].setup();
}
bankswitch.setup();
}
void loop() {
for (byte i = 0; i < NumSwitches; i++) {
footswitchs[i].update(i + bankswitch.bankNum());
}
bankswitch.update();
}